473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

urlib - automatic cookie handling

I'm using urllib to post data to a web form by issuing a command similar to this:
filename, headers =
urllib.urlretrieve("http://www.thewebsitenamehere.com/servlet/com.blah.bloo.XmlFeed",
"content.txt", None, urllib.urlencode({"aParameter": "theValue"}))

Now, the problem is that the above fails, since I am not sending a session
cookie. Visitors to the web sites' html submission form are sent a session
cookie which is given back to the server when they submit a search via the
browser, as often happens.
Now, I could use urllib to get the form page and read the cookie from the
headers that are returned and then manually put that cookie in my submission to
the servlet, but my question is: is there a way to tell urllib or some other
part of HTTP handling in python that I want to remember any cookie that is
given to me, and give it back to that site if I send requests later on?

thanks
alex

Jul 18 '05 #1
5 5166

Yes.

First fetch the URL which gives you the cookie. Parse the HTTP headers to
get the cookie (use the header parsing function and get the Set-Cookie
header). Then send the cookie in your headers along your next request, and
you're in. For this you must use urllib2 which allows custom headers (use
request.headers or something).

I'm using urllib to post data to a web form by issuing a command similar
to this:
filename, headers =
urllib.urlretrieve("http://www.thewebsitenamehere.com/servlet/com.blah.bloo.XmlFeed",
"content.txt", None, urllib.urlencode({"aParameter": "theValue"}))

Now, the problem is that the above fails, since I am not sending a
session cookie. Visitors to the web sites' html submission form are sent
a session cookie which is given back to the server when they submit a
search via the browser, as often happens.
Now, I could use urllib to get the form page and read the cookie from
the headers that are returned and then manually put that cookie in my
submission to the servlet, but my question is: is there a way to tell
urllib or some other part of HTTP handling in python that I want to
remember any cookie that is given to me, and give it back to that site
if I send requests later on?

thanks
alex


Jul 18 '05 #2
Pierre-Frédéric Caillaud wrote:

Yes.

First fetch the URL which gives you the cookie. Parse the HTTP
headers to get the cookie (use the header parsing function and get the
Set-Cookie header). Then send the cookie in your headers along your
next request, and you're in. For this you must use urllib2 which allows
custom headers (use request.headers or something).

thanks for your response!
An even better answer, which I've just found:
http://wwwsearch.sourceforge.net/ClientCookie

Can I ask that you please don't top-post? It makes threads hard to follow...

thanks
alex

Jul 18 '05 #3
Alex Hunsley wrote:
I'm using urllib to post data to a web form by issuing a command similar
to this:

filename, headers =
urllib.urlretrieve("http://www.thewebsitenamehere.com/servlet/com.blah.bloo.
XmlFeed", "content.txt", None, urllib.urlencode({"aParameter": "theValue"}))

Now, the problem is that the above fails, since I am not sending a session
cookie. Visitors to the web sites' html submission form are sent a session
cookie which is given back to the server when they submit a search via the
browser, as often happens.
Now, I could use urllib to get the form page and read the cookie from the
headers that are returned and then manually put that cookie in my
submission to the servlet, but my question is: is there a way to tell urllib or some other part of HTTP handling in python that I want to remember
any cookie that is given to me, and give it back to that site if I send
requests later on?


You're looking for ClientCookie:

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

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

-Mike
Jul 18 '05 #4
Michael Geary wrote:
Alex Hunsley wrote:
I'm using urllib to post data to a web form by issuing a command similar
to this:

filename, headers =


urllib.urlretrieve("http://www.thewebsitenamehere.com/servlet/com.blah.bloo.
XmlFeed",
"content.txt", None, urllib.urlencode({"aParameter": "theValue"}))

Now, the problem is that the above fails, since I am not sending a session
cookie. Visitors to the web sites' html submission form are sent a session
cookie which is given back to the server when they submit a search via the
browser, as often happens.
Now, I could use urllib to get the form page and read the cookie from the
headers that are returned and then manually put that cookie in my
submission to the servlet, but my question is: is there a way to tell


urllib
or some other part of HTTP handling in python that I want to remember
any cookie that is given to me, and give it back to that site if I send
requests later on?

You're looking for ClientCookie:

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

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

-Mike


hi there!
Snap! I think we both posted at about the same time. Good to know someone was
suggesting it anyway!

thanks
alex

Jul 18 '05 #5
Alex Hunsley <la**@tardis.ed.ac.molar.uk> writes:
[...]
http://wwwsearch.sourceforge.net/ClientCookie/
[...] Snap! I think we both posted at about the same time. Good to know
someone was suggesting it anyway!


You might care to know that the cookie-handling part of ClientCookie
has found its way into Python 2.4, as module cookielib. So, the
development version of CC (which is compatible with old versions of
Python) is currently moving towards being as-compatible-as-possible
with cookielib and 2.4 urllib2.

Actually, if you're using Python 2.3 and want to be
forwards-compatible with 2.4 here, I'm 95% sure you can just grab
cookielib and urllib2 from Python CVS and use those. cookielib
doesn't import urllib2, so you can leave 2.3's urllib2 intact and use
a local copy of 2.4's urllib2, eg. by keeping it somewhere like
~/lib/python and renaming it urllib2_24.py. cookielib doesn't include
stuff like HTTPEquivProcessor that are in ClientCookie, but I'll make
those available in a form that works with 2.4 in a separate package
soon (that package will probably be ClientCookie itself, in a 1.0.x or
late 0.9.x version).

I should mention these things on the web page...
John
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: N.K | last post by:
Hi , Python's existing cookie module doesnt supports new cookie headers SetCookie2 , How to submit a patch for that ? I tried emailing person who owns that module.But no response. Thanks,...
1
by: Jim | last post by:
I have an asp.net site and I want to provide automatic login when a user returns to my site, similar to amazon.com. Currently my site authenticates the user and generates a token when the user...
3
by: A.M | last post by:
Hi, I have implemented forms authentication on my application, but I have to fill the login form every time i run the web app during debugging phases. I know that i can change the code to...
23
by: Phil Powell | last post by:
// OBTAINED FROM http://www.javascripter.net/faq/settinga.htm // NOTE THAT IF YOU SET days TO -1 THE COOKIE WILL BE SET TO YESTERDAY AND THUS EXPIRE function setCookie(name, value, days, docObj)...
17
by: Bruno | last post by:
I have a feature that is hosted on a different domain from the primary one in a frame, and need to retain values in a cookie. example: A web page at one.com contains a frame which has a page...
7
by: T J Rogers | last post by:
Hi folks, Below is a script that I use on a site to detect the size of the browser window then automatically load an alternative stile sheet when appropriate. Its a bit clumsy, and I don't...
6
by: kelvlam | last post by:
Hello all, I'm still a bit new with JavaScript, and I hope the guru here can shed some light for me. It's regarding handling cookie and the case-sensitive nature of JavaScript itself. My...
6
by: Victor | last post by:
Hi everybody, could anybody help me with the following problem : I need to set a cookie containing a Russian character string as the value, using the construct "document.cookie = ...". The...
10
by: Victor | last post by:
hi guys. In my project, now I am using a asp.net login control and a customized membership provider to do the form authentication. Now I want some function that user can skip the login form and be...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.