469,891 Members | 2,251 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,891 developers. It's quick & easy.

Passing a Cookie with httplib

Hello,

From a shell script, I have used /usr/bin/curl to access a web site
and pass a cookie (as required by the site). But, I can't seem to
accomplish this task with Python. I would like to use the httplib
module to do this. Any thoughts on this subject? I would like to
hard code the cookie in the code so it works every time:

i.e. cookie = 'auth=buster%3A12345678901234567890exZ9rzMqgtxa5A' .

The question is - how do I pass it with httplib? Here is what I have
so far:
# Import external modules
import sys
import httplib

# Define our site and path
WP_SERVER="www.foo.com"
WP_PATH='/somedir/confirmquery?value=%s'
try:
# Grab the command line argument
uservalue = sys.argv[1]
# Use the httplib module and connect
conn=httplib.HTTPConnection(WP_SERVER)
conn.request('GET',WP_PATH % uservalue)
response=conn.getresponse()
data=response.read()
conn.close()
print data
except:
print "Some stupid error occurred"
sys.exit(1)
Any help would be greatly appreciated,

Scott
Jun 28 '06 #1
5 19399
On 2006-06-28, sc***@bogusaddress.com <sc***@bogusaddress.com> wrote:
From a shell script, I have used /usr/bin/curl to access a web site
and pass a cookie


I use ClientCookie for that.

http://wwwsearch.sourceforge.net/ClientCookie/

--
Grant Edwards grante Yow! I'm into SOFTWARE!
at
visi.com
Jun 28 '06 #2
In article <i2********************************@4ax.com>,
sc***@bogusaddress.com wrote:
I would like to
hard code the cookie in the code so it works every time:

i.e. cookie = 'auth=buster%3A12345678901234567890exZ9rzMqgtxa5A' .

conn=httplib.HTTPConnection(WP_SERVER)
conn.request('GET',WP_PATH % uservalue)


According to <http://docs.python.org/lib/httpconnection-objects.html>,
you can pass additional "body" and "headers" args to
HTTPConnection.request. How about trying something like this in place of
the last line above:

Headers = {"Cookie" :
"auth=buster%3A12345678901234567890exZ9rzMqgtxa5A" }
conn.request('GET',WP_PATH % uservalue, None, Headers)
Jun 29 '06 #3
On Thu, 29 Jun 2006 21:42:50 +1200, Lawrence D'Oliveiro
<ld*@geek-central.gen.new_zealand> wrote:
According to <http://docs.python.org/lib/httpconnection-objects.html>,
you can pass additional "body" and "headers" args to
HTTPConnection.request. How about trying something like this in place of
the last line above:

Headers = {"Cookie" :
"auth=buster%3A12345678901234567890exZ9rzMqgtxa5A "}
conn.request('GET',WP_PATH % uservalue, None, Headers)


Perfect! Thanks so much. I appreciate your help. I looked through
the docs before, but didn't spot this nugget because I was focused on
searching for the text "cookie". Obviously, it pays to read... :-)

Scott
Jun 29 '06 #4
On Wed, 28 Jun 2006 20:21:37 -0000, Grant Edwards <gr****@visi.com>
wrote:
On 2006-06-28, sc***@bogusaddress.com <sc***@bogusaddress.com> wrote:
From a shell script, I have used /usr/bin/curl to access a web site
and pass a cookie


I use ClientCookie for that.

http://wwwsearch.sourceforge.net/ClientCookie/


Thanks Grant. I'll take a look at this solution.

Scott
Jun 29 '06 #5
Grant Edwards <gr****@visi.com> writes:
On 2006-06-28, sc***@bogusaddress.com <sc***@bogusaddress.com> wrote:
From a shell script, I have used /usr/bin/curl to access a web site
and pass a cookie
I use ClientCookie for that.

http://wwwsearch.sourceforge.net/ClientCookie/


Note that ClientCookie has moved, to become part of mechanize (well,
is moving -- mechanize is still in beta):
http://wwwsearch.sourceforge.net/ClientCookie/

mechanize exports a superset of the ClientCookie interface, so "import
mechanize as ClientCookie" should be all you need to do to switch
(modulo some trivial details, documented at the URL below).

Also note that module cookielib in the Python 2.4 stdlib contains most
of the functionality of ClientCookie (specifically, all the cookie
handling code, of course).
John
Jun 29 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Glauco | last post: by
reply views Thread by Shagshag | last post: by
13 posts views Thread by Manlio Perillo | last post: by
1 post views Thread by Brian Beck | last post: by
reply views Thread by Robert | last post: by
2 posts views Thread by James Stroud | last post: by
7 posts views Thread by itay_k | last post: by
4 posts views Thread by Patrick Altman | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.