473,811 Members | 3,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

send cookie on request with urllib2

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

c= cookielib.Cooki e(1,"Name","Tom ", 80,False, "itay", False, False,
"d:\\asddd",Fal se, False,time.time ()+1000,False,N one,None,None)
cj = cookielib.Cooki eJar()
cj.set_cookie(c )
opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
opener.open(r'h ttp://itay/temp.html")

why this isn't working?
Itay is my computer name. it is an IIS server too.
the code is running, but i dont see any cookie that attached to the GET
request.

thanks alot!

Apr 20 '06 #1
7 19086
"itay_k" <it****@gmail.c om> writes:
Hi,

I dont understand why this is so complicated, just to add one line of
cookie header on the GET request.
You haven't said what you're really trying to do.

http://www.catb.org/~esr/faqs/smart-questions.html#goal

This is my unworking code:
import time
import Cookie
import cookielib, urllib2

c= cookielib.Cooki e(1,"Name","Tom ", 80,False, "itay", False, False,
"d:\\asddd",Fal se, False,time.time ()+1000,False,N one,None,None)
Constructing your own Cookie instances is rarely necessary or
sensible. If you do, I recommend getting the constructor arguments by
inspecting the Cookie object the server actually returns, rather than
using the values you *think* you know are correct.

cj = cookielib.Cooki eJar()
cj.set_cookie(c )
opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
opener.open(r'h ttp://itay/temp.html")

why this isn't working?


Please define "working".

If you want to handle cookies when opening URLs, just do this:

opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor())
opener.open("ht tp://itay/temp.html")
# more .open() calls go here...
Of course, on the first HTTP request, cookie handling cannot have any
effect at all unless you've somehow loaded some old cookies first.
Your .open() may only involve a single HTTP request (though
server-side software may detect the absence of a cookie on the first
request, and do a page refresh or redirect so it can see its cookie
returned to it again; vanilla urllib2 handles redirects but not
refreshes; package ClientCookie handles the latter, amongst other
things).

Regardless, turning on cookielib and httplib debug output will likely
be helpful if you're stuck (if only to post the output to this
newsgroup):

import logging, urllib2, sys

hh = urllib2.HTTPHan dler()
hsh = urllib2.HTTPSHa ndler()
hh.set_http_deb uglevel(1)
hsh.set_http_de buglevel(1)
opener = urllib2.build_o pener(hh, hsh, urllib2.HTTPCoo kieProcessor())
logger = logging.getLogg er("cookielib" )
logger.addHandl er(logging.Stre amHandler(sys.s tdout))
logger.setLevel (logging.DEBUG)

response = opener.open("ht tp://wwwsearch.sf.ne t/cgi-bin/cookietest.cgi" )
John

Apr 20 '06 #2
itay_k wrote:
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

c= cookielib.Cooki e(1,"Name","Tom ", 80,False, "itay", False, False,
"d:\\asddd",Fal se, False,time.time ()+1000,False,N one,None,None)


^^^^ path is the server path to which the cookie applies. Try '/'.

Kent
Apr 20 '06 #3
Kent Johnson <ke**@kentsjohn son.com> writes:
itay_k wrote:
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

c= cookielib.Cooki e(1,"Name","Tom ", 80,False, "itay", False, False,
"d:\\asddd",Fal se, False,time.time ()+1000,False,N one,None,None)


^^^^ path is the server path to which the cookie applies. Try '/'.


"""
"No," scolded Yoda. "Do, or do not. There is no try."
"""

(there, a Star Wars quote -- I guess there's truly no hope of ever
erasing my geek status now!-)

Why guess? Why not log in (using Python) and see what the cookie
actually is? Once you've actually done that, there's nothing to stop
you storing it as a Cookie constructor call.

I know I certainly don't remember all the *truly horrendous* detail of
what *exactly* all those parameters mean :-) The Cookie object, as is
documented, is merely a struct object and it is easy to construct
incorrect and even self-inconsistent Cookie objects; CookieJar has all
the knowledge about constructing cookies -- and allows use of that
knowledge through .make_cookies() and .load()/.revert().
John

Apr 20 '06 #4
ok.
i will explain what exactly i wanna do.

i want to simulate the following client-side script with python:
<body>
<img name="Pic">

<script>
document.cookie ="name=tom";
document.images["Pic"].src="temp2.htm l"
</script>

what that happen here, is when this page loading, the browser loads
"temp2.html " with HTTP header:
Cookie: name=tom;

this cookie does not come for the server, this is temporary cookie that
sending from the client to the server.
this cookie doesnt save on disk at all.

is it possible to implements this at python??

Apr 21 '06 #5
"itay_k" <it****@gmail.c om> writes:
ok.
i will explain what exactly i wanna do.

i want to simulate the following client-side script with python:
<body>
<img name="Pic">

<script>
document.cookie ="name=tom";
document.images["Pic"].src="temp2.htm l"
</script>


Ah! In which case what you're trying to do is a reasonable hack, but
better (UNTESTED):

import urllib2, cookielib
cj = cookielib.Cooki eJar
opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
request = urllib2.Request (url)
response = opener.open(req uest)
response["Set-Cookie"] = "name=tom"
cj.extract_cook ies(response, request)
If you have HTML-parsing code to extract these JS cookies that you
want to run on every request (e.g. so even cookies set by JS during
redirections get handled), you can make all this transparent very
easily by using a handler similar to HTTPCookieProce ssor itself (using
a recent version of package ClientCookie here for (a recent version
of) the response_seek_w rapper class) UNTESTED:
import urllib2, cookielib

class JSHTTPCookiePro cessor(urllib2. BaseHandler):
handler_order = 400 # before HTTPCookieProce ssor
def process_respons e(self, request, response):
from ClientCookie import response_seek_w rapper
if not hasattr(respons e, "seek"):
response = response_seek_w rapper(response )
try:
name, value = get_js_cookie(r esponse) # your ugly HTML parsing code here ;-)
finally:
response.seek(0 )
response["Set-Cookie"] = "%s=%s" % (name, value)
return response

opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(),
JSHTTPCookiePro cessor())
response = opener.open(url ) # now we're handling JS cookies transparently!

John

Apr 21 '06 #6
In article <ma************ *************** ************@py thon.org>,
jj*@pobox.com (John J. Lee) wrote:
"No," scolded Yoda. "Do, or do not. There is no try."


Convincing argument against exceptions, don't you think. :)
Apr 22 '06 #7
Thanks!

Apr 22 '06 #8

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

Similar topics

3
3500
by: Venkat | last post by:
Hi All, I have to send a request to JSP with some key value pairs which is executing in a Web server. Please help me in solving this problem. I tried with the following procedure but I am not successful. { URL url;
5
2354
by: Rafael T. Ugolini | last post by:
Im trying to add more than one cookie with urllib2 but im not getting much sucess. An example... >>> req = urllib2.Request('http://localhost','j_username=psyca&j_password=******',{...
2
10327
by: Matt | last post by:
The JSP page needs to send XML request string to another JSP page when the user clicks submit button, I wonder how to get started because when user click submit button, it will send the form to page2.jsp. <form action="page2.jsp" method="POST"> Name: <input type="text" name="fname"> //etc.. controls <input type="submit"> </form>
1
1360
by: DFS | last post by:
MS has made many, many $millions selling Access as part of Office Pro. There's no excuse for it not to have a better ER diagramming component, so that when you talk to clients or work with other IT people you have something more than the amateurish model currently produced. Those interested should send a request to support@microsoft.com and ask them to include some of the Visio ER diagramming features in Access (download 189mb Visio...
1
2248
by: Vimala Sri | last post by:
Hello all, Have a great day. I wish to made interaction from c program in unix to the java servlet. That is i want to send a request from the Unix c program to the servlet. I have no idea on how to do this? The exact scenario is In the client there is a c program which send a request to the server (servlet in java) using poat method and should the response from the servlet. The...
3
2575
by: korque | last post by:
Hi I'm gathering information from web page, first I get headers and store recieved cookies then I get source with file()-function. Is there easy way to send cookie value back to host on each page, so my script would be recognized as web browser and sessid wouldn't change on every page ?
5
3135
by: Jimmy | last post by:
How can I do that? Is it possible to send a server request (i.e. GET) without refreshing the web page? Using Javascript? Or Ajax (i.e. AjaxAnywhere)? Or if there's a way (i.e. using AjaxAnywhere) to send the 1st GET request without updating any zone to server #1? If so, then I can send the 2nd GET request to server #2 and update a particular zone in the web page. Thanks,
3
8189
by: Dave | last post by:
string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Which works fine, but I need to set and send a cookie with the WebRequest. How do I do that?
0
1314
by: Atul Goyal | last post by:
Hello I want to send Http request by using swing . Can you help me fot that. ?
0
9724
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
9604
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
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10394
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
9201
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
6882
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
5552
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
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.