Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 29th, 2005, 7:24 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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
Sane is offline   Reply With Quote
Old Dec 29th, 2005, 7:45 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
I believe you would use the optional data parameter.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Dec 29th, 2005, 8:27 PM   #3
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Dec 29th, 2005, 10:25 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
Oooh! Pretty!! ^^;;

The cookie thing will need some experimentin' with though. Thanks.
Sane is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:31 PM.

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