Python interface Automation Test two: Common operations

Source: Internet
Author: User

url = ' interface address '

r = Requests.get (URL) # send GET request

Print (R.status_code) # Prints the status code, if redirected, returns the code after the redirect

Print (r.headers) # Prints the returned header (header)

Print (r.text) # View the textual form of the returned results

R.status_code # Response Status code

R.content # byte-way response body, will automatically decode for you gzip and deflate compression

R.headers # Stores the server response header in a Dictionary object, but this dictionary is special, the dictionary key is not case-sensitive, and if the key does not exist return none

R.json () # Requests built-in JSON decoder, JSON converted to Python dictionary

R.url # If there is no redirect, that is the URL of the request, if there is a redirect, that is the redirected URL
R.encoding # View the encoded format returned
R.cookies # Get Cookies
R.raw #返回原始响应体

R.history #追踪重定向过程 (list type)
R.text #字符串方式的响应体 is automatically decoded based on the character encoding of the response header

R.content.decode (' Utf-8 ') # returns content that is garbled when printed with this method
R.raise_for_status () #失败请求 (not 200 responses) throws an exception

Logincookies = r.cookies: # Get logged in cookies

s = requests.session () # can use this method to keep the session, not every time to get cookies (only applicable to the website is a cookie, the site is token of useless)

Print (r.encoding) # View the encoded format returned

R.json # Gets the JSON returned

Verify=false # When you access an HTTPS request plus do not validate the certificate

# Open Excel file, save file as suffix xls

fp = open (' Yoyo.xls ', ' WB ') # W: write, B: Binary form

Python interface Automation Test two: Common operations

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.