473,473 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 19790
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Aki Niimura | last post by:
Hello everyone, I have been trying to write a Python script to access a Web site. I'm currently having a problem completing my script because the contents I would like to access in the Web...
2
by: Glauco | last post by:
I'm using a library based on httplib. Recently i've done a conversion for use of https with a key and certificate file. This goes perfectly :-) . Now, the problem is passing throw an http_proxy....
0
by: Shagshag | last post by:
hello, how can i set multiple cookies like : Set-Cookie: cookiename1=value1; expires=Tue, 31-Mar-09 11:17:43 GMT; path=/; domain=www.domain.com Set-Cookie: cookiename2=value2; expires=Tue,...
13
by: Manlio Perillo | last post by:
Hi. I'm using the Cookie module (on the client side). I have found a problem trying to parse the cookie: "Set-Cookie: value=thevalue; path=/; expires=Fri, 21-May-2004 10:40:51 GMT" The date...
1
by: Brian Beck | last post by:
Hi. I'm having some problems with code based directly on the following httplib documentation code: http://www.zvon.org/other/python/doc21/lib/httplib-examples.html I've included the code and...
0
by: Robert | last post by:
did you solve this problem? It seems to be still present here with py2.3.5. Robert -- From: Manish Jethani <manish.j@gmx.net> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US;...
2
by: James Stroud | last post by:
Hello All, I want to use python to download files from sites where authentication is required. The page appears to send a form with the login and pass by post. I would like to log in and keep...
7
by: itay_k | last post by:
Hi, I dont understand why this is so complicated, just to add one line of cookie header on the GET request. This is my unworking code: import time import Cookie import cookielib, urllib2
4
by: Patrick Altman | last post by:
I am attempting to use a HEAD request against Amazon S3 to check whether a file exists or not and if it does parse the md5 hash from the ETag in the response to verify the contents of the file so...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.