与 Requests 类似,但使用 urllib2。
项目描述
内置 urllib2 模块的 Python 包装器。该 API 与 [优秀的 Requests 库][requests] 兼容,但省略了会话和保持活动支持等功能。
Notrequests 旨在在 [Google App Engine][gae] 上执行 HTTP 请求,其中 Requests 有一些缺点。
它(还)不兼容 Python 3。
安装
来自 PyPI:
$ pip install notrequests
或正常下载并运行安装程序:
$ curl -L -o notrequests.zip https://github.com/davidwtbuxton/notrequests/archive/master.zip $ unzip notrequests.zip $ cd notrequests-master $ python setup.py install
用法
### 基本用法
Notrequests 与 Requests API 兼容(或者它试图兼容)。
>>> import notrequests >>> >>> response = notrequests.get('http://httpbin.org/get') >>> response.status_code == notrequests.codes.ok True
但它并没有做 Requests 所做的一切。没有会话支持,没有保持活动支持,它将整个响应读入内存。
响应正文可用作字节字符串或 unicode。
>>> response = notrequests.get('http://httpbin.org/encoding/utf8') >>> response.headers['content-type'] 'text/html; charset=utf-8' >>> type(response.content) <type 'str'> >>> type(response.text) <type 'unicode'>
解码为 unicode 依赖于服务器已发送有效的内容类型标头。这与 Requests 不同,因为 Requests 可以在响应不包含内容类型标头时嗅探编码。
Notrequests 使用 urllib2,但行为更像 Requests。所以它不会对 4xx 和 5xx 响应抛出异常。
>>> response = notrequests.get('http://httpbin.org/status/404') >>> response.status_code == notrequests.codes.not_found True
您还可以测试失败或引发异常。
>>> response = notrequests.get('http://httpbin.org/status/200') >>> response.ok True >>> response.raise_for_status() >>> response = notrequests.get('http://httpbin.org/status/404') >>> response.ok False >>> response.raise_for_status() Traceback (most recent call last): ... notrequests.HTTPError: Error 404 for http://httpbin.org/status/404
### 重定向
如果你想在重定向响应之后阻止 Notrequests,你可以使用allow_redirects关键字:
>>> url = 'http://httpbin.org/redirect/1' >>> response = notrequests.get(url) >>> response.status_code 200 >>> response = notrequests.get(url, allow_redirects=False) >>> response.status_code 302
在 Google App Engine 上,只有在 [发送应用程序不允许重定向!][appidentity] 时才会设置X-Appengine-Inbound-Appid标头
### 验证
您可以像请求一样执行基本身份验证(但不能执行其他身份验证类型):
>>> url = 'http://httpbin.org/basic-auth/alice/secret' >>> response = notrequests.get(url) >>> response.status_code 401 >>> response = notrequests.get(url, auth=('alice', 'secret')) >>> response.status_code 200
### JSON
并发送和解码 JSON:
>>> import pprint >>> response = notrequests.put('http://httpbin.org/put', json={'foo': ['bar', 'baz']}) >>> data = response.json() >>> pprint.pprint(data) {u'args': {}, u'data': u'{"foo": ["bar", "baz"]}', u'files': {}, u'form': {}, u'headers': {u'Accept-Encoding': u'identity', u'Content-Length': u'23', u'Content-Type': u'application/json', u'Host': u'httpbin.org', u'User-Agent': u'notrequests/0.1'}, u'json': {u'foo': [u'bar', u'baz']}, u'origin': u'10.10.10.1', u'url': u'http://httpbin.org/put'}
### 访问链接头
如果服务器在响应中发送了“链接”标头(通常由 API 用于提供指向下一页结果的链接),那么您可以直接从响应对象中获取已解析的链接:
>>> response.headers['Link'] '<https://example.com/?page=2>; rel="next"' >>> response.links['next']['url'] 'https://example.com/?page=2'
###上传文件
还支持上传文件:
>>> import io >>> fileobj = io.BytesIO('foo bar baz') >>> response = notrequests.post('http://httpbin.org/post', files={'upload': fileobj}) >>> response.json()['files'] {u'upload': 'foo bar baz'}
与请求一样,文件字典中的键是表单字段输入名称,文件字典中的值可以是带有文件对象或字节字符串的文件名的 2 元组:
>>> files = {'upload': ('my-file.txt', b'Foo\nbar\nbaz.')} >>> response = notrequests.post('http://httpbin.org/post', files=files) >>> print response.request.data --10.10.10.1.503.2717.1443987498.810.2 Content-Disposition: file; name="upload"; filename="my-file.txt" Content-Type: text/plain富吧巴兹。–10.10.10.1.503.2717.1443987498.810.2–
### 禁用 SSL 证书检查
使用verify关键字禁用 SSL 证书检查。默认值为verify=True ,因此如果证书与服务器的主机名不匹配,Notrequests 将引发ssl.CertificateError 。
>>> response = notrequests.get('https://swupdl.adobe.com', verify=False)
Notrequests 不支持指定备用 CA 捆绑包。
API 兼容性
这些是 Notrequests _未_实现的 [请求 API][api] 的一些功能。这不是一个完整的列表,如果有更好的支持会很好。
会话
响应历史
流式上传/下载和迭代数据
状态码的替代名称
代理
测试
使用 [tox][tox] 运行测试。
默认情况下,测试向http://httpbin.org发出请求,但您可以运行本地实例来加快速度。
$ pip install httpbin gunicorn $ gunicorn –bind 127.0.0.1:8888 httpbin:app $ export NOTREQUESTS_TEST_URL=”http://127.0.0.1:8888” $ tox
为什么不使用请求?
Google App Engine 修补标准库中的 httplib 以使用其 urlfetch 服务,并将 [sockets API][sockets] 限制为付费应用程序。Requests 不使用 httplib,而是使用套接字。
如果您想使用[应用程序身份服务来验证 App Engine 应用程序之间的连接][应用程序身份],您必须使用 urlfetch 服务,您不能使用请求。Notrequests 之所以有效,是因为它使用 urllib2,后者使用 httplib。
[请求]:http ://www.python-requests.org/ [gae]:https ://cloud.google.com/appengine/ [tox]:http ://codespeak.net/tox/ [appidentity]:https://cloud.google.com/appengine/docs/python/appidentity/#Python_Asserting_identity_to_other_App_Engine_apps [套接字]:https ://cloud.google.com/appengine/docs/python/sockets/ [api]:http:// requests.readthedocs.org/en/latest/api/