hit counter

Timeline

My development logbook

Whenever You Think You Need Urllib2, You Actually Need Requests

So, today, when I want to write up a simple test to post data to a web server, I imported lib urllib2 right away.

It usually works great… until you need something more than basic.

Apparently I need to set some cookie and set to the server. Let’s import cookiejar and cookie…

Why it throws an exception AttributeError: ‘SimpleCookie’ object has no attribute +’rfc2965’?

OK, let’s import DefaultCookiePolicy. Then I got hit by a missing domain attribute exception. “Do I need to use a lower level Cookie class to set the domain?”, I wonder. Try switching to Cookie and it throw this exception

1
2
3
4
Traceback (most recent call last):
    ...
    c = Cookie()
TypeError: __init__() takes at least 17 arguments (1 given)

At this point I said to myself: No, I am not going to figure out the 17 arguments. My test code is at this point in this dismal state:.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    import urllib2
    from cookielib import Cookie, CookieJar, DefaultCookiePolicy
    from Cookie import SimpleCookie

    policy = DefaultCookiePolicy(rfc2965=True)

    c = SimpleCookie()
    c['secretkey'] = auth_token
    cj = CookieJar(policy)
    cj.set_cookie(c)

    req = urllib2.Request("%s/postdata" % self.server_url)
    proxyHandler = urllib2.ProxyHandler( {} )
    defaultErrorHandler = urllib2.HTTPDefaultErrorHandler()
    cookieHandler = urllib2.HTTPCookieProcessor(cj)
    opener = urllib2.build_opener( proxyHandler, defaultErrorHandler, cookieHandler )
    opener.open(req, data, 10000)

Instead of continuing banging my head against a wall, I recall reading about ‘requests’ module. Let give it a go.

Like magic, it is all I need. 2 lines.

1
2
     import requests
     requests.post(url, data, cookie=cookie, headers=headers)

It just works. Well done, requests!