2

I like urllib2 for automated routine operations, like cookie handling (with cookielib.CookieJar) or redirects processing. But i also like httplib for the low-level control that programmer have. For example, with httplib i can control for the order of HTTP headers but with urllib2 i cannot. Also, with httplib i can easily set Content-Type header to whatever i need and send, but not with urllib2 - it takes too much control over headers for itself and may rewrite my headers by its internal logic.

I would stay with httplib, but cookies processing with httplib is not very easy task. I can't connect it to cookielib and cookies parsing and processing is not so simple task to be done in 20 minutes with developing my own cookie-handling class.

Is there any cookies-processing solution standard for httplib?

Is there a way to control over order of headers to be sent with urllib2 and turn off its intellect that may add new headers?

pavelkolodin
  • 2,859
  • 3
  • 31
  • 74
  • 2
    i prefer [requests](http://docs.python-requests.org/en/latest/) – Foo Bar User Oct 04 '13 at 20:31
  • actualy i've done http requests using js,java,groovy,python(urllib2 and requests) and i believe requests is the best by far. very easy to use and decent documentation. – Foo Bar User Oct 04 '13 at 20:37
  • @FooBarUser does it allows you to control over headers order? Does it support cookies? – pavelkolodin Oct 04 '13 at 21:06
  • [cookies](http://docs.python-requests.org/en/latest/user/quickstart/#cookies) for headers i know it takes a dict as an arg, i believe if you pass ordereddict it will work in order but i've never tried it. – Foo Bar User Oct 04 '13 at 21:14
  • if you tried requests and it doesn't work for headers order can you please post back? i will raise an issue in github if it doesn't work. thank you. – Foo Bar User Oct 08 '13 at 15:36
  • 1
    @FooBarUser my choise is `httplib` + `import Cookie`. – pavelkolodin Oct 08 '13 at 21:20
  • thank you for the reply i just thought if you tried and didnt work we could contribute to make it better. – Foo Bar User Oct 08 '13 at 21:25

1 Answers1

2

You can pass headers to httplib.

import httplib
headers = {"Cookie": "foobar",
           "Accept": "text/plain"}
conn = httplib.HTTPConnection("example.org")
conn.request("GET", "", "", headers)
response = conn.getresponse()
conn.close()

You could still use Cookielib for managing them.

karlcow
  • 6,977
  • 4
  • 38
  • 72