How to extract a specific key from HTTP cookies in Python?

Trying to get a specific value from the cookie string of an HTTP request in Python. I believe using a requests library or urllib2 would be a good idea.

Example:

Suppose

 headers['cookie'] = 'somekey=somevalue;someotherkey=someothervalue' 

Trying to get somekey value.

Thank you very much!

+4
source share
1 answer

Cookies may be a little% * $%. Every time I tried to use them, there is always some tiny thing that I am doing wrong, and they do not work. It is best to use a wrapper library.

Using WSGI, you can use the python cookie library:

 import Cookie # The function that receives the request def application(environ, start_response): cookie = Cookie.SimpleCookie() cookie.load(environ['HTTP_COOKIE']) some_value = cookie['some_key'].value 
+8
source

Source: https://habr.com/ru/post/1484102/


All Articles