python 3 处理HTTP 请求的包
2017-06-27 18:35
232 查看
http
http: https://docs.python.org/3/library/http.htmlhttp是一个包,里面含有多个模块:http.client,http.server,http.cookies,http.cookiejar。http.client 对应python2.X 的 httplib 模块。官方文档对 http.client的说明如下:This module defines classes which implement the client side of the HTTP and HTTPS protocols. It is normally not used directly — the moduleurllib.request uses it to handle URLs that use HTTP and HTTPS.总结起来就是:该库一般不直接使用,比较底层。GET的官方例子:
>>> import http.client >>> conn = http.client.HTTPSConnection("www.python.org") >>> conn.request("GET", "/") >>> r1 = conn.getresponse() >>> print(r1.status, r1.reason) 200 OK >>> data1 = r1.read() # This will return entire content.
urllib
urllib:https://docs.python.org/3/library/urllib.htmlurllib也是一个包,里面含有多个模块:urllib.request,urllib.error,urllib.parse,urllib.robotparser。这里的urllib.request 跟python 2.X 的urllib2有点像。urllib.request 基于http.client,但是比 http.client 更高层一些。发送请求使用urllib.request.urlopen,URL可以接受字符串或者Request对象。带有data参数就是POST方法,否则就是GET。GET:>>> import urllib.request >>> import urllib.parse >>> params = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> url = "http://www.musi-cal.com/cgi-bin/query?%s" % params >>> with urllib.request.urlopen(url) as f: ... print(f.read().decode('utf-8'))
POST:
>>> import urllib.request >>> import urllib.parse >>> data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0}) >>> data = data.encode('ascii') >>> with urllib.request.urlopen("http://requestb.in/xrbl82xr", data) as f: ... print(f.read().decode('utf-8'))
urllib3
urllib3:https://pypi.python.org/pypi/urllib3urllib3 brings many critical features that are missing from the Python standard libraries: -Thread safety. -Connection pooling. -Client-side SSL/TLS verification. -File uploads with multipart encoding. -Helpers for retrying requests and dealing with HTTP redirects. -Support for gzip and deflate encoding. -Proxy support for HTTP and SOCKS.-100% test coverage.
总结起来就是:相比python的标准库,urllib3有很多很重要的特性,比如线程安全等。同时urllib3也很强大而且易于使用。GET示例:
>>> import urllib3>>> http = urllib3.PoolManager()>>> r = http.request('GET', 'http://httpbin.org/robots.txt')>>> r.status200 >>> r.data'User-agent: *\nDisallow: /deny\n'
Requests
Requests:http://docs.python-requests.org/en/latest/index.htmlRequests 基于urllib3,号称“Requests is an elegant and simple HTTP library for Python, built for human beings.”,意思就是专门为人类设计的HTTP库。使用的感觉就是优雅、简单大方 。推荐使用这个库,非常好用。官方示例:>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))>>> r.status_code200 >>> r.headers['content-type']'application/json; charset=utf8'>>> r.encoding'utf-8'>>> r.text u'{"type":"User"...'>>> r.json() {u'private_gists': 419, u'total_private_repos': 77, ...}
总结
Python 3 处理HTTP请求的包:http,urllib,urllib3,requests。其中,http 比较 low-level,一般不直接使用。urllib更 high-level一点,属于标准库。urllib3跟urllib类似,拥有一些重要特性而且易于使用,但是属于扩展库,需要安装。requests 基于urllib3 ,也不是标准库,但是使用非常方便。个人感觉,如果非要用标准库,就使用urllib。如果没有限制,就用requests。相关文章推荐
- python处理HTTP 请求的几种方式
- python 3 处理HTTP 请求的包
- 用 Python 实现每秒处理 120 万次 HTTP 请求
- python 2 处理HTTP 请求的包
- 用 Python 实现每秒处理 120 万次 HTTP 请求
- 通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流程[下]:管道是如何构建起来的?
- Struts2 拦截器处理普通Http请求和Ajax请求时拦截配置
- Http请求通过线程添加超时处理
- loadrunner处理HTTP重定向请求
- AJAX中同时发送多个请求XMLHttpRequest对象处理方法
- 学习笔记:使用HTTP处理程序监视指定目录请求
- Python入门:模拟登录(二)或注册之requests处理带token请求
- 关于python的bottle框架跨域请求报错问题的处理
- C#处理和对接HTTP接口请求
- 零基础写python爬虫之HTTP异常处理
- Python—HTTP处理Gzip压缩数据
- HttpURLConnection处理post请求,返回字符串
- python nginx+uwsgi+WSGI 处理请求详解
- Tomcat源码解读系列——Tomcat对HTTP请求处理的整体流程
- 他妈的 Python(1):怎么发起一个同步的 HTTP 请求