After the environment is set up, let's look at some simple uses of requests, including:
- Requests commonly used request methods, including: Get,post
- Use of session and cookie in requests library
- Other advanced sections: Authentication, proxy, certificate validation, timeout configuration, error handling, and more.
This section begins with a look at how to send a GET request in the requests library:
First, look at the method definition:
1, to the official document went to the next requests.get () method definition, as follows:
2, click "Source" in the upper right corner, to see its source code as follows:
See the last line Return,get method is finally implemented by calling the Requests.request method, in fact, in other methods such as Post,put,head,delete request method, such as the invocation method, The type of the requested method is then passed to the request method's first parameter.
3, the HTTP protocol is a request/response mode-based, stateless, application-level protocol. Since there is a request, there is a response, to see the response information commonly used in Resquest:
Second, the Get method is simple to use:
1. Get with no parameters:
#-*-Coding:utf-8-*-#不带参数的getimport requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([Host, Endpoint]) R = requests.get (URL) #response = R.json () print type (r.text) print (eval (r.text))
Output:
{ ' origin ': ' 183.14.133.88 ', ' headers ': { ' Connection ': ' Close ', ' Host ': ' httpbin.org ', ' Accept-encoding ': ' gzip, deflate ', ' Accept ': ' */* ', ' user-agent ': ' python-requests/2.18.1 ' }, ' args ': { }, ' url ': ' http://httpbin.org/get '}
2. Get with parameters:
#-*-Coding:utf-8-*-#带参数的getimport requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([host,e Ndpoint]) params = {"Show_env": "1"}r = Requests.get (url=url,params=params) Print R.url
Output:
http://httpbin.org/get?show_env=1{ ' origin ': ' 183.14.133.88 ', ' headers ': { ' X-request-id ': ' Ebe922b4-c463-4fe9-9faf-49748d682fd7 ', ' accept-encoding ': ' gzip, deflate ', ' x-forwarded-port ': ' 80 ', ' total-route-time ': ' 0 ', ' Connection ': ' Close ', ' connect-time ': ' 0 ', ' Via ': ' 1.1vegur ', ' X-forwarded-for ': ' 183.14.133.88 ', ' Accept ': ' */* ', ' user-agent ': ' python-requests/2.18.1 ', ' X-request-start ': ' 1504755961007 ', ' Host ': ' httpbin.org ', ' x-forwarded-proto ': ' http ' }, ' Args ': { ' show_env ': ' 1 ' }, ' url ': ' http://httpbin.org/get?show_env=1 '}
3. Get with Header:
#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([Host,endpoint] ) headers = {"User-agent": "Test request Headers"}r = Requests.get (URL) r = Requests.get (url,headers=headers) #response = R.json () Print (eval (r.text)) [' Headers '] [' user-agent ']
Output:
Test request Headers
4, with parameters and header:
#-*-Coding:utf-8-*-import requestsimport jsonhost = "http://httpbin.org/" endpoint = "get" url = '. Join ([Host,endpoint] ) headers = {"User-agent": "Test request Headers"}params = {"Show_env": "1"}r = Requests.get (URL) r = requests.get (URL, Headers=headers,params=params) #response = R.json () print (eval (r.text)) [' Headers '] [' user-agent ']print r.url
Output:
Test Request Headershttp://httpbin.org/get?show_env=1
Python Interface Automation Test II (REQUEST.GET)