文件上传接口
这只是python请求的一组实用程序,但并不属于requests正确的。最低测试请求版本是 2.1.0。实际上,工具带2.0.1也应该可以使用,但是一些特性阻止了对该版本的有效或合理的测试。
pip install requests-toolbelt 开始!
multipart / form-data编码器
主要的吸引力是流式多部分表单数据对象,MultipartEncoder。它的API看起来像这样:
from requests_toolbelt import MultipartEncoder
import requests
m = 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})
您还可以multipart/form-data对不需要文件的请求使用编码:
from requests_toolbelt import MultipartEncoder
import requests
m = 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 above
m.to_string()  # Always returns unicode
用户代理构造函数
您可以轻松构建请求样式的User-Agent字符串:
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
在SSLAdapter最初发表在科里菲尔德的博客。此适配器允许用户选择Python ssl模块中提供的SSL协议之一用于传出HTTPS连接:
from requests_toolbelt import SSLAdapter
import requests
import ssl
s = requests.Session()
s.mount('https://', SSLAdapter(ssl.PROTOCOL_TLSv1))
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号