PycURL简介
作者:互联网
PycURL简介
编辑日期:2021-07-04
代码验证环境:python 3.7.10 +
操作系统:MACOS 11.4
鉴于cURL是一个非常强大的网络请求工具,习惯用cURL的同学也可以用PyCURL这个工具。
获取网络资源
需要注意PycURL返回的响应消息是字节流,如果解析文本的话,需要解码。
import pycurl
try:
# python 3
from io import BytesIO
except ImportError:
# python 2
from StringIO import StringIO as BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://httpbin.org/')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue()
# Body is a byte string.
# We have to know the encoding in order to print it to a text file
# such as standard output.
print(body.decode('utf-8'))
获取响应信息
import pycurl
try:
from io import BytesIO
except ImportError:
from StringIO import StringIO as BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://pycurl.io/')
c.setopt(c.WRITEDATA, buffer)
c.perform()
# HTTP response code, e.g. 200.
print('Status: %d' % c.getinfo(c.RESPONSE_CODE))
# Elapsed time for the transfer.
print('Time: %f' % c.getinfo(c.TOTAL_TIME))
# getinfo must be called before close.
c.close()
更多响应信息点击这里。
表单数据
使用POSTFIELDS
发送表单数据,表单数据需要提前URL序列化。
import pycurl
try:
# python 3
from urllib.parse import urlencode
except ImportError:
# python 2
from urllib import urlencode
try:
# python 3
from io import BytesIO
except ImportError:
# python 2
from StringIO import StringIO as BytesIO
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://httpbin.org/post')
post_data = {'params': 'this is value','params1':'hello world'}
# Form data must be provided already urlencoded.
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.
c.setopt(c.POSTFIELDS, postfields)
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()
body = buffer.getvalue()
# Body is a byte string.
# We have to know the encoding in order to print it to a text file
# such as standard output.
print('表单数据需要url序列化:'+ postfields)
print('\n\n')
print(body.decode('utf-8'))
输出
表单数据需要url序列化:params=this+is+value¶ms1=hello+world
{
"args": {},
"data": "",
"files": {},
"form": {
"params": "this is value",
"params1": "hello world"
},
"headers": {
"Accept": "*/*",
"Content-Length": "40",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "PycURL/7.43.0.6 libcurl/7.71.1 OpenSSL/1.1.1k zlib/1.2.11 libssh2/1.9.0",
"X-Amzn-Trace-Id": "Root=1-60e1b494-7d6e13807ca6fe07765724c4"
},
"json": null,
"origin": "103.142.140.170",
"url": "https://httpbin.org/post"
}
POSTFIELDS
会自动将请求方法设为POST,自定义请求方法需要用CUSTOMREQUEST
:
c.setopt(c.CUSTOMREQUEST, 'PATCH')
上传文件
参考资料:
标签:setopt,buffer,简介,python,print,import,BytesIO,PycURL 来源: https://blog.csdn.net/beibaozhao/article/details/118468129