Passing a Cookie with httplib

S

scott

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
 
L

Lawrence D'Oliveiro

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)
 
S

scott

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
 
J

John J. Lee

Grant Edwards said:

Note that ClientCookie has moved, to become part of mechanize (well,
is moving -- mechanize is still in beta):


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top