The Python HTTP client customizes the Cookie implementation instance

  • 2020-05-30 20:31:42
  • OfStack

The Python HTTP client customizes the Cookie implementation instance

Almost all scripting languages offer the convenience of HTTP client handling, and Python is no exception. urllib and urllib2 make it easy to do HTTP GET and POST, among other things. It also allows you to customize request and response by adding some handler in a plugin-like fashion, such as proxy support and cookie support. Specifically, an opener is constructed as follows:


opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())

Then this opener can handle cookie, which is quite convenient and customizable... Well, anyway, now I want to be able to manually insert some cookie values into the client, but neither HTTPCookieProcessor nor CookieJar in cookielib provide a similar way to do this.

It seems that I am not alone in this requirement, because when I was looking for the solution, I also found the Patch submitted by someone to Python, which is to add this function. It doesn't look like it's been accept yet, and the portability of the patch to the standard library doesn't look good either. So I found another solution, which was actually quite simple: after looking at the HTTPCookieProcessor implementation code, I found that I could do something similar, namely write an handler and force the cookie value I wanted into the header of the request object.

So I looked up Python's documentation, and there seemed to be little description of handler's interface, so I wrote HTTPCookieProcessor. This handler should be placed behind the normal cookie processing handler, then check the existing cookie header, and merge again 1. However, the weird thing is that there is no Request object found in the Python document. There is a method like get_header that can get the value of the existing header item. I think it is very weird, so I directly checked the source code and found that there is a method. I have heard people say how bad Ruby's documents are and how good Python's documents are. Although I don't think Ruby's documents are bad, I also think Python's documents are really good. I like Examples at the end most. The two document systems go a different way. Ruby's document is automatically generated by extracting (formatted) comments from code, similar to javadoc's; However, Python now USES a document system independent of the source code, which is written manually. However, in the end, even the functions are left out, so it can be seen that the disadvantages of manual maintenance of documents are very obvious. In fact, the best document system I have ever seen belongs to Emacs/Elisp. But without further ado, handler is as follows:


class SimpleCookieHandler(urllib2.BaseHandler):
 def http_request(self, req):
  simple_cookie = 'cc98Simple=1'
  if not req.has_header('Cookie'):
   req.add_unredirected_header('Cookie', simple_cookie)
  else:
   cookie = req.get_header('Cookie')
   req.add_unredirected_header('Cookie', simple_cookie + '; ' + cookie)
  return req

Then, when constructing opener, add this handler:


opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(), 
SimpleCookieHandler())

But it's always going to be an workaround, so expect that patch to be added to the standard library.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: