Requests 完全满足今日 web 的需求。
- 国际化域名和 URL
- Keep-Alive & 连接池
- 带持久 Cookie 的会话
- 浏览器式的 SSL 认证
- 基本/摘要式的身份认证
- 优雅的 key/value Cookie
- 自动解压
- 自动内容解码
- Unicode 响应体
- 文件分块上传
- 连接超时
- 流下载
- 支持
.netrc
- 分块请求
- 线程安全
Requests 支持 Python 2.6—3.5,而且能在PyPy下完美运行。
payload = {'key1': 'value1', 'key2': ['value2', 'value3']} ret = requests.get('http://httpbin.org/get', params=payload) print(ret.url)
以上代码运行结果:
http://httpbin.org/get?key1=value1&key2=value2&key2=value3
ret.text 字符串
ret.content 字节
ret.status_code 状态码
实例:
向后台发数据
def send(self,info): import requests requests.post( url='http://127.0.0.1:8000/post_info.html/', json=info )
后台接收数据
def post_info(request): str = request.body # 后台从request.body里面接收数据 str = str.decode(encoding='utf-8') return HttpResponse('ok')