面向python请求高级用户的实用程序带
requests-toolbelt的Python项目详细描述
请求工具带
这只是python请求的实用程序集合,但不是 真正属于 请求。最低测试请求版本为 2.1.0 。实际上,工具带也应该与 2.0.1 一起工作,但是 有些特性会妨碍对该版本进行有效或理智的测试。
pip install 请求工具带开始!
多部分/窗体数据编码器
主要的吸引力是一个流式多部分表单数据对象, 多部分编码器 。 它的api如下:
fromrequests_toolbeltimportMultipartEncoderimportrequestsm=MultipartEncoder(fields={'field0':'value','field1':'value','field2':('filename',open('file.py','rb'),'text/plain')})r=requests.post('http://httpbin.org/post',data=m,headers={'Content-Type':m.content_type})
您还可以对不需要的请求使用多部分/表单数据编码 需要文件:
fromrequests_toolbeltimportMultipartEncoderimportrequestsm=MultipartEncoder(fields={'field0':'value','field1':'value'})r=requests.post('http://httpbin.org/post',data=m,headers={'Content-Type':m.content_type})
或者,您可以创建字符串并检查数据:
# Assuming `m` is one of the abovem.to_string()# Always returns unicode
用户代理构造函数
您可以轻松构建一个请求样式 用户代理 字符串:
from requests_toolbelt import user_agent headers = { 'User-Agent': user_agent('my_package', '0.0.1') } r = requests.get('https://api.github.com/users', headers=headers)
雷达
ssladapter最初发表在cory benfield的博客上。 此适配器允许用户选择可用的SSL协议之一 在python的输出https连接的ssl模块中:
fromrequests_toolbeltimportSSLAdapterimportrequestsimportssls=requests.Session()s.mount('https://',SSLAdapter(ssl.PROTOCOL_TLSv1))
已知问题
在python 3.3.0和3.3.1上,标准库的http 模块将失败 当传递 multipartencoder 的实例时。以后再解决 python 3.3的小版本。请考虑升级到以后的未成年人 版本或Python3.4。 这个图书馆绝对无能为力 解决这个问题。
贡献
请阅读建议的工作流 为本项目做出贡献。
请在问题跟踪器上报告任何错误