请求pycURL的兼容接口
pycurl-requests的Python项目详细描述
PycURL请求<pycurl://☤>
PycURL Requests是与的Requests兼容的接口 PycURL。在
要求
安装
通过^{
pip install pycurl-requests [--user]
通过Git:
^{pr2}$快速入门
>>>importpycurl_requestsasrequests>>>r=requests.get('https://api.github.com/repos/dcoles/pycurl-requests')>>>r.status_code200>>>r.headers['content-type']'application/json; charset=utf-8'>>>r.encoding'utf-8'>>>r.text'{\n "id": 236427187,\n...'>>>data=r.json()>>>data['name']'pycurl-requests'>>>data['html_url']'https://github.com/dcoles/pycurl-requests'>>>data['description']'A Requests-compatible interface for pycURL'
该库还可以用于运行导入requests
模块的现有Python脚本。
通过pycurl_requests
助手运行脚本,使用requests
模块将
自动重定向到pycurl_requests
。在
python3 -m pycurl_requests -- script.py arg arg...
request
工具
包含基本的curl
类命令行实用程序:
usage: request.py [-h] [-d DATA] [-H HEADER] [--json JSON] [-L] [-o OUTPUT]
[-X REQUEST] [-v]
url
A basic `curl`-like command-line HTTP utility
positional arguments:
url URL of resource to connect to
optional arguments:
-h, --help show this help message and exit
-d DATA, --data DATA Add POST data
-H HEADER, --header HEADER
Add custom request header (format: `Header: Value`)
--json JSON Add JSON POST data
-L, --location Follow redirects
-o OUTPUT, --output OUTPUT
Write to file instead of stdout
-X REQUEST, --request REQUEST
Request command to use (e.g. HTTP method)
-v, --verbose Verbose logging
这也可以与Requests库一起使用,如果
PYCURLREQUESTS_REQUESTS
环境变量设置为非空值。在
文件
此库旨在与Requests兼容, 因此Requests documentation应该是 主要适用。在
卷曲选项
可以在
^{
例如,在不请求尸体的情况下提出请求:
importpycurlimportpycurl_requestsasrequestswithrequests.Session()assession:session.curl.setopt(pycurl.NOBODY,1)response=session.get('http://example.com')
请参阅^{curl
属性方法。在
卷曲异常
全部^{
为了方便起见,原始的pycurl.error
错误消息和
cURL error code将在异常上设置
对象作为curl_message
和curl_code
属性。在
importpycurl_requestsasrequeststry:requests.get('http://connect_error')exceptrequests.RequestExceptionase:print('ERROR: {} (cURL error: {})'.format(e.curl_message,e.curl_code))
也可以使用__cause__
属性获得原始的pycurl.error
。在
日志
可以显示libcurl
中的详细日志记录,包括信息性文本和HTTP头
通过将curl
记录器(或子记录器)设置为^{
importlogginglogging.getLogger('curl').setLevel(logging.DEBUG)
对于每种类型的记录,日志记录被分成专用的子日志记录器:
curl.text
-信息性文本curl.header_in
-从对等方接收的头数据- ^{cd26>发送到对等端的数据
已知限制
- 不支持读取Cookies
- 不支持client-side certificates
- 不支持proxies
- 不支持link headers(例如^{
} ) - 不支持authentication
- 不支持sending multi-part encoded files)
- 对^{
} objects 的基本支持(例如^{} )
在不久的将来,这些特性中的大多数都应该得到支持。在
许可证
根据麻省理工学院的许可证。在
- 项目
标签: