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&params1=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')

上传文件

请参考官方文档


参考资料:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值