1 开发哲学
Beautiful is better than ugly.(美丽优于丑陋)
Explicit is better than implicit.(直白优于含蓄)
Simple is better than complex.(简单优于复杂)
Complex is better than complicated.(复杂优于繁琐)
Readability counts.(可读性很重要)
2 遵循的协议
Requests 的发布许可为 Apache2 License.
因为项目发行于 GPL 协议之后,就不能用于任何本身没开源的商业产品中。
MIT、BSD、ISC、Apache2 许可都是优秀的替代品,它们允许你的开源软件自由应用在私有闭源软件中。
3 requests信息
requests是一个Python的网络请求库,和urllib、httplib之流相比起来最大的优点就是好用。requests还支持https验证并且是线程安全的。
下载地址
https://github.com/kennethreitz/requests/tarball/master
安装后在eclipse如果还没有识别,需要在PYTHON解释器中加上
Properties-> PyDev Interpreter
Click here to configure an Interpreter not list.
4 简单使用
import requests
r = requests.get('https://www.python.org')
r.status_code
print r.status_code
直接执行即可。
或者如下:
import requests
r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
print r.status_code
print r.headers['content-type']
print r.encoding
print r.text
print r.json()
5 代码
因为官方提供的项目信息并不是特别多,一个一个解决不免枯燥乏味,为此想到一个实战办法。以具体实例为切入点一点一点阅读。
5.1 Request(主要接口)
示例代码如下:
import requests
r = requests.get('https://github.com/timeline.json')
print r
r = requests.post("http://httpbin.org/post")
print r
r = requests.put("http://httpbin.org/put")
print r
r = requests.delete("http://httpbin.org/delete")
print r
r = requests.head("http://httpbin.org/get")
print r
r = requests.options("http://httpbin.org/get")
print r
我们来学习一下
import 导入我们安装的requests包
然后调用函数get,post,put,delete,head,options
Requests 所有的功能都可以通过以下 7 个方法访问
最后都返回一个Response对象的实例。
5.1.1 get
在api.py文件中,内如如下:
def get(url, params=None, **kwargs):
r"""Sends a GETrequest.
:param url: URL for the new:class:`Request` object.
:param params: (optional)Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)
发送GET请求,参数有url,params,kwargs.
url参数Request对象。Params是字典。Kwargs是可选参数,被requests调用。
返回Response对象。
最后调用request函数。
5.1.2 post
def post(url, data=None, json=None, **kwargs):
r"""Sends a POSTrequest.
:param url: URL for the new:class:`Request` object.
:param data: (optional) Dictionary (willbe form-encoded), bytes, or file-like object to send in the body of the:class:`Request`.
:param json: (optional) jsondata to send in the body of the :class:`Request`.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
return request('post', url, data=data, json=json, **kwargs)
5.1.3 put
def put(url, data=None, **kwargs):
r"""Sends a PUTrequest.
:param url: URL for the new:class:`Request` object.
:param data: (optional) Dictionary (willbe form-encoded), bytes, or file-like object to send in the body of the:class:`Request`.
:param json: (optional) jsondata to send in the body of the :class:`Request`.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
return request('put', url, data=data, **kwargs)
和get基本一致。
5.1.4 delete
def delete(url, **kwargs):
r"""Sends a DELETErequest.
:param url: URL for the new:class:`Request` object.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
return request('delete', url, **kwargs)
和get基本一致。
5.1.5 head
def head(url, **kwargs):
r"""Sends a HEADrequest.
:param url: URL for the new:class:`Request` object.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', False)
return request('head', url, **kwargs)
和get基本一致。
5.1.6 options
def options(url, **kwargs):
r"""Sends a OPTIONSrequest.
:param url: URL for the new:class:`Request` object.
:param \*\*kwargs: Optionalarguments that ``request`` takes.
:return::class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('options', url, **kwargs)
和get基本一致。
5.1.7 request
def request(method, url, **kwargs):
"""Constructs andsends a :class:`Request <Request>`.
:param method: method for the new:class:`Request` object.
:param url: URL for the new:class:`Request` object.
:param params: (optional)Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param data: (optional) Dictionary or listof tuples ``[(key, value)]`` (will be form-encoded), bytes, or file-like objectto send in the body of the :class:`Request`.
:param json: (optional) jsondata to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary ofHTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict orCookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipartencoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``,3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type',custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-likeobject containing additional headers
to add for the file.
:param auth: (optional) Authtuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How long towait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional)Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection.Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional)Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either aboolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be apath
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``,the response content will be immediately downloaded.
:param cert: (optional) if String,path to ssl client cert file (.pem). If Tuple, ('cert','key') pair.
:return::class:`Response <Response>` object
:rtype: requests.Response
Usage::
>>> import requests
>>> req = requests.request('GET','http://httpbin.org/get')
<Response [200]>
"""
# By using the 'with' statement weare sure the session is closed, thus we
# avoid leaving sockets open whichcan trigger a ResourceWarning in some
# cases, and look like a memory leakin others.
with sessions.Session() as session:
return session.request(method=method,url=url, **kwargs)
打开session。那么我们来看下这个session对象。
5.2 session
位于session.py文件中。
这个模块提供Session对象来管理cookie持久化,连接池和配置.
示例:
import requests
s = requests.Session()
print s.get('http://httpbin.org/get')
或者:
import requests
with requests.Session() as s:
print s.get('http://httpbin.org/get')
5.3 身份验证
包含request的认证句柄。
5.4 exceptions异常
异常定义在exceptions.py文件中。
从urllib3中导入HTTPError,命名为BaseHTTPError
定义了RequestException类。
6 模块
6.1 包含关键模块urllib3
Requests允许你发送纯天然,植物饲养的 HTTP/1.1 请求,无需手工劳动。你不需要手动为 URL 添加查询字串,也不需要对 POST 数据进行表单编码。Keep-alive 和 HTTP 连接池的功能是 100% 自动化的,一切动力都来自于根植在 Requests 内部的 urllib3
7 附录A
7.1 httplib2
httplib2尽管名声在外,但它文档欠佳,而且基本操作要写的代码依旧太多。对于 httplib2,要写一个现代 HTTP 客户端要跟一吨低级麻烦事打交道,实在是件苦差事。但无论如何,还是直接使用 Requests 吧。Kenneth Reitz 是一个很负责的作者,他能把简单的东西做简单。httplib2 感觉像是一个学术产物,而 Requests 才真的是一个人们可以在生产系统中使用的东西。