Using Cookie and Proxy handler with urllib2
Joshua Gilman
joshuagilman at gmail.com
Sat Feb 9 23:15:33 EST 2008
Hello, here is my current code:
import urllib, urllib2, cookielib
class wrapper:
def __init__(self):
self.setupCookies()
def request(self, address, postData=None, headers={ }):
if not postData == None:
postData = urllib.urlencode(postData)
req = urllib2.Request(address, postData, headers)
return urllib2.urlopen(req).read()
def loadCookies(self, cookieFile):
self.cookieJar.load(cookieFile)
def saveCookies(self, cookieFile):
self.cookieJar.save(cookieFile)
def useProxy(self, proxy):
proxymap = {'http': proxy}
self.cookieJar = cookielib.LWPCookieJar()
director = urllib2.OpenerDirector()
director.add_handler(urllib2.ProxyHandler(proxymap))
director.add_handler(urllib2.HTTPCookieProcessor(self.cookieJar))
urllib2.install_opener(director)
def setupCookies(self):
self.cookieJar = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(
self.cookieJar))
urllib2.install_opener(opener)
proxyhttp = wrapper()
proxyhttp.useProxy(proxy)
postData = {'username': username, 'password': password}
content = proxyhttp.request('http://blah.com/login.php', postData)
Now, basically what this should do is send a POST request to login.php using
the given proxy and then save the returned cookies using the cookie handler.
Either I'm doing something wrong or the proxy and cookie handler don't place
nicely together. Here is the resulting error:
File "C:\Python25\neolib\wrapper.py", line 11, in request
return urllib2.urlopen(req).read()
File "C:\Python25\lib\urllib2.py", line 121, in urlopen
return _opener.open(url, data)
File "C:\Python25\lib\urllib2.py", line 380, in open
response = meth(req, response)
File "C:\Python25\lib\urllib2.py", line 1124, in http_response
self.cookiejar.extract_cookies(response, request)
File "C:\Python25\lib\cookielib.py", line 1627, in extract_cookies
_debug("extract_cookies: %s", response.info())
AttributeError: 'NoneType' object has no attribute 'info'
I don't know much about these handlers to really understand what's going
wrong. I assume it has to be something with these two handlers conflicting
because I can't seem to find anything wrong with my code. Is this a bug or
am I doing something wrong? Help is much appreciated, thanks.
Cheers,
Josh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080209/42501baf/attachment.html>
More information about the Python-list
mailing list