Python implements interface testing

Source: Internet
Author: User

There are many types of interfaces, but we often meet the two kinds of get and post that we often use. What is the difference between the two? Personal understanding is mainly expressed in terms of safety.

Python code post arbitrary HTTP data and the method of using cookies, the need for friends can refer to.

1), when not using cookies, it is very simple to send an HTTP post:

Import URLLIB2, Urllib data = {' name ': ' www ', ' password ': ' 123456 '}f = urllib2.urlopen (        url     = ' Http://www.haibia n.com/',        data    = Urllib.urlencode (data)        ) print F.read ()

2), when using cookies, the code becomes somewhat more complex:

Import Urllib2 cookies = Urllib2. Httpcookieprocessor () opener = Urllib2.build_opener (cookies) F = opener.open (' http://www.haibian.com ') data = ' <root >Hello</root> ' request = Urllib2. Request (        url     = ' http://www.haibian.com/index/ajax_login ',        headers = {' Content-type ': ' Text/xml '},        Data    = data) Opener.open (Request)

Annotations:
The first open () is to log in. The cookie returned by the server is automatically saved in the cookie and is used for subsequent requests.
The second open () sends the CONTENT-TYPE=TEXT/XML data to the server using the POST method. If you do not create a Request, but instead use the Urlopen () method directly, Python forces the Content-type to be changed to application/x-www-form-urlencoded.

Python implements interface testing

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.