How to share session with IE

John J. Lee jjl at pobox.com
Sat Oct 14 10:54:21 EDT 2006


Cameron Walsh <cameron.walsh at gmail.com> writes:
[...]
> Another option instead of making your program run through a series of
> clicks and text inputs, which is difficult to program, is to browse
> the html source until you find the name of the script that processes
> the login, and use python to request the page with the necessary form
> fields encoded in the request.  Request something like
> http://www.targetsite.com/login.cgi?username=pyuser&password="fhqwhgads"
> This format is not guaranteed to work, since the login script or
> server might only support one of GET and POST.  If this is the case,
> creating the request is slightly more involved and to be honest I
> haven't looked into how to do it.

Absolutely, that's often a great way to do things, since it's very
simple, and is not in conflict with handling cookies (where that's
required).

(But of course if you need to handle cookies, you still need to
arrange to actually handle the cookies somewhere.)


> Thereafter, you will have to pass the environment to every page
> request so the server can read the cookie.  Which brings me to
> question whether or not it is possible to do this manually once,
> export the environment variable to a file, and reload this file each
> time the program is run. Or to generate the cookie in the environment
> yourself.
[...]

Standard library module cookielib (or mechanize, which is not part of
the stdlib, and does some more stuff automatically and provides some
extra features for page navigation and form handling) does all this
automatically:

http://docs.python.org/lib/cookielib-examples.html

import cookielib, urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
r = opener.open("http://example.com/")


For loading and saving (including Firefox support):

http://docs.python.org/lib/file-cookie-jar-classes.html

http://docs.python.org/lib/cookie-jar-objects.html


For loading IE cookies, use mechanize.

http://wwwsearch.sourceforge.net/mechanize/


John



More information about the Python-list mailing list