当前位置:网站首页>Request module

Request module

2022-04-23 14:07:00 Fresh strawberries

request modular

Python A module based on network request of Zhongyuan .

effect : Simulate browser to send request

install

pip install requests

Send a request

r = requests.get('https://api.github.com/events')
r = requests.post('http://httpbin.org/post', data = {
    'key':'value'})
r = requests.put('http://httpbin.org/put', data = {
    'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')

Pass parameters

payload = {
    'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
#  You can also pass in a list as a value :
payload = {
    'key1': 'value1', 'key2': ['value2', 'value3']}

Response content

# Requests  Will automatically decode the content from the server . majority  unicode  Character sets can be decoded seamlessly .
r = requests.get('https://api.github.com/events')
r.text

#  View encoding 
r.encoding
#  Change coding 
r.encoding = 'ISO-8859-1'

##  Binary response content ( Access the request response body in bytes )
r.content

## json Response content ( Built in  JSON  decoder )
r.json()

Custom request header

#  Simply pass a  dict  to  headers  Parameters are OK .
url = 'https://api.github.com/some/endpoint'
headers = {
    'user-agent': 'my-app/0.0.1'}
r = requests.get(url, headers=headers)

Be careful : customized header The priority of is lower than that of some specific information sources , for example :

  • If in .netrc User authentication information is set in , Use headers= The set authorization will not take effect . And if you set it auth= Parameters ,.netrc The settings of are invalid .
  • If redirected to another host , to grant authorization header It will be deleted .
  • Agency authorization header Will be URL The proxy identity provided in overrides .
  • When we can judge the length of the content ,header Of Content-Length Will be rewritten .

More complicated POST request

You want to send some data encoded as a form , Simply pass a dictionary to data Parameters . Your data dictionary is automatically encoded as a form when you make a request :

payload = {
    'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)

#  You can also for  data  Parameter into a tuple list :
payload = (('key1', 'value1'), ('key1', 'value2'))

#  send out  json  Of  POST/PATCH  data :
url = 'https://api.github.com/some/endpoint'
payload = {
    'some': 'data'}
requests.post(url, data=json.dumps(payload))	# 2.4.2  edition :requests.post(url, json=payload)

Upload Multipart-Encoded The file of

url = 'http://httpbin.org/post'
files = {
    'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

#  You can explicitly set the file name , File types and request headers :
files = {
    'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {
    'Expires': '0'})}

Status response code

r = requests.get('http://httpbin.org/get')
r.status_code
#  Built in status code query object 
r.status_code == requests.codes.ok

#  Throw error request exception 
r.raise_for_status()

Response head

r.headers	#  View response headers 

##  View response header specific fields 
r.headers['Content-Type']	
r.headers.get('content-type')

Cookies

r.cookies['example_cookie_name']

#  send out cookies
url = 'http://httpbin.org/cookies'
cookies = dict(cookies_are='working')
r = requests.get(url, cookies=cookies)

# Cookie  The return object of is  RequestsCookieJar, It behaves like a dictionary , But the interface is more complete , It is suitable for cross domain and cross path use . You can also put  Cookie Jar  to  Requests  in :
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
r.text	-> '{"cookies": {"tasty_cookie": "yum"}}'

Advanced

Conversation object (Session)

Session objects allow you to hold certain parameters across requests .

s = requests.Session()

s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")

print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'

Sessions can also be used to provide default data for request methods . This is done by providing data for the properties of the session object :

s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({
    'x-test': 'true'})

# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={
    'x-test2': 'true'})

Stream upload

#  Just provide a class file object for your request body :
with open('massive-body') as f:
    requests.post('http://some.url/streamed', data=f)

Response body content workflow

#  By default , When you make a network request , The response will be downloaded immediately . You can go through  stream  Parameters , Delay downloading the response body , Until visit  Response.content  Properties to download :
r = requests.get(tarball_url, stream=True)

 You can further use  Response.iter_content  and  Response.iter_lines  Method to control the workflow , Or with  Response.raw  From the bottom  urllib3  Of  urllib3.HTTPResponse <urllib3.response.HTTPResponse  Read the non decoded response body .

 If you put... In your request  stream  Set to  True,Requests  Unable to release connection back to connection pool , Unless you   Consumed all the data , Or call  Response.close.  This will lead to the problem of inefficient connection . If you find yourself using  stream=True  At the same time, it also partially reads the request  body( Or no reading at all  body), Then you should consider using  with  Statement send request , This ensures that the request will be closed :
with requests.get('http://httpbin.org/get', stream=True) as r:
    #  Process the response here .
    pass

Event hook

#  You can pass a  {hook_name: callback_function}  Dictionary for  hooks  Request parameters assign a hook function to each request :
hooks=dict(response=callback_function)

callback_function  Will accept a data block as its first parameter .
def callback_function(r, *args, **kwargs):
    print(r.url)

Overtime (timeout)

requests By default, timeout processing will not be performed automatically .

#  this  timeout  The value will be used as  connect  and  read  Of the two  timeout.
r = requests.get('https://github.com', timeout=5)

#  If you want to make it separately , Just pass in a tuple :
r = requests.get('https://github.com', timeout=(3.05, 27))

Setting agent

import requests

proxies = {
    
  "http": "http://10.10.1.10:3128",
  "https": "http://10.10.1.10:1080",
}

requests.get("http://example.org", proxies=proxies)

#  You can also use environment variables  HTTP_PROXY  and  HTTPS_PROXY  To configure the agent .
$ export HTTP_PROXY="http://10.10.1.10:3128"
$ export HTTPS_PROXY="http://10.10.1.10:1080"

#  If your agent needs to use HTTP Basic Auth, have access to  http://user:password@host/  grammar :
proxies = {
    
    "http": "http://user:[email protected]:3128/",
}

#  To set up a proxy for a specific connection method or host , Use  scheme://hostname  As  key,  It will match the specified host and connection mode .
proxies = {
    'http://10.20.1.128': 'http://10.10.1.10:5323'}

版权声明
本文为[Fresh strawberries]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231400483373.html