Python HTTP client custom Cookie implementation instance, pythoncookie

Source: Internet
Author: User

Python HTTP client custom Cookie implementation instance, pythoncookie

Python HTTP client custom Cookie implementation instance

Almost all scripting languages provide convenient HTTP client processing functions, and Python is no exception. Using urllib and urllib2 can easily perform http get and POST operations. You can also add some handler in the form of a plug-in to customize the request and response. For example, the support for proxy and cookie are added in this way. Specifically, an opener is constructed as follows:

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

Then this opener can process cookies, which is quite convenient and customizable ...... Okay, in short, I want to insert some cookie values manually on the client, but no similar method is provided for both HTTPCookieProcessor and cookielib.

It seems that I did not have such a requirement, because I found the Patch submitted by someone for Python while searching for the solution, which is to add this function. However, it seems that it has not been accept, so it does not seem portable to do a brute force patch on the standard library. So I found another solution, which is actually quite simple: after reading the implementation code of HTTPCookieProcessor, I found that I could do something similar, that is, writing a handler, place the cookie value I want in the header of the request object.

So I checked the Python document and hardly described the handler interface, so I wrote it according to HTTPCookieProcessor. This handler should be placed behind the normal cookie processing handler, then check the existing cookie header, and then merge it. However, the strange thing is that the Python document does not find a method such as get_header in the Request object to get the value of an existing header item. It is very strange, so I directly checked the source code, this method does exist. Some people have heard about how Ruby's documents are done and how well Python documents are done. Although I didn't think Ruby's documents are very bad, but I also think the Python documentation is really good. I like Examples at the end of it most. The two document systems are different. Ruby documents are automatically generated by extracting (in a specific format) Comments from the code, similar to javadoc; python now uses a file system independent of the source code, which is manually written. However, in the end, even functions are missing. As a result, the disadvantages of manual maintenance documents are obvious. In fact, the document system I have seen should belong to Emacs/Elisp. However, 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, add the handler when constructing the opener:

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

But it is always a workaround. We hope that the patch will be added to the standard library.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.