Python – Keeping a session in python while making HTTP requests


I need to write a python script that makes multiple HTTP requests to the same site. Unless I'm wrong (and I may very well be) urllib reauthenticates for every request. For reasons I won't go into I need to be able to authenticate once and then use that session for the rest of my requests.

I'm using python 2.3.4

Best Solution

Use Requests library. From :

The Session object allows you to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance.

s = requests.session()

r = s.get("")

print r.text
# '{"cookies": {"sessioncookie": "123456789"}}'