Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Submitting POST data (http://www.programmingforums.org/showthread.php?t=7757)

Sane Dec 29th, 2005 7:24 PM

Submitting POST data
 
Okay, so using urllib2 I can send GET data easily to a website like so:

:

import urllib2
opener = urllib2.build_opener()
page = opener.open( 'http://jammersbase.ath.cx?error=Sent%20some%20get%20data...' ).read()
print "Sent GET data successfully!"


But how do you suppose I sent POST data to a website? I need this so I can login to a website using an automated script.

Will urllib2 store your session as if you're browing directly on the site, or is there some fancy thing I need to do for that? :o

Dameon Dec 29th, 2005 7:45 PM

I believe you would use the optional data parameter.

Cerulean Dec 29th, 2005 8:27 PM

As Dameon said, pass the POST data as the second parameter to urlopen. You can either construct the POST query string by yourself, or use urllib.urlencode to construct the query string from you from a dictionary, e.g
:

p = urllib.urlopen("http://foo.com/", urllib.urlencode({"foo" : "bar", "bar" : "baz"}))
Quote:

Originally Posted by Sane
Will urllib2 store your session as if you're browing directly on the site, or is there some fancy thing I need to do for that?

Depends on the kind of session handling used by the server. If the session ID is added to URLs generated by the server then sure, you won't have to do anything. If the session ID is stored in a cookie, however, you'll need to do a little more work and set up a cookie jar to do so. Python 2.4 includes a built-in module cookielib which lets you do this, but earlier versions of Python can use an external cookie library (with more or less the same usage as cookielib) called ClientCookie.

Sane Dec 29th, 2005 10:25 PM

Oooh! Pretty!! ^^;;

The cookie thing will need some experimentin' with though. Thanks.


All times are GMT -5. The time now is 6:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC