I highly recommend using the HTTP request library. He will handle it all for you.
http://docs.python-requests.org/en/latest/
import requests sess = requests.session() sess.post("http://somesite.com/someform.php", data={"username": "me", "password": "pass"})
edit: There are many ways to save a session to disk. You can do the following:
from requests.utils import cookiejar_from_dict as jar cookies = jar(sess.cookies)
Then read the following documentation. You can convert it to FileCookieJar and save the cookies to a text file and then load them at the beginning of the program.
http://docs.python.org/2/library/cookielib.html#cookiejar-and-filecookiejar-objects
Alternatively, you can disassemble the dict and save this data to a file and load it using pickle.load(file) .
http://docs.python.org/2/library/pickle.html
edit 2: To handle the expiration, you can iterate over the CookieJar as follows. cj it is assumed that CookieJar is obtained in some way.
for cookie in cj: if cookie.is_expired():
To check if any of the cookies have expired, it may be more convenient to do if any(c.is_expired() for c in cj) .
source share