Python Requests Library learning notes (top)

Source: Internet
Author: User
Tags urlencode

Respect the original spirit of the blog Park, please do not reprint!

Requests Library Official user's manual address: http://www.python-requests.org/en/master/; Chinese user's manual address: http://cn.python-requests.org/zh_CN/latest/;

Requests Library author Kenneth Reitz personal homepage: https://www.kennethreitz.org/;

Requests Library GitHub address: https://github.com/requests/requests;

Requests Library Download method: Pip Install requests

Learning Purpose: The Python+requests library realizes the interface Automation test;

Requests Library author Kenneth Reitz created server side: http://httpbin.org/, can be used when learning.

Python's own interface test library urllib, Urllib2, URLLIB3, these three libraries are not advanced relationships, are independent of each other. The Requests library uses URLLIB3 (multiple requests to reuse a socket, consuming fewer resources).

1. A small program implemented using Urllib and URLLIB2:

#-*-Coding:utf-8-*-
Import Urllib
Import Urllib2#引入urllib, URLLIB2 library

url_ip = ' HTTP://HTTPBIN.ORG/IP '
Url_get = ' Http://httpbin.org/get '

Def use_simple_urllib2 ():
Response = Urllib2.urlopen (URL_IP)#urlopen () methods for accessing URLs
print ' >>>>response Headers: '
Print Response.info ()#info () Ways to print headers
print ' >>>>response body: '
print '. Join ([line to line in Response.readlines ()])#join () creates a new string from the element in the response body with "join"Str= "-";Seq= (A, "B", C); Span class= "PLN" >< Span class= "pun" >< span class= "str" >< Span class= "com" >print str< Span class= "pun". join ( seq a-b-c

                                                                                                                                                                                                
Def use_params_urllib2 ():
#构建请求参数

params = Urllib.urlencode ({' param1 ': ' Hello ', ' param2 ': ' World ') #urlencode () URL-encode parameters
#发送请求

Response = Urllib2.urlopen ('? '). Join ([Url_get, '%s '])% params)
#处理响应

print ' >>>>response Headers: '
Print Response.info ()
print ' >>>>status Code: '
Print Response.getcode () #getcode () method to get status code
print ' >>>>request body: '
print '. Join ([line to line in Response.readlines ()])

if __name__ = = ' __main__ ':
print ' >>>use simple urllib2: '
USE_SIMPLE_URLLIB2 ()
print '
print ' >>>use params urllib2: '
USE_PARAMS_URLLIB2 ()

Data returned by the server:

C:\Python27\python.exe c:/users/lxz/desktop/study/androidappshizhandaima/httpapi/jiekouceshi.py
>>>use Simple urllib2:
>>>>response Headers:
Connection:close#可以看到, the status of connection after a request is close, indicating that the Urllib library has to reopen a socket every time
server:meinheld/0.6.1
Date:fri, 06:25:44 GMT
Content-type:application/json
Access-control-allow-origin: *
Access-control-allow-credentials:true
X-powered-by:flask
x-processed-time:0.000429153442383
Content-length:32
via:1.1 Vegur

>>>>response Body:
{
"Origin": "39.109.125.70"
}

>>>use params urllib2:
>>>>response Headers:
Connection:close
server:meinheld/0.6.1
Date:fri, 06:25:44 GMT
Content-type:application/json
Access-control-allow-origin: *
Access-control-allow-credentials:true
X-powered-by:flask
x-processed-time:0.000815868377686
content-length:309
via:1.1 Vegur

>>>>status Code:
200
>>>>request Body:
{
"Args": {
"param1": "Hello",
"Param2": "World"
},
"Headers": {
"Accept-encoding": "Identity",
"Connection": "Close",
"Host": "httpbin.org",
" user-agent": "python-urllib/2.7 "
},
"Origin": "39.109.125.70",
"url": "Http://httpbin.org/get?param2=world&param1=hello"
}

2. A small program implemented using requests:

#coding =utf-8
Import requests#引入requests库

url_ip = ' HTTP://HTTPBIN.ORG/IP '
Url_get = ' Http://httpbin.org/get '

Def use_simple_requests ():
Response = Requests.get (URL_IP)#以get方法访问url
print ' >>>>response Headers: '
Print Response.headers#. Headers Get Headers
print ' >>>>response body: '
Print Response.text#. Text Gets the value

Def use_params_requests ():
params = {' param1 ': ' Hello ', ' param2 ': ' World '}#参数直接以字典的形式赋值, no coding required
#发送请求
Response = Requests.get (Url_get, Params=params)#get方法会自动连接url和参数
#处理响应
print ' >>>>response Headers: '
Print Response.headers
print ' >>>>status Code: '
Print Response.status_code#. Status_code Get Status_code Method
print ' >>>>reason: '
Print Response.reason#. Reason get access interface result method
print ' >>>>request body: '
Print Response.text#. Text Gets the value

if __name__ = = ' __main__ ':
print ' >>>use simple requests: '
Use_simple_requests ()
print '
print ' >>>use params requests: '
Use_params_requests ()

Data returned by the server:

>>>use Simple requests:
>>>>response Headers:
{' content-length ': ' 34 ', ' X-processed-time ': ' 0.000436067581177 ', ' x-powered-by ': ' Flask ', ' Server ': ' meinheld/0.6.1 ', ' Connection ': ' Keep-alive ', ' Via ': ' 1.1 Vegur ', ' access-control-allow-credentials ': ' true ', ' Date ': ' Sat, ' 08:12:17 GMT ', ' Acc Ess-control-allow-origin ': ' * ', ' content-type ': ' Application/json '}
>>>>response body:
{
  "origin": "111.204.108.132"
}

>>>use params requests:
>>>>response Headers :
{' content-length ': ' 343 ', ' x-processed-time ': ' 0.000698089599609 ', ' x-powered-by ': ' Flask ', ' Server ': ' meinheld/ 0.6.1 ', ' Connection ': ' keep-alive ', ' Via ': ' 1.1 Vegur ', ' access-control-allow-credentials ': ' True ', ' Date ': ' Sat, 08:12:18 GMT ', ' access-control-allow-origin ': ' * ', ' content-type ': ' Application/json '}

#可以看到使用requests库, the state of connection is keep-alive, which explains the multiple requests to reuse a socket, so the requests library consumes less resources than the Urllib library

>>>>status Code:
200
>>>>reason:
Ok
>>>>request Body:
{
"Args": {
"param1": "Hello",
"Param2": "World"
},
"Headers": {
"Accept": "*/*",
"Accept-encoding": "gzip, deflate",
"Connection": "Close",
"Host": "httpbin.org",
" user-agent": "python-requests/2.18.3 "
},
"Origin": "111.204.108.132",
"url": "Http://httpbin.org/get?param2=world&param1=hello"
}

3. Sending the request

Request Method:

Ways to send requests using the Requests library:requests.[ Method] (URL)

Patch: Updating resources , implemented by submitting JSON data, is more lightweight compared to put,patch.

How to use public APIs on GitHub address:https://developer.github.com/v3/

Call URL when using:https://api.github.com #可以用来日常学习使用

The following demo will use the above two domain names in code.

3.1, get the user name method https://developer.github.com/v3/users

Where https://api.github.com is the root domain name ,/users/username is endpoint.

Sample of the GitHub official web return data:

Implementation code:

#-*-Coding:utf-8-*-
Import JSON
Import requests

URL = ' https://api.github.com '

def build_uri (endpoint):
Return '/'. Join ([URL, endpoint])

def better_print (JSON_STR):
Return Json.dumps (Json.loads (JSON_STR), indent=4)

Def request_method ():
Response = Requests.get (Build_uri (' Users/caolanmiao '))
Print Better_print (response.text)

if __name__ = = ' __main__ ':
Request_method ()

The returned data:

{
"Public_repos": 0,
"Site_admin": false,
"Subscriptions_url": "Https://api.github.com/users/caolanmiao/subscriptions",
"gravatar_id": "",
"hireable": null,
"id": 22490616,
"Followers_url": "Https://api.github.com/users/caolanmiao/followers",
"Following_url": "Https://api.github.com/users/caolanmiao/following{/other_user}",
"Blog": "",
"Followers": 0,
"Location": "Pecking",
"Type": "User",
"Email": null,
"Bio": "Software QA Engineer",
"Gists_url": "https://api.github.com/users/caolanmiao/gists{/gist_id}",
"Company": null,
"Events_url": "Https://api.github.com/users/caolanmiao/events{/privacy}",
"Html_url": "Https://github.com/caolanmiao",
"Updated_at": "2017-08-19t09:27:39z",
"Received_events_url": "Https://api.github.com/users/caolanmiao/received_events",
"Starred_url": "Https://api.github.com/users/caolanmiao/starred{/owner}{/repo}",
"Public_gists": 0,
"name": "Yannan.jia",
"Organizations_url": "Https://api.github.com/users/caolanmiao/orgs",
"url": "Https://api.github.com/users/caolanmiao",
"Created_at": "2016-09-28t06:00:27z",
"Avatar_url": "Https://avatars0.githubusercontent.com/u/22490616?v=4",
"Repos_url": "Https://api.github.com/users/caolanmiao/repos",
"Following": 1,
"Login": "Caolanmiao"
}
The returned data information is themy own GitHub account, stating that this requestit worked.

Note: For GitHub, the pass -through parameter auth= (' username ', ' password ')can be authenticated.

Access the User/emails API, modify the following sections of the above code, add auth parameters, complete the certification

Response = Requests.get (Build_uri (' user/emails '), auth= (' Caolanmiao ', ' ######## '))

Return Data:

[{"Email": "jia##### @outlook. com", "PRIMARY": true, "verified": true, "visibility": "Public"}]
[
{
"Verified": true,
"Email": "jia##### @outlook. com",
"Visibility": "Public",
"PRIMARY": True
}
]
Complies with API usage instructions.

3.2, request with parameters

Get method: This parameter directly stitching the parameter after the URL of the advantages of the method of submission: Information delivery/page to page to facilitate the jump; disadvantage: Clear text, poor security, browser to the length of the URL is limited.

Post mode: strong security, can pass a large number of parameters.

1.get method for parameter passing, test with API address:https://developer.github.com/v3/users

The Since parameter filters the users before it (for example, since is 11, so only users after 11 are shown)

Implementation code:

#-*-Coding:utf-8-*-
Import JSON
Import requests
From requests import exceptions

URL = ' https://api.github.com '

def build_uri (endpoint):
Return '/'. Join ([URL, endpoint])

def better_print (JSON_STR):
Return Json.dumps (Json.loads (JSON_STR), indent=4)

Def params_request ():
Response = requests. Get (Build_uri (' users '), params={' since ': 11})
Print Better_print (response.text)
Print Response.request.headers
Print Response.url

if __name__ = = ' __main__ ':
Params_request ()

2.post, Patch way to implement parameter passing, test with API address: https://developer.github.com/v3/users/emails/

Implementation code:

Def json_request ():
Response = requests. Patch (Build_uri (' user '), auth= (' Caolanmiao ', ' ######## '), json={' name ': ' Yannan.jia ', ' email ': ' [email protected] ‘})
Response = requests. Post (Build_uri (' user/emails '), auth= (' Caolanmiao ', ' ######## '), json=[' [email protected] ')
Print Better_print (response.text)
Print Response.request.headers
Print Response.request.body
Print Response.status_code

Modify the name and mailbox through the patch method, and add the mailbox via post;

See the students can also use the API on GitHub to try it yourself.

Respect the original spirit of the blog Park, please do not reprint!

Python Requests Library learning notes (top)

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.