question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set PROXIES for requests?

See original GitHub issue

I was looking for a way to set proxy in order to make requests. At the moment I’ve patched base backend:

proxies = getattr(settings, 'SOCIAL_SERVICES_PROXIES', {})
old_request = BaseAuth.request

@wraps(old_request)
def new_request(self, url, method='GET', *args, **kwargs):
    '''
    COPY-PASTE from social_core.backends.base import BaseAuth
    with proxy support
    '''
    kwargs.setdefault('headers', {})
    if self.setting('VERIFY_SSL') is not None:
        kwargs.setdefault('verify', self.setting('VERIFY_SSL'))
    kwargs.setdefault('timeout', self.setting('REQUESTS_TIMEOUT') or self.setting('URLOPEN_TIMEOUT'))
    if self.SEND_USER_AGENT and 'User-Agent' not in kwargs['headers']:
        kwargs['headers']['User-Agent'] = self.setting('USER_AGENT') or user_agent()

    kwargs.setdefault('proxies', proxies)  # main line

    try:
        if self.SSL_PROTOCOL:
            session = SSLHttpAdapter.ssl_adapter_session(self.SSL_PROTOCOL)
            response = session.request(method, url, *args, **kwargs)
        else:
            response = request(method, url, *args, **kwargs)
    except ConnectionError as err:
        LOG.info(str(kwargs))
        LOG.error(str(err))
        raise AuthFailed(self, str(err))
    response.raise_for_status()
    return response

BaseAuth.request = new_request

Is there a better way to do that?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
POD666commented, Apr 24, 2018

@msadig Usually I create ‘adapters’ app for such purposes. Then I define AppConfig for each 3rd party lib patching and import exect patch in ready method. So I can include those apps in a right place in INSTALLED_APPS.

adapters/
    __init__.py
    apps.py
    patch_social_auth.py  # contains that code inside `def patch():`
    patch_something_other.py  # something other


# adapters/apps.py
from django.apps.config import AppConfig
from .patch_social_auth import patch as patch_social_auth
from .patch_something_other import patch as patch_something_other

class AddProxySupport(AppConfig):
    verbose_name = "Add proxy support "
    def ready(self):
        patch_social_auth()
...

# myproject/settings.py
INSTALLED_APPS = [
    # ...
    'adapters.apps.AddProxySupport',
    # ...
]
0reactions
bd808commented, Sep 21, 2021

A SOCIAL_AUTH_PROXIES config var seems to have been quietly added for this use case in 25ed3b6242e89f644b3d4a4d235496905b4bc9c1 as part of the 3.4.0 release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using a Proxy Server with Python requests - Datagy
In order to use proxies in the requests Python library, you need to create a dictionary that defines the HTTP, HTTPS, and FTP...
Read more >
Proxies with Python 'Requests' module - Stack Overflow
If you need to use a proxy, you can configure individual requests with the proxies argument to any request method: import requests proxies...
Read more >
How to use proxies with Python Requests Proxy Module - Zyte
In this part, we're going to cover how to configure proxies in Requests. To get started we need a working proxy and a...
Read more >
How do I use a proxy server with Python Requests? - ReqBin
Another way to pass a proxy address to Python is to set the http_proxy and https_proxy environment variables. If you set environment variables, ......
Read more >
Requests behind corporate proxies in Python - Plotly
Note that proxy URLs must include the scheme. You may also see this error if your proxy variable is set but you are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found