Python: urllib2 how to send a cookie with urlopen request

I am trying to use urllib2 to open a url and send a specific cookie to the server. For example. I want to open a website Solve problems with chess , with a specific cookie, for example. search = 1. How to do it?

I am trying to do the following:

import urllib2 (need to add cookie to the request somehow) urllib2.urlopen("http://chess-problems.prg") 

Thank you in advance

+73
python urllib2
Jul 26 2018-10-12T00:
source share
4 answers

A cookie is another HTTP header.

 import urllib2 opener = urllib2.build_opener() opener.addheaders.append(('Cookie', 'cookiename=cookievalue')) f = opener.open("http://example.com/") 

See urllib2 examples for other ways to add HTTP headers to your request.

There are more ways to handle cookies. Some modules, such as cookielib , try to behave like a web browser - remember which cookies you received earlier, and automatically send them again in the following requests.

+99
Jul 26 2018-10-12T00:
source share

Maybe using cookielib.CookieJar can help you. For example, when sending to a page containing a form:

 import urllib2 import urllib from cookielib import CookieJar cj = CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) # input-type values from the html form formdata = { "username" : username, "password": password, "form-id" : "1234" } data_encoded = urllib.urlencode(formdata) response = opener.open("https://page.com/login.php", data_encoded) content = response.read() 

EDIT:

After Peter's comment, I thought a little. From the docs:

The CookieJar class stores HTTP cookies. It extracts cookies from HTTP requests and returns them in HTTP responses. Examples of using CookieJar, if necessary, the use of cookies automatically ends. Subclasses are also responsible for storing and retrieving cookies from a file or database.

This way, any requests you make with your CookieJar instance will be processed automatically. Kinda, how is your browser ::

I can only speak from my own experience, and my 99% use of cookies is to get a cookie and then send it with all subsequent requests in this session. The code above only handles this, and it does it transparently.

+54
Nov 21 2018-11-11T00:
source share

You can take a look at the excellent Python HTTP library called Requests . This makes every HTTP task a little easier than urllib2. From the Cookies section of the Quick Start Guide:

To send your own cookies to the server, you can use the cookie parameter:

 >>> cookies = dict(cookies_are='working') >>> r = requests.get('http://httpbin.org/cookies', cookies=cookies) >>> r.text '{"cookies": {"cookies_are": "working"}}' 
+13
Jun 02 2018-12-12T00:
source share

Use cookielib . The linked links page contains examples. You will also find a tutorial here .

+5
Jul 26 2018-10-12T00:
source share



All Articles