Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Proxy Error With Requests Library

I am trying to access the web via a proxy server in Python. I am using the requests library and I am having an issue with authenticating my proxy as the proxy I am using requires a password.

proxyDict = { 
          'http'  : 'username:[email protected]', 
          'https' : 'username:[email protected]'
        }
r = requests.get("http://www.google.com", proxies=proxyDict)

I am getting 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: '[email protected]'

How do I solve this?

Thanks in advance for your help.

like image 421
Mark Collier Avatar asked Jan 14 '12 13:01

Mark Collier


2 Answers

You should remove the embedded username and password from proxyDict, and use the auth parameter instead.

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)
like image 121
reclosedev Avatar answered Sep 29 '22 01:09

reclosedev


I've been having a similar problem on Windows and found the only way to get requests to work was to set the proxies as environment variables before I started Python. For you this would be something like this:

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

You might also want to check is there's a specific port required, and if so set it after the url. For example, if the port is 8443 then do:

set HTTP_PROXY=http://77.75.105.165:8443
set HTTPS_PROXY=https://77.75.105.165:8443
like image 30
Sean Avatar answered Sep 29 '22 02:09

Sean