Python interface Automation Test two: Python Code implementation Interface test

Source: Internet
Author: User
Tags save file

url = ' interface address '

r = Requests.get (URL) #发送get请求

Print (R.status_code) #打印状态码, if redirected, returns the code after redirection

Print (r.headers) #打印返回的报头 (head)

Print (R.text) #查看返回结果的文本形式

R.status_code #响应状态码

R.content #字节方式的响应体 that will automatically decode for you gzip and deflate compression

R.headers #以字典对象存储服务器响应头, but this dictionary is very special, the dictionary key is not case-sensitive, if the key does not exist return none

R.json () #Requests中内置的JSON解码器, JSON turns into a 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

Cookies=logincookies: # Incoming request for cookies

s = requests.session () # a miniature browser that can be understood as code, the advantage of doing so is that cookies can be saved without having to get it every time (only applies to websites that are cookies, and the site is token 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

Remove warning Warning:

Import URLLIB3

Urllib3.disable_warnings ()

First, HTTP:

GET Request:

1. Get request (no parameters):

2. Get request (with parameters):

Interface Address: HTTP://JAPI.JUHE.CN/QQEVALUATE/QQ

Return format: JSON

Request method: Get Post

Sample request: Http://japi.juhe.cn/qqevaluate/qq?key= the appkey&qq=295424589 you requested

Interface Note: According to the parameters of the QQ number and the AppKey you applied to test QQ inauspiciousness

Request parameter description (enter parameter):

Name Required type description

Key is the string you requested for the APPKEY:8DBEE1FCD8627FB6699BCE7B986ADC45

QQ is the string needs to test QQ number: 907728701

2.1, the URL of the way to pass the parameter:

url = ' http://japi.juhe.cn/qqevaluate/qq?key= 8dbee1fcd8627fb6699bce7b986adc45&qq=907728701 '

2.2, in the form of a dictionary to pass the parameter:

url = ' HTTP://JAPI.JUHE.CN/QQEVALUATE/QQ '

Par = {

' Key ': ' 8dbee1fcd8627fb6699bce7b986adc45 ',

' QQ ': ' 907728701 '

}

R = Requests.get (Url,params=par) #发送get请求

POST request:

1. Post request (no parameters):

url = ' HTTP://JAPI.JUHE.CN/QQEVALUATE/QQ '

r = Requests.post (URL) #发送post请求

Print (R.status_code) #打印状态码

Print (r.headers) #打印返回的报头 (head)

Print (R.text) #查看返回结果的文本形式 (body part)

2. Post request (with parameters):

2.1, the URL of the way to pass the parameter:

url = ' http://japi.juhe.cn/qqevaluate/qq?key= 8dbee1fcd8627fb6699bce7b986adc45&qq=907728701 '

r = Requests.post (URL) #发送post请求

2.2, in the form of a dictionary to pass the parameter:

url = ' HTTP://JAPI.JUHE.CN/QQEVALUATE/QQ '

Par = {

' Key ': ' 8dbee1fcd8627fb6699bce7b986adc45 ',

' QQ ': ' 907728701 '

}

R = Requests.post (Url,params=par) #发送get请求

Second, HTTPS:

1. Get:

url = ' HTTPS://WWW.JUHE.CN/DOCS/API/ID/39 '

r = Requests.get (URL) #发送get请求

Print (R.status_code) #打印状态码

Print (r.headers) #打印返回的报头 (head)

Print (R.text) #查看返回结果的文本形式 (body part)

Sslerror: Certificate Issue:

Method 1: Check if the Faddler is closed and the access is successful when

Method 2: The request parameter is followed by the Verify=false

Verify defaults to True, at which time the certificate is validated and false will not validate the certificate

have a Body section:

content-type:application/x-www-form-urlencoded; Charset=utf-8: Data transfer

Content-type:application/json: Transmitting JSON

Parse the returned content into JSON format:

Prerequisite: A certain content is in JSON format

If garbled::

Output in the content byte stream, decoded into Utf-8:

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

Remove warning Warning:

Import URLLIB3

Urllib3.disable_warnings ()

Error handling:

403: Denied or forbidden access: Must disguise the head (header details according to the interface document)

1, the server identified as code access:

1.1. Code Access header: User-agent for Python

1.2. Browser access to the head: User-agent for the browser

1.3. Add User-agent to the head:

2, Camouflage head still 403: Server check Cookic (Cookic has timeliness)

2.1. No Cookic during code access

2.2. Browser Access with Cookic

2.3. Add Cookic access to the header successfully

Python interface Automation Test two: Python Code implementation Interface test

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.