Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass proxy-authentication to python Requests module

I'm working on existing python code that uses the request module to perform get/post towards a web site.

The python code also allow the use of proxies when a proxy is passed to the script otherwise no proxies are used.

My problem regards the use of proxies that requires authentication.

proxy = user:password@ip:port
self.s = requests.Session()
if proxy != "":
      self.proxies = {
        "http": "http://" + proxy,
        "https": "https://" + proxy
      }
      self.s.proxies.update(proxies)

self.s.get('http://checkip.dyndns.org')

In the code above, if the proxy is configured each get is refused because no authentication is provided.

I found a solution that implies the use of HTTPProxyAuth.

This is the solution that I found:

import requests
s = requests.Session()

proxies = {
  "http": "http://ip:port",
  "https": "https://ip:port"
}

auth = HTTPProxyAuth("user", "pwd")
ext_ip = s.get('http://checkip.dyndns.org', proxies=proxies, auth=auth)
print ext_ip.text

My question is: is it possibile to set the proxy authorization globally instead of modify each s.get in the code? Because sometime the script could be used without proxy and sometimes with proxy.

Thanks in advance!

Best Regards, Federico

like image 327
Federico Avatar asked Aug 27 '18 16:08

Federico


People also ask

How do you add a proxy to a request in Python?

In order to use proxies in the requests Python library, you need to create a dictionary that defines the HTTP, HTTPS, and FTP connections. This allows each connection to map to an individual URL and port. This process is the same for any request being made, including GET requests and POST requests.

How do I authenticate API requests in Python?

There are a few common authentication methods for REST APIs that can be handled with Python Requests. The simplest way is to pass your username and password to the appropriate endpoint as HTTP Basic Auth; this is equivalent to typing your username and password into a website.

How do I change proxy settings in Python?

You can configure proxies by setting the environment variables HTTP_PROXY and HTTPS_PROXY.


1 Answers

I found the solution, about how to set proxy authentication globally:

import requests
import urllib2
import re
from requests.auth import HTTPProxyAuth

s = requests.Session()

proxies = {
  "http": "http://185.165.193.208:8000",
  "https": "https://185.165.193.208:8000"
}

auth = HTTPProxyAuth("gRRT7n", "NMu8g0")

s.proxies = proxies
s.auth = auth        # Set authorization parameters globally

ext_ip = s.get('http://checkip.dyndns.org')
print ext_ip.text

BR, Federico

like image 53
Federico Avatar answered Oct 12 '22 16:10

Federico