"Learning Notes" urllib2 and use of cookies

Source: Internet
Author: User

Because of some relationship, I need to discard the lovely requests library and then use URLLIB2, have to say, the function, or requests powerful Ah, cookie,session,put,option and so on,,,,

Well, back to the point, due to functional needs, I need to obtain a request after the cookie, not how to use URLLIB2, just this study, I use cookielib and urllib2 cooperation. Use Cookielib to create a file that stores cookies, and then read the contents of the cookie after initiating the request. The specific code is as follows:

import urllib2import cookielibheaders = {          ' user-agent ':  ' mozilla/5.0  (windows nt 6.1;  win64; x64; rv:53.0)  gecko/20100101 firefox/53.0 ',          ' Referer ':  ' http://www.baidu.com ',     }cj = cookielib. Cookiejar ()   opener = urllib2.build_opener (urllib2. Httpcookieprocessor (CJ) Urllib2.install_opener (opener) Urllib2.socket.setdefaulttimeout (Ten) target =  ' http://127.0.0.1/cms/phpcms/' Response = urllib2. Request (target, '/', headers)   #利用request发起带data和headers的请求 #data not required in this time, so use '/' instead of Opener.open (response) for  i in cj:    print i.name    print i.value        #循环读取, remove the cookie 

In fact, I want to write the script at the same time to launch three times to take cookies with a request for cookies, my ability is limited, really can not solve, give up, but read the cookie is still possible, hmm. And then we'll try harder.

This article is from the "hesitated" blog, make sure to keep this source http://chichu.blog.51cto.com/11287515/1973862

"Learning Notes" urllib2 and use of cookies

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.