httplib

httplib: https://docs.python.org/2/library/httplib.html

python 的官方文档这样说明:

This module defines classes which implement the client side of the HTTP and HTTPS protocols. It is normally not used directly — the module urllib uses it to handle URLs that use HTTP and HTTPS.

总结起来就是:该库一般不直接使用,比较底层。

GET的官方例子:

>>> import httplib
>>> conn = httplib.HTTPSConnection("www.python.org")
>>> conn.request("GET", "/")
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
>>> data1 = r1.read()
>>> conn.request("GET", "/")
>>> r2 = conn.getresponse()
>>> print r2.status, r2.reason
404 Not Found
>>> data2 = r2.read()
>>> conn.close()

urllib

urllib:https://docs.python.org/2/library/urllib.html

基于httplib,但是比httplib更高层一些。

发送请求使用urllib.urlopen,带有params参数则是POST,否则就是GET。

GET:

>>> import urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query?%s" % params)
>>> print f.read()

POST:

>>> import urllib
>>> params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
>>> f = urllib.urlopen("http://www.musi-cal.com/cgi-bin/query", params)
>>> print f.read()

urllib2

urllib2:https://docs.python.org/2/library/urllib2.html

urllib2 也是使用 urlopen来发送请求。

urllib vs urllib2:

参考 Python: difference between urllib and urllib2

1) urllib不可以设置头信息等。urllib2的urlopen函数,URL参数可以是字符串或者Request对象,而Request对象可以设置头信息等;而urllib中URL只可以接受字符串。

2) urllib提供urlencode方法,urllib2没有。urlencode方法用来生成GET查询字符串。

正是由于urllib2没有urlencode方法,导致urllib使用的更广泛。

urllib3

urllib3:https://pypi.python.org/pypi/urllib3

urllib3 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.status
200
>>> r.data
'User-agent: *\nDisallow: /deny\n'

Requests

Requests:http://docs.python-requests.org/en/latest/index.html

Requests 基于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_code
200
>>> 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 2.X处理HTTP的包:httpliburlliburllib2urllib3requests

其中,httplib比较 low-level,一般不直接使用。

urllib、urllib2、urllib3比较类似:urllib用的比较多,urllib3拥有比较多的特性但是不是标准库。

requests 基于urllib3 ,也不是标准库,但是使用非常方便。

个人感觉,如果非要用标准库,就使用urllib。如果没有限制,就用requests。

python 2 处理HTTP 请求的包的更多相关文章

  1. python 3 处理HTTP 请求的包

    http http: https://docs.python.org/3/library/http.html http是一个包,里面含有多个模块:http.client,http.server,htt ...

  2. Python爬虫--- 1.1请求库的安装与使用

    来说先说爬虫的原理:爬虫本质上是模拟人浏览信息的过程,只不过他通过计算机来达到快速抓取筛选信息的目的所以我们想要写一个爬虫,最基本的就是要将我们需要抓取信息的网页原原本本的抓取下来.这个时候就要用到请 ...

  3. Python+requests 发送简单请求--》获取响应状态--》获取请求响应数据

    Python+requests 发送简单请求-->获取响应状态-->获取请求响应数据 1.环境:安装了Python和vscode编译器(Python自带的编译器也ok).fiddler抓包 ...

  4. Selenium爬虫实践(踩坑记录)之ajax请求抓包、浏览器退出

    上一篇: 使用Selenium截取网页上的图片 前言 最近在搞公司内部系统,累的一批,需要从另一个内部系统导出数据存到数据库做分析,有大量的数据采集工作,又没办法去直接拿到那个系统的接口,太难了,只能 ...

  5. iOS开发之Socket通信实战--Request请求数据包编码模块

    实际上在iOS很多应用开发中,大部分用的网络通信都是http/https协议,除非有特殊的需求会用到Socket网络协议进行网络数 据传输,这时候在iOS客户端就需要很好的第三方CocoaAsyncS ...

  6. I/O 请求数据包

    MSDN原文:https://msdn.microsoft.com/zh-cn/library/windows/hardware/hh439638(v=vs.85).aspx 发送到设备驱动程序的大部 ...

  7. python下的复杂网络编程包networkx的安装及使用

    由于py3.x与工具包的兼容问题,这里采用py2.7 1.python下的复杂网络编程包networkx的使用: http://blog.sina.com.cn/s/blog_720448d30101 ...

  8. Python标准库07 信号 (signal包,部分os包)

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在了解了Linux的信号基础之后,Python标准库中的signal包就很容易学习 ...

  9. python CSRF跨站请求伪造

    python CSRF跨站请求伪造 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

随机推荐

  1. SQL Server T-SQL高级查询【转】

    高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student;   --all 查询所有 select all sex from ...

  2. C#中检查网络是否连通的二种方法

    using System;  2 using System.Collections.Generic;  3 using System.Text;  4 //方法一  5 using System.Ru ...

  3. ecshop 函数列表大全

    lib_time.phpgmtime() P: 获得当前格林威治时间的时间戳 /$0server_timezone() P: 获得服务器的时区 /$0local_mktime($hour = NULL ...

  4. gooflow0.6的流程设计

    为何使用gooflow:1.兼容性好 2.扩展点很多可以个性化设计 3.配有api文档 4.json格式的数据传输 gooflow0.8版 由于最近项目需要,急需设计一个流程,考虑到时间问题,和用户个 ...

  5. 两个winform窗体同步

    /// <summary>        /// 初始left距离        /// </summary>        int initx = 0;        /// ...

  6. 浅谈ListBox控件,将对象封装在listBox中,在ListBox中显示对象中某个属性,在ListBox中移除和移动信息

    大家好,俗称万事开头难,不经历风雨,怎能见彩虹.在此小编给大家带来一个自己练习的小实例,希望与大家一起分享与交流.下面进入应用场景,从SQL2008数据库取出数据,给ListBox赋值在界面并显示出来 ...

  7. php多行字符串输出

      $content_header =<<<CONTENT_HEADER <section class="content-header"> <h ...

  8. POJ 2513 Colored Sticks 字典树、并查集、欧拉通路

    Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some ...

  9. Codeforces Round #205 (Div. 2) : D

    思维题,感叹自己的智商不够啊. 思路大概是这样的: 1.排在队伍前面的女生是不用换位置的: 2.女生在队伍中的顺序是不会变的: 3.最后一个女生稳定了则程序结束: 4.每个女生都有个初始位置和最终位置 ...

  10. 为什么用GPU挖比特币?

    http://www.leiphone.com/gpubitcoin.html 呵呵,这么红火的东东,不了解就长不了见识. 转一下两个东东,这挖矿机天天在算什么内容,还有,当前为什么GPU比CPU有优 ...