当前位置:网站首页>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
.netrcUser authentication information is set in , Use headers= The set authorization will not take effect . And if you set itauth=Parameters ,.netrcThe 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
边栏推荐
- 基于微信小程序的wifi模块使用
- Wechat applet communicates with esp8266 based on UDP protocol
- 接口文档yaml
- leetcode--380. O (1) time insertion, deletion and acquisition of random elements
- Chrome插件 之 Selenium IDE、XPath 安装
- websocket
- 微信小程序通过低功耗蓝牙设备进行定位及测距(二)
- PyMySQL
- 帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)
- win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
猜你喜欢

RobotFramework 之 用例标签机制
poi操作word模板替换数据并且导出word

烟雾传感器(mq-2)使用详细教程(基于树莓派3b+实现)

基于CM管理的CDH集群集成Phoenix

室内外地图切换(室内基于ibeacons三点定位)

浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证

DDT+Excel进行接口测试

帆软分割求解:一段字符串,只取其中某个字符(所需要的字段)

Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)

1256: bouquet for algenon
随机推荐
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
BUG_me
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
Check in system based on ibeacons
visio安装报错 1:1935 2:{XXXXXXXX...
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
生成随机高质量符合高斯分布的随机数
Chrome插件 之 Selenium IDE、XPath 安装
分页SQL
request模块
leetcode--357. Count the number of different figures
Idea控制台乱码解决
Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
使用Postman进行Mock测试
1256: bouquet for algenon
01-NIO基础之ByteBuffer和FileChannel
微信小程序通过低功耗蓝牙设备进行定位及测距(二)
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
leetcode--380. O (1) time insertion, deletion and acquisition of random elements