Python automation Test (ix) URLLIB2 send HTTP Request

Source: Internet
Author: User
Tags urlencode

The URLLIB2 is a standard module that comes with Python and is used to send HTTP request. Similar to. NET, the HttpWebRequest class

Advantages of URLLIB2

The HTTP request sent by Python Urllib2 can be intercepted automatically by fiddler, which facilitates debugging.

Python can automatically process cookies

Disadvantages of URLLIB2

The header in the HTTP request sent by Python Urllib2 will be changed to "capitalize"

For example, your code is written in the header: Content-type=application/x-www-form-urlencoded, will be modified to content-type=application/ x-www-form-urlencoded

Instance one, get method, and custom header

#-*-Coding:utf-8-*-Importurllib2request= Urllib2. Request ("http://www.baidu.com/") Request.add_header ('Content-type','application/x-www-form-urlencoded') Response=Urllib2.urlopen (Request)PrintResponse.getcode ()PrintResponse.geturl ()PrintResponse.read ()

Example two, POST method

#-*-Coding:utf-8-*-ImportUrllib2Importurllibrequest= Urllib2. Request ("http://passport.cnblogs.com/login.aspx") Request.add_header ('Content-type','application/x-www-form-urlencoded') Data={"Tbusername":"Test_username","Tbpassword":"Test_password"}response=Urllib2.urlopen (Request, Urllib.urlencode (data))PrintResponse.getcode ()PrintResponse.geturl ()PrintResponse.read ()

Example three: processing of cookies

#-*-Coding:utf-8-*-ImportUrllib2ImportUrllibImportCOOKIELIBCJ=Cookielib. Cookiejar () opener=Urllib2.build_opener (urllib2. Httpcookieprocessor (CJ) Request= Urllib2. Request ("https://dynamic.12306.cn/otsweb/") Request.add_header ('Content-type','application/x-www-form-urlencoded') Data={"Tbusername":"Test_username","Tbpassword":"Test_password"}response=Opener.open (Request, Urllib.urlencode (data))#send again, you'll see cookies sent to Web serverResponse =Opener.open (Request, Urllib.urlencode (data))PrintResponse.getcode ()PrintResponse.geturl ()PrintResponse.read ()

Example four: How to handle jumps

When creating opener, Ul2. Httpredirecthandler is one of the handler that is added by default

Python automation Test (ix) URLLIB2 send HTTP Request

Related Article

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.