Python的urllib3软件包详解
来源:互联网 发布:政府oa办公软件 编辑:程序博客网 时间:2024/05/20 05:10
urllib3是一款Python语言的HTTP客户端。
1. urllib3的特性
- 线程安全
- 连接缓冲池
- 客户端SSL/TLS验证
- 文件上传
- 请求重试
- HTTP重定向
- 支持gzip和deflate encoding
- 支持HTTP和SOCKS的代理
pip install urllib3
3. 用法
1)HTTP GET请求
>>> import urllib3>>> http = urllib3.PoolManager()>>> r = http.request('GET', 'http://httpbin.org/robots.txt')>>> r.status200>>> r.data...>>> r.headers...
注意:任何HTTP请求,只有通过PoolManager对象发出,才能够提供连接缓冲池和线程安全特性。
任何请求的返回对象都是HTTPResponse对象,其中包含status, data和headers三个属性。
2) HTTP POST请求
>>> import urllib3>>> http = urllib3.PoolManager()>>> r = http.request('POST', 'http://httpbin.org/post', fields={'hello': 'Xiangbin'})>>> r.status200>>> r.data...>>> r.headers...
3) JSON响应的处理
>>> import urllib3>>> import json>>> http = urllib3.PoolManager()>>> r = http.request('GET', 'http://httpbin.org/ip')>>> r.datab'{\n "origin": "104.232.115.37"\n}\n'>>> json.loads(r.data.decode('utf-8')){'origin': '127.0.0.1'}注意:使用json的loads()方法
4) 流式响应的处理
>>> import urllib3>>> http = urllib3.PoolManager()>>> r = http.request('GET', 'http://httpbin.org/bytes/1024', preload_content=False)>>> for chunk in r.stream(32):... print(chunk)...>>> r.release_conn()注意:preload_content=False表示流式处理响应数据。
处理stream()方法读取响应数据之外,还可以使用read()方法,示例如下:
>>> import urllib3>>> http = urllib3.PoolManager()>>> r = http.request('GET', 'http://httpbin.org/bytes/1024', preload_content=False)>>> r.read(4)b'\x88\x1f\x8b\xe5'>>> r.release_conn()
5) 请求带参数
>>> r = http.request('GET', 'http://httpbin.org/headers', fields={'hello': 'Xiangbin'}, headers={'X-Something': 'value'})
对于POST和PUT方法,需要将参数编码后,这样才可以追加到URL,示例如下:
>>> from urllib.parse import urlencode>>> encoded_args = urlencode({'arg': 'value'})>>> url = 'http://httpbin.org/post?' + encoded_args>>> r = http.request('POST', url)
>>> r = http.request('POST', 'http://httpbin.org/post', fields={'hello': 'Xiangbin'})
>>> import json>>> data = {'Hello': 'Xiangbin'}>>> encoded_data = json.dumps(data).encode('utf-8')>>> r = http.request('POST', 'http://httpbin.org/post', body=encoded_data, headers={'Content-Type': 'application/json'})>>> json.loads(r.data.decode('utf-8'))['json']{'Hello': 'Xiangbin'}
6) 上传文件
文本文件
>>> with open('example.txt') as fp:... file_data = fp.read()>>> r = http.request(... 'POST',... 'http://httpbin.org/post',... fields={... 'filefield': ('example.txt', file_data, 'text/plain'),... })>>> json.loads(r.data.decode('utf-8'))['files']{'filefield': '...'}注意:上传文件必须使用POST方法。
二进制文件
>>> with open('example.jpg', 'rb') as fp:... binary_data = fp.read()>>> r = http.request(... 'POST',... 'http://httpbin.org/post',... body=binary_data,... headers={'Content-Type': 'image/jpeg'})>>> json.loads(r.data.decode('utf-8'))['data']b'...'
参考链接:
http://urllib3.readthedocs.io/en/latest/index.html
https://github.com/shazow/urllib3
阅读全文
1 0
- Python的urllib3软件包详解
- Python--urllib3库详解
- Python--urllib3库详解1
- Python--urllib3库详解1
- Python的urllib3软件包的证书认证及警告的禁用
- Python的requests软件包详解
- Python - 体验urllib3 -- HTTP连接池的应用
- python中urllib, urllib2,urllib3, httplib,httplib2, request的区别
- urllib3
- python软件包的卸载
- Python软件包的配置
- [python]urllib3中的连接池--Poolmanager
- python软件包的安装(win7)
- 简单的python3 urllib3 多线程 抓取图片
- Http协议python优秀库 urllib3 (支持python3)
- 网页抓取方式(六)--python/urllib3/BeautifulSoup
- python的turtle画图软件包的使用
- 删除 setup.py 安装的 Python 软件包
- 在树莓派上搭建LAMP服务
- 笔试编程经典(leetcode在线编程)
- android studio——替换全局的某个字符串
- 关于Objective C运行时(runtime)技术的几个要点总结
- java使用HttpClient模拟发送http的get、post请求
- Python的urllib3软件包详解
- [转] MySQL死锁问题分析及解决方法实例详解
- XML文件结构和基本语法
- Markdown支持的语言
- TeamTalk 详细介绍
- rbtree
- python基础笔记-2
- 深度学习+几何结构:1+1>2?
- mac终端连接阿里云服务器报错WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!