• python 2 处理HTTP 请求的包


    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: *
    Disallow: /deny
    '

    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。

  • 相关阅读:
    服务器迁移的标准
    WMS日常运维_WJC
    WMS 162服务器空间满,清日志
    从壹开始【学代码】|| 我开发中的用到的几个框架
    从壹开始 [ Ids4实战 ] 之七 ║ 客户端、服务端、授权中心全线打通
    从壹开始 [ Design Pattern ] 之二 ║ 单例模式 与 Singleton
    从壹开始 [Admin] 之五 ║ 实现『按钮』级别权限配置
    从壹开始 NetCore 新篇章 ║ Blog.Core 开发社之招募计划书【已完成】
    回答
    三行
  • 原文地址:https://www.cnblogs.com/miniren/p/5885313.html
Copyright © 2020-2023  润新知