0

According to the documentation, this is the format:

Request.set_proxy(host, type)

So what I have is this for example:

Request.set_proxy("localhost:8888","http")

What is the format if the proxy requires a username and password?

User
  • 23,729
  • 38
  • 124
  • 207
  • Duplicate of [this question](http://stackoverflow.com/questions/34079/how-to-specify-an-authenticated-proxy-for-a-python-http-connection) – mhawke Jun 06 '14 at 02:03
  • Lol no it's not. That's using an opener. I want to use it on the request object like I've shown above. – User Jun 06 '14 at 02:27
  • Laugh away... but you can't do it directly via Request's public interface, but you can manually set the Proxy-Authorization header - see answer below. Generally, you are better of using the "opener" approach, or even better to use the [requests](http://docs.python-requests.org/en/latest/user/advanced/#proxies) library - if your requirements permit. – mhawke Jun 06 '14 at 05:58

1 Answers1

1

Two ways to do this if you must use a urllib2.Request object:

Set environment variables http_proxy and/or https_proxy either outside of the Python interpreter, or internally using os.environ['http_proxy'] before you import urllib2.

import os
os.environ['http_proxy'] = 'http://user:password@localhost:8888'
import urllib2
req = urllib2.Request('http://www.blah.com')
f = urllib2.urlopen(req)

Or manually set HTTP request header:

import urllib2
from base64 import urlsafe_b64encode
PROXY_USERNAME = 'user'
PROXY_PASSWORD = 'password'
req = urllib2.Request('http://www.blah.com')
req.set_proxy('localhost:8888', 'http')
proxy_auth = urlsafe_b64encode('%s:%s' % (PROXY_USERNAME, PROXY_PASSWORD))
req.add_header('Proxy-Authorization', 'Basic %s' % proxy_auth)
f = urllib2.urlopen(req)
mhawke
  • 84,695
  • 9
  • 117
  • 138