Objective
Requests module, that is, old dirty turtle, why call it old dirty turtle, because this official online logo is this dirty turtle, the next is to learn it.
First, environmental installation
1. Install the requests module with PIP
>>PIP Install Requests
Second, GET request
1. After importing requests, the URL address can be accessed directly using the Get method, such as: Http://www.cnblogs.com/wanghaihong200/, it looks cool.
2. Here r is response, the return value after the request, you can call response in the Status_code method to view the status code
3. Status code 200 can only indicate that the server address accessed by this interface is right, and does not explain the function OK, generally to see the content of the response, R.text is the return text information (compression format needs to use r.content)
The content method outputs all Unicode encodings
If you need to develop an encoding format, you can use r.encoding = "Utf-8"
Third, params
1. Send another GET request with parameters, such as in the blog Park search: wanghaihong200, url address: http://zzk.cnblogs.com/s/blogpost?Keywords=wanghaihong200
2. Request parameter: Keywords=yoyoketang, can be passed as a dictionary: {"Keywords": "Wanghaihong200"}
3. Multiple parameter formats: {"Key1": "Value1", "Key2": "value2", "Key3": "Value3"}
Iv. Content
1. Baidu Homepage If using R.text will find the content has garbled, because Baidu home response content is gzip compressed (non-text text)
2. If the Fiddler tool is garbled, it can be decoded after the click, in the code can be used r.content This method, content will automatically decode gzip and deflate compression
Wu, response
Return content for 1.response and more
--R.status_code #响应状态码
--R.content #字节方式的响应体, 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解码器
--R.url # Get URL
--r.encoding # encoding format
--R.cookies # Get Cookie
--R.raw #返回原始响应体
--R.text #字符串方式的响应体, automatically decoded according to the character encoding of the response head
--R.raise_for_status () #失败请求 (not 200 responses) throws an exception
Postscript:
For small partners who want to test the interface with Python, personal advice is to read through requests's official documentation: http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
After the basic knowledge is written, we will provide some ideas of the interface testing framework and the simple application of the Jenkins running time task.
Python interface automation 1-Send a GET request