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?
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?
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)