Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.06; 'urllib2': 0.07; 'cookie': 0.09; 'extracted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:2.7': 0.09; 'subject:How': 0.10; 'stored': 0.12; 'analysing': 0.16; 'before.': 0.16; 'cookies': 0.16; 'handling,': 0.16; 'process?': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'request)': 0.16; 'subject:making': 0.16; 'subject:requests': 0.16; 'subject:when': 0.16; 'work,': 0.20; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'adds': 0.24; 'headers': 0.24; 'why.': 0.24; 'right.': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; "doesn't": 0.30; 'requests': 0.31; 'towards': 0.31; 'writes:': 0.31; 'subject: (': 0.35; "can't": 0.35; 'problem.': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'prepare': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'really': 0.36; 'doing': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'problems': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'new': 0.61; 'first': 0.61; 'save': 0.62; 'received:217': 0.63; 'address': 0.63; 'provide': 0.64; 'here': 0.66; 'hints': 0.68; 'containing': 0.69; 'receive': 0.70; 'respect': 0.70; 'evaluate': 0.72; 'responses': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: How to keep cookies when making http requests (Python 2.7) Date: Wed, 28 Aug 2013 06:52:37 +0200 References: <7e79a9b4-0bf9-4756-afd4-3bc127360b95@googlegroups.com> <8266faf3-892b-4e49-9b38-87f0030250fc@googlegroups.com> <5970a1d3-ec1b-4892-b53a-907de332ecaa@googlegroups.com> <62a5fb2d-b4d0-4de4-a0d8-a7ee4dbb1b90@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e0848a.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:Oi768Ey/WTmk/s9C7y07WUYsR1A= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377665572 news.xs4all.nl 15915 [2001:888:2000:d::a6]:39545 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:53109 Luca Cerone writes: > ... > Ok so after reading the documentation for urllib2 and cookielib I came up with the following code: > > #START > from urllib2 import urlopen , Request > from cookielib import CookieJar > import re > regex = re.compile(r'\{(.*)\}') > > base_url = "http://quiz.gambitresearch.com" > job_url = base_url + "/job/" > > cookies = CookieJar() > r = Request(base_url) #prepare the request object > cookies.add_cookie_header(r) #allow to have cookies > R = urlopen(r) #read the url > cookies.extract_cookies(R,r) #take the cookies from the response R and adds #them to the request object "adds them to the request object" should be "adds them to the cookie jar". > #build the new url > t = R.read() > v = str(eval(regex.findall(t)[0])) > job_url = job_url + v > > > # Here I create a new request to the url containing the email address > r2 = Request(job_url) > cookies.add_cookie_header(r2) #I prepare the request for cookies adding the cookies that I extracted before. > > #perform the request and print the page > R2 = urlopen(r2) > t2 = R2.read() > print job_url > print t2 > #END > > This still doesn't work, but I really can't understand why. > As far as I have understood first I have to instantiate a Request object > and allow it to receive and set cookies (I do this with r = Request() and cookies.add_cookie_header(r)) > Next I perform the request (urlopen), save the cookies in the CookieJar (cookies.extract_cookies(R,r)). > > I evaluate the new address and I create a new Request for it (r2 = Request) > I add the cookies stored in the cookiejar in my new request (cookies.add_cookie_header(r2)) > Then I perform the request (R2 = urlopen(r2)) and read the page (t2 = R2.read()) > > What am I doing wrong? With respect to cookie handling, you do everything right. There may be other problems with the (wider) process. Analysing the responses of your requests (reading the status codes, the response headers and the response bodies) may provide hints towards the problem. >Do I misunderstand something in the process? Not with respect to cookie handling.