Python proxy error with request library

I am trying to access the network through a proxy in Python. I use the request library and I had a problem authenticating my proxy server, because the proxy server I use requires a password.

proxyDict = { 
          'http'  : 'username:mypassword@77.75.105.165', 
          'https' : 'username:mypassword@77.75.105.165'
        }
r = requests.get("http://www.google.com", proxies=proxyDict)

I get the following error:

Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
r = requests.get("http://www.google.com", proxies=proxyDict)
File "C:\Python27\lib\site-packages\requests\api.py", line 78, in get
:param url: URL for the new :class:`Request` object.
File "C:\Python27\lib\site-packages\requests\api.py", line 65, in request
"""Sends a POST request. Returns :class:`Response` object.
File "C:\Python27\lib\site-packages\requests\sessions.py", line 187, in request
def head(self, url, **kwargs):
File "C:\Python27\lib\site-packages\requests\models.py", line 407, in send
"""
File "C:\Python27\lib\site-packages\requests\packages\urllib3\poolmanager.py", line     127, in proxy_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line    521, in connection_from_url
File "C:\Python27\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 497, in get_host
ValueError: invalid literal for int() with base 10: 'h6f2v6jh5dsxa@77.75.105.165'

How to solve this?

Thanks in advance for your help.

+8
source share
3 answers

You must remove the built-in username and password from proxyDictand use the parameter instead auth.

import requests
from requests.auth import HTTPProxyAuth

proxyDict = { 
          'http'  : '77.75.105.165', 
          'https' : '77.75.105.165'
        }
auth = HTTPProxyAuth('username', 'mypassword')

r = requests.get("http://www.google.com", proxies=proxyDict, auth=auth)
+15
source

Windows , requests - , Python. :

set HTTP_PROXY=http://77.75.105.165
set HTTPS_PROXY=https://77.75.105.165

, , , URL-. , 8443, :

set HTTP_PROXY=http://77.75.105.165:8443
set HTTPS_PROXY=https://77.75.105.165:8443
+3

You can use the library for this urllib.

from urllib import request
request.urlopen("your URL", proxies=request.getproxies())
0
source

All Articles