473,804 Members | 4,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.urlretri eve("http://www.thewebsiten amehere.com/servlet/com.blah.bloo.X mlFeed",
"content.tx t", None, urllib.urlencod e({"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 5185

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.urlretri eve("http://www.thewebsiten amehere.com/servlet/com.blah.bloo.X mlFeed",
"content.tx t", None, urllib.urlencod e({"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.urlretri eve("http://www.thewebsiten amehere.com/servlet/com.blah.bloo.
XmlFeed", "content.tx t", None, urllib.urlencod e({"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.urlretri eve("http://www.thewebsiten amehere.com/servlet/com.blah.bloo.
XmlFeed",
"content.txt" , None, urllib.urlencod e({"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 HTTPEquivProces sor 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
4323
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, Nirmal
1
1889
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 has been validated, this token is unique per login and is valid unitl the user logouts or their session has timed out. How would I achieve automatic login? Do I have to store the username and password in the cookie?
3
432
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 automatically login on development machine, but i prefer to not to have such setting in the code. Are there any special settings in web.config so Forms authentication login automatically as certain UID and PWD?
23
3220
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) { var today = new Date(); var expire = new Date(); if (days == null || isNaN(days) || days == 0) days = 1; if (days >= 1 || days < 0) expire.setTime(today.getTime() + 3600000 * 24 * days);
17
4184
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 hosted at two.com If I view the frameset from one.com in Firefox, all works well with the content from two.com. But if trying to view this using IE (with standard security settings), the cookie set by two.com is not accessible.
7
1927
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 like using js but the the brief demanded a solution. Now the only difference between the two style sheets is one line:
6
2704
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 problem is how do I handle the "path" parameter in cookie. First, the sequence start at http://www.testServer1.com/TestApp/page1.htm, and a cookie is set at
6
4367
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 whole project runs in the UTF-8 encoding. The Internet Explorer handles this cookie correctly, whereas the FireFox writes only some byte gargabe and then reads it back.
10
8157
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 authenticated and login the system automatically base on the username and password already in the session. Is that possible to do ? and how to do it? Cheers Victor
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.