Using Chris Atlee’s python poster library is there any way to include cookie handling?
I have python http login code, which works with cookies:
cookiejar = cookielib.CookieJar()
urlOpener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
request = urllib2.Request(login_url, params)
result = urlOpener.open(request)
But when I need to upload a file, I don’t know how to use both poster lib code and cookie handling code. Poster lib seems to need to call urllib2.urlopen() and not some custom url opener, like in the code above.
For instance, I don’t know how to use cookies with the python file post code below:
register_openers()
params = {'file': open("test.txt", "rb"), 'name': 'upload test'}
datagen, headers = multipart_encode(params)
request = urllib2.Request(upload_url, datagen, headers)
result = urllib2.urlopen(request)
I sent an email to Chris AtLee asking whether we could get a basic authentication example. He was very cool about answering my questions and even ran some example code I sent him.
To include cookie handling, you do something like this:
To add basic authentication to the request, you simply do this (I added the base64 encode line for completeness):
Hope this helps. And another big thanks to Chris AtLee.