Compare commits
297 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d06908d655 | ||
|
|
75703d57e6 | ||
|
|
bd6b981d12 | ||
|
|
6a2eea8b21 | ||
|
|
325ea7b7e2 | ||
|
|
4ffae38627 | ||
|
|
0cd23d8d6e | ||
|
|
aca91e06f2 | ||
|
|
88177ec33f | ||
|
|
1d5c4f3f0f | ||
|
|
478d49027f | ||
|
|
46a770e03f | ||
|
|
ba25184ed5 | ||
|
|
7f1ad5c127 | ||
|
|
ab19b79375 | ||
|
|
c63472e328 | ||
|
|
6963b8490c | ||
|
|
12ef43e407 | ||
|
|
92f7478174 | ||
|
|
19d38d502f | ||
|
|
52d328ec3c | ||
|
|
a554828931 | ||
|
|
80a861cb84 | ||
|
|
0d07d1afb3 | ||
|
|
030b9763b5 | ||
|
|
03a3ca5004 | ||
|
|
1325409560 | ||
|
|
13aeb9cb06 | ||
|
|
7d217bf9bd | ||
|
|
cb2116dcce | ||
|
|
db90d85a8d | ||
|
|
af791d48c6 | ||
|
|
120a2f385a | ||
|
|
5bb2be9a23 | ||
|
|
e958511df0 | ||
|
|
1642996798 | ||
|
|
c41932e184 | ||
|
|
25ea8cdb38 | ||
|
|
e706d18cf8 | ||
|
|
3e72f234d0 | ||
|
|
25dc07e8ed | ||
|
|
13de69299f | ||
|
|
90e109c241 | ||
|
|
d25ba77b2e | ||
|
|
c0d4b23cea | ||
|
|
99eead01a3 | ||
|
|
d60845303b | ||
|
|
b14584f36e | ||
|
|
23d8522285 | ||
|
|
c4f9340fb4 | ||
|
|
d372a5b10b | ||
|
|
7aad3fd08c | ||
|
|
76c4b68e4a | ||
|
|
07441c8951 | ||
|
|
98ff29f793 | ||
|
|
08cab48431 | ||
|
|
7e7c275504 | ||
|
|
73abf84ace | ||
|
|
7eba5ffe48 | ||
|
|
1d2ee524d8 | ||
|
|
38f2581d6f | ||
|
|
b80f8aa475 | ||
|
|
057f32924a | ||
|
|
6a0845c984 | ||
|
|
18b29ea005 | ||
|
|
796d3225dd | ||
|
|
be62645dd5 | ||
|
|
603fd42fe6 | ||
|
|
1fc567449c | ||
|
|
c01bc5be5b | ||
|
|
577dba2bf7 | ||
|
|
a788cb7271 | ||
|
|
178ff62b93 | ||
|
|
aa99525537 | ||
|
|
7e0b9cf409 | ||
|
|
017c027629 | ||
|
|
c2480f65e6 | ||
|
|
3f86e22a07 | ||
|
|
b53975327f | ||
|
|
d0390d4f27 | ||
|
|
f3393fb24c | ||
|
|
4c21106222 | ||
|
|
87d9d9643c | ||
|
|
df5dcb8a7d | ||
|
|
69ba64380b | ||
|
|
d0285fac42 | ||
|
|
f73bda06e9 | ||
|
|
4dfd6f3fc1 | ||
|
|
cdec20af65 | ||
|
|
09a7251245 | ||
|
|
56f4b7ca68 | ||
|
|
4c8d1b9a7d | ||
|
|
6da7e22a4a | ||
|
|
5d9fcc711b | ||
|
|
2e31696156 | ||
|
|
0fb13e0b6c | ||
|
|
1da1490bcd | ||
|
|
9cdc8325ae | ||
|
|
12d66cfc41 | ||
|
|
9c8660dbb6 | ||
|
|
e7bc9bf1b2 | ||
|
|
e2ad0d0fe8 | ||
|
|
3299771e34 | ||
|
|
f2be5fae27 | ||
|
|
90cd37fda0 | ||
|
|
b0657cf163 | ||
|
|
03f6908fe9 | ||
|
|
c68f928254 | ||
|
|
ff954e16f1 | ||
|
|
d437c338d0 | ||
|
|
9fc7059140 | ||
|
|
544d08d0f6 | ||
|
|
ae55676a50 | ||
|
|
b07c1ebd85 | ||
|
|
113b67c069 | ||
|
|
12246afab8 | ||
|
|
85da715f40 | ||
|
|
b554b0c1cb | ||
|
|
9ff8cbe5fb | ||
|
|
79727abb32 | ||
|
|
ba485913b6 | ||
|
|
a95bfd3032 | ||
|
|
207a2744aa | ||
|
|
95084c9ca8 | ||
|
|
2f7923bc05 | ||
|
|
2ac3913733 | ||
|
|
231e343a3b | ||
|
|
c8ad4f0b73 | ||
|
|
25545b393b | ||
|
|
dfa89e299a | ||
|
|
318300848f | ||
|
|
d4aaef1e9a | ||
|
|
40a060cf57 | ||
|
|
ac5e6807fa | ||
|
|
250896b7f3 | ||
|
|
9d1d927420 | ||
|
|
440aeca5d4 | ||
|
|
b6af86225d | ||
|
|
c50f8f7cc8 | ||
|
|
8d8865aadb | ||
|
|
f7c10ca74d | ||
|
|
4cac9f07c4 | ||
|
|
f453892960 | ||
|
|
44b1e7ebea | ||
|
|
4ce6440795 | ||
|
|
22623bd8c2 | ||
|
|
5861254d8f | ||
|
|
ab2e7df8a9 | ||
|
|
beec48c8f8 | ||
|
|
b2427223c1 | ||
|
|
ef52044c5f | ||
|
|
ccea28db07 | ||
|
|
e21ef547f7 | ||
|
|
0af8ff9405 | ||
|
|
6e0ca583fe | ||
|
|
2bcc4a774a | ||
|
|
34268b16c3 | ||
|
|
1cfe59299b | ||
|
|
e771aa9386 | ||
|
|
cc7bee19f0 | ||
|
|
088f908d58 | ||
|
|
c4ad8afba9 | ||
|
|
0c09d16b29 | ||
|
|
30573b2d8b | ||
|
|
1a87f15e6f | ||
|
|
472a5b4748 | ||
|
|
b94510fc42 | ||
|
|
6e53290acd | ||
|
|
ca85b4ec6c | ||
|
|
a6360ca134 | ||
|
|
47aa968f3c | ||
|
|
3bf46609c8 | ||
|
|
cd0cbada65 | ||
|
|
ed559bb8b1 | ||
|
|
27e814ad76 | ||
|
|
03893d9b7f | ||
|
|
297aa04bea | ||
|
|
c678a7b402 | ||
|
|
b7242a1ad1 | ||
|
|
cf0a5089f5 | ||
|
|
de32f1774f | ||
|
|
e1c4fe21d4 | ||
|
|
89c8cbbe0c | ||
|
|
16ba63ccde | ||
|
|
347a52aa5c | ||
|
|
c920ee57b8 | ||
|
|
985a906c6c | ||
|
|
f038f68ad8 | ||
|
|
d3e6597f73 | ||
|
|
f8d729a17b | ||
|
|
b66427eab9 | ||
|
|
35d4a47cb1 | ||
|
|
a721d590b4 | ||
|
|
628e393b9a | ||
|
|
a950322b07 | ||
|
|
34917618d8 | ||
|
|
1a7c91f658 | ||
|
|
41687c8fa8 | ||
|
|
91f6e69cc4 | ||
|
|
6ec1a9ca52 | ||
|
|
9d33629b6d | ||
|
|
ef8563ab36 | ||
|
|
a392a87389 | ||
|
|
4a5b5bc86e | ||
|
|
68edcd12b1 | ||
|
|
fbc366c1a5 | ||
|
|
20b10aed1b | ||
|
|
b4505284f2 | ||
|
|
47fe258b5a | ||
|
|
21616a815b | ||
|
|
50934e056e | ||
|
|
ba1df2cfca | ||
|
|
7dc13e5cab | ||
|
|
7687746c7c | ||
|
|
5264c71d86 | ||
|
|
5d87e1aeba | ||
|
|
c03e893416 | ||
|
|
fef09a6691 | ||
|
|
1d35d439cc | ||
|
|
054e1ab3ee | ||
|
|
641f4611b2 | ||
|
|
594716abea | ||
|
|
d675abbd3f | ||
|
|
702f4039bc | ||
|
|
b479a62ec2 | ||
|
|
81c18c70f5 | ||
|
|
bbea679ab2 | ||
|
|
f1d51aa7eb | ||
|
|
5b5ff69201 | ||
|
|
0665129008 | ||
|
|
afd6482219 | ||
|
|
a23f22e5dc | ||
|
|
e00e50d01a | ||
|
|
1960c3c29d | ||
|
|
08f66a99b1 | ||
|
|
ab64052642 | ||
|
|
25a9c9fb22 | ||
|
|
f0beed8686 | ||
|
|
5a7d461230 | ||
|
|
d45eb53bc7 | ||
|
|
911e8aec8d | ||
|
|
9f89ebe0de | ||
|
|
526a0befc7 | ||
|
|
f0fe551dc9 | ||
|
|
f8e2d0e732 | ||
|
|
fcccc82282 | ||
|
|
3ddcc99131 | ||
|
|
70faca2a14 | ||
|
|
5f9fecd3aa | ||
|
|
6e780fad6d | ||
|
|
28b706da83 | ||
|
|
aa3347b2a3 | ||
|
|
f8a59c3e6f | ||
|
|
484916066a | ||
|
|
b3fbc810d2 | ||
|
|
1539d17366 | ||
|
|
a158abfa15 | ||
|
|
7d085b188c | ||
|
|
aaa3bf16a9 | ||
|
|
22a1f0917b | ||
|
|
78f48aef8f | ||
|
|
7b1b9df423 | ||
|
|
e23343ab17 | ||
|
|
b8affd9520 | ||
|
|
1790b1df17 | ||
|
|
60b4843678 | ||
|
|
0b42772663 | ||
|
|
a49db50b4a | ||
|
|
bfef8d99c8 | ||
|
|
dd271782e8 | ||
|
|
09da1921ff | ||
|
|
0769ee3b6d | ||
|
|
05de270d7a | ||
|
|
f1ba27faa2 | ||
|
|
8cb904b49d | ||
|
|
7e594eb121 | ||
|
|
4966d9c714 | ||
|
|
14da5cf180 | ||
|
|
6acce57271 | ||
|
|
c60b72e2d0 | ||
|
|
b2d03ac9a2 | ||
|
|
027fbd3d51 | ||
|
|
a2f773a9fc | ||
|
|
68d394952a | ||
|
|
e355d6ac6c | ||
|
|
dc74223461 | ||
|
|
5c4aa0bcb7 | ||
|
|
8c01865d62 | ||
|
|
916e6fcd64 | ||
|
|
4701243199 | ||
|
|
a7c5d5e8ac | ||
|
|
d0f23820b5 | ||
|
|
fe0b0a989f | ||
|
|
f549efd43d | ||
|
|
5eb3a17b55 | ||
|
|
54b77afbb5 | ||
|
|
be364e13d3 |
@@ -2,8 +2,10 @@ language: python
|
||||
python:
|
||||
- 2.6
|
||||
- 2.7
|
||||
- 3.2
|
||||
- 3.3
|
||||
env: HTTPBIN_URL=http://httpbin.org/
|
||||
script: make test
|
||||
script: invoke test
|
||||
install:
|
||||
- make test-deps
|
||||
- pip install -r requirements.txt
|
||||
notifications:
|
||||
email: false
|
||||
|
||||
@@ -117,3 +117,9 @@ Patches and Suggestions
|
||||
- Stephen Zhuang (everbird)
|
||||
- Martijn Pieters
|
||||
- Jonatan Heyman
|
||||
- David Bonner <dbonner@gmail.com> @rascalking
|
||||
- Vinod Chandru
|
||||
- Johnny Goodnow <j.goodnow29@gmail.com>
|
||||
- Denis Ryzhkov <denisr@denisr.com>
|
||||
- Wilfred Hughes <me@wilfred.me.uk> @dontYetKnow
|
||||
- Dmitry Medvinsky <me@dmedvinsky.name>
|
||||
|
||||
54
HISTORY.rst
54
HISTORY.rst
@@ -3,6 +3,54 @@
|
||||
History
|
||||
-------
|
||||
|
||||
1.2.0 (2013-03-31)
|
||||
++++++++++++++++++
|
||||
|
||||
- Fixed cookies on sessions and on requests
|
||||
- Significantly change how hooks are dispatched - hooks now receive all the
|
||||
arguments specified by the user when making a request so hooks can make a
|
||||
secondary request with the same parameters. This is especially necessary for
|
||||
authentication handler authors
|
||||
- certifi support was removed
|
||||
- Fixed bug where using OAuth 1 with body ``signature_type`` sent no data
|
||||
- Major proxy work thanks to @Lukasa including parsing of proxy authentication
|
||||
from the proxy url
|
||||
- Fix DigestAuth handling too many 401s
|
||||
- Update vendored urllib3 to include SSL bug fixes
|
||||
- Allow keyword arguments to be passed to ``json.loads()`` via the
|
||||
``Response.json()`` method
|
||||
- Don't send ``Content-Length`` header by default on ``GET`` or ``HEAD``
|
||||
requests
|
||||
- Add ``elapsed`` attribute to ``Response`` objects to time how long a request
|
||||
took.
|
||||
- Fix ``RequestsCookieJar``
|
||||
- Sessions and Adapters are now picklable, i.e., can be used with the
|
||||
mutiprocessing library
|
||||
- Update charade to version 1.0.3
|
||||
|
||||
The change in how hooks are dispatched will likely cause a great deal of
|
||||
issues.
|
||||
|
||||
1.1.0 (2013-01-10)
|
||||
++++++++++++++++++
|
||||
|
||||
- CHUNKED REQUESTS
|
||||
- Support for iterable response bodies
|
||||
- Assume servers persist redirect params
|
||||
- Allow explicit content types to be specified for file data
|
||||
- Make merge_kwargs case-insensitive when looking up keys
|
||||
|
||||
1.0.3 (2012-12-18)
|
||||
++++++++++++++++++
|
||||
|
||||
- Fix file upload encoding bug
|
||||
- Fix cookie behavior
|
||||
|
||||
1.0.2 (2012-12-17)
|
||||
++++++++++++++++++
|
||||
|
||||
- Proxy fix for HTTPAdapter.
|
||||
|
||||
1.0.1 (2012-12-17)
|
||||
++++++++++++++++++
|
||||
|
||||
@@ -20,7 +68,7 @@ History
|
||||
- /s/prefetch/stream
|
||||
- Removal of all configuration
|
||||
- Standard library logging
|
||||
- Make Reponse.json() callable, not property.
|
||||
- Make Response.json() callable, not property.
|
||||
- Usage of new charade project, which provides python 2 and 3 simultaneous chardet.
|
||||
- Removal of all hooks except 'response'
|
||||
- Removal of all authentication helpers (OAuth, Kerberos)
|
||||
@@ -555,10 +603,10 @@ This is not a backwards compatible change.
|
||||
++++++++++++++++++
|
||||
|
||||
* New HTTPHandling Methods
|
||||
- Reponse.__nonzero__ (false if bad HTTP Status)
|
||||
- Response.__nonzero__ (false if bad HTTP Status)
|
||||
- Response.ok (True if expected HTTP Status)
|
||||
- Response.error (Logged HTTPError if bad HTTP Status)
|
||||
- Reponse.raise_for_status() (Raises stored HTTPError)
|
||||
- Response.raise_for_status() (Raises stored HTTPError)
|
||||
|
||||
|
||||
0.2.2 (2011-02-14)
|
||||
|
||||
4
LICENSE
4
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright 2012 Kenneth Reitz
|
||||
Copyright 2013 Kenneth Reitz
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@@ -10,4 +10,4 @@ Copyright 2012 Kenneth Reitz
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
limitations under the License.
|
||||
|
||||
@@ -1 +1 @@
|
||||
include README.rst LICENSE NOTICE HISTORY.rst test_requests.py requests/cacert.pem
|
||||
include README.rst LICENSE NOTICE HISTORY.rst test_requests.py requirements.txt requests/cacert.pem
|
||||
|
||||
30
Makefile
30
Makefile
@@ -1,30 +0,0 @@
|
||||
SHELL := /bin/bash
|
||||
|
||||
test:
|
||||
py.test
|
||||
|
||||
test-deps:
|
||||
pip install -r requirements.txt
|
||||
|
||||
six:
|
||||
python test_requests.py
|
||||
python3 test_requests.py
|
||||
|
||||
deps: urllib3 certs charade
|
||||
|
||||
urllib3:
|
||||
rm -fr requests/packages/urllib3
|
||||
git clone https://github.com/shazow/urllib3.git
|
||||
cd urllib3 && git checkout master && cd ..
|
||||
mv urllib3/urllib3 requests/packages/
|
||||
rm -fr urllib3
|
||||
|
||||
charade:
|
||||
rm -fr requests/packages/charade
|
||||
git clone https://github.com/sigmavirus24/charade.git
|
||||
cd charade && git checkout master && cd ..
|
||||
mv charade/charade requests/packages/
|
||||
rm -fr charade
|
||||
|
||||
certs:
|
||||
cd requests && curl -O https://raw.github.com/kennethreitz/certifi/master/certifi/cacert.pem
|
||||
@@ -74,7 +74,7 @@ Contribute
|
||||
----------
|
||||
|
||||
#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug. There is a Contributor Friendly tag for issues that should be ideal for people who are not very familiar with the codebase yet.
|
||||
#. Fork `the repository`_ on Github to start making your changes to the **develop** branch (or branch off of it).
|
||||
#. Fork `the repository`_ on Github to start making your changes to the **master** branch (or branch off of it).
|
||||
#. Write a test which shows that the bug was fixed or that the feature works as expected.
|
||||
#. Send a pull request and bug the maintainer until it gets merged and published. :) Make sure to add yourself to AUTHORS_.
|
||||
|
||||
|
||||
2
docs/_templates/sidebarintro.html
vendored
2
docs/_templates/sidebarintro.html
vendored
@@ -1,6 +1,6 @@
|
||||
<p class="logo">
|
||||
<a href="{{ pathto(master_doc) }}">
|
||||
<img class="logo" src="{{ pathto('_static/requests-sidebar.png', 1) }}" alt="Logo"/>
|
||||
<img class="logo" src="{{ pathto('_static/requests-sidebar.png', 1) }}" title="Rezzy the Requests Sea Turtle"/>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
|
||||
2
docs/_templates/sidebarlogo.html
vendored
2
docs/_templates/sidebarlogo.html
vendored
@@ -1,6 +1,6 @@
|
||||
<p class="logo">
|
||||
<a href="{{ pathto(master_doc) }}">
|
||||
<img class="logo" src="{{ pathto('_static/requests-sidebar.png', 1) }}" alt="Logo"/>
|
||||
<img class="logo" src="{{ pathto('_static/requests-sidebar.png', 1) }}" title="Rezzy the Requests Sea Turtle"/>
|
||||
</a>
|
||||
</p>
|
||||
<p>
|
||||
|
||||
142
docs/api.rst
142
docs/api.rst
@@ -1,7 +1,7 @@
|
||||
.. _api:
|
||||
|
||||
API
|
||||
===
|
||||
Developer Interface
|
||||
===================
|
||||
|
||||
.. module:: requests
|
||||
|
||||
@@ -18,14 +18,6 @@ They all return an instance of the :class:`Response <Response>` object.
|
||||
|
||||
.. autofunction:: request
|
||||
|
||||
---------------------
|
||||
|
||||
|
||||
.. autoclass:: Response
|
||||
:inherited-members:
|
||||
|
||||
---------------------
|
||||
|
||||
.. autofunction:: head
|
||||
.. autofunction:: get
|
||||
.. autofunction:: post
|
||||
@@ -34,15 +26,22 @@ They all return an instance of the :class:`Response <Response>` object.
|
||||
.. autofunction:: delete
|
||||
|
||||
|
||||
-----------------
|
||||
Lower-Level Classes
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
.. autofunction:: session
|
||||
.. autoclass:: requests.Request
|
||||
:inherited-members:
|
||||
|
||||
.. autoclass:: Response
|
||||
:inherited-members:
|
||||
|
||||
Request Sessions
|
||||
----------------
|
||||
|
||||
.. autoclass:: Session
|
||||
:inherited-members:
|
||||
|
||||
|
||||
|
||||
Exceptions
|
||||
~~~~~~~~~~
|
||||
|
||||
@@ -55,41 +54,6 @@ Exceptions
|
||||
.. autoexception:: TooManyRedirects
|
||||
|
||||
|
||||
.. _configurations:
|
||||
|
||||
Configurations
|
||||
--------------
|
||||
|
||||
.. automodule:: requests.defaults
|
||||
|
||||
|
||||
.. _async:
|
||||
|
||||
Async
|
||||
-----
|
||||
|
||||
.. module:: requests.async
|
||||
|
||||
|
||||
.. autofunction:: map
|
||||
.. autofunction:: request
|
||||
.. autofunction:: head
|
||||
.. autofunction:: get
|
||||
.. autofunction:: post
|
||||
.. autofunction:: put
|
||||
.. autofunction:: patch
|
||||
.. autofunction:: delete
|
||||
|
||||
|
||||
|
||||
Utilities
|
||||
---------
|
||||
|
||||
These functions are used internally, but may be useful outside of
|
||||
Requests.
|
||||
|
||||
.. module:: requests.utils
|
||||
|
||||
Status Code Lookup
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -123,14 +87,6 @@ Encodings
|
||||
.. autofunction:: decode_gzip
|
||||
|
||||
|
||||
Internals
|
||||
---------
|
||||
|
||||
These items are an internal component to Requests, and should never be
|
||||
seen by the end user (developer). This part of the API documentation
|
||||
exists for those who are extending the functionality of Requests.
|
||||
|
||||
|
||||
Classes
|
||||
~~~~~~~
|
||||
|
||||
@@ -140,8 +96,82 @@ Classes
|
||||
.. autoclass:: requests.Request
|
||||
:inherited-members:
|
||||
|
||||
.. autoclass:: requests.PreparedRequest
|
||||
:inherited-members:
|
||||
|
||||
.. _sessionapi:
|
||||
|
||||
.. autoclass:: requests.Session
|
||||
:inherited-members:
|
||||
|
||||
|
||||
Migrating to 1.x
|
||||
----------------
|
||||
|
||||
This section details the main differences between 0.x and 1.x and is meant
|
||||
to ease the pain of upgrading.
|
||||
|
||||
|
||||
API Changes
|
||||
~~~~~~~~~~~
|
||||
|
||||
* ``Response.json`` is now a callable and not a property of a response.
|
||||
|
||||
::
|
||||
|
||||
import requests
|
||||
r = requests.get('https://github.com/timeline.json')
|
||||
r.json() # This *call* raises an exception if JSON decoding fails
|
||||
|
||||
* The ``Session`` API has changed. Sessions objects no longer take parameters.
|
||||
``Session`` is also now capitalized, but it can still be
|
||||
instantiated with a lowercase ``session`` for backwards compatibility.
|
||||
|
||||
::
|
||||
|
||||
s = requests.Session() # formerly, session took parameters
|
||||
s.auth = auth
|
||||
s.headers.update(headers)
|
||||
r = s.get('http://httpbin.org/headers')
|
||||
|
||||
* All request hooks have been removed except 'response'.
|
||||
|
||||
* Authentication helpers have been broken out into separate modules. See
|
||||
requests-oauthlib_ and requests-kerberos_.
|
||||
|
||||
.. _requests-oauthlib: https://github.com/requests/requests-oauthlib
|
||||
.. _requests-kerberos: https://github.com/requests/requests-kerberos
|
||||
|
||||
* The parameter for streaming requests was changed from ``prefetch`` to
|
||||
``stream`` and the logic was inverted. In addition, ``stream`` is now
|
||||
required for raw response reading.
|
||||
|
||||
::
|
||||
|
||||
# in 0.x, passing prefetch=False would accomplish the same thing
|
||||
r = requests.get('https://github.com/timeline.json', stream=True)
|
||||
r.raw.read(10)
|
||||
|
||||
* The ``config`` parameter to the requests method has been removed. Some of
|
||||
these options are now configured on a ``Session`` such as keep-alive and
|
||||
maximum number of redirects. The verbosity option should be handled by
|
||||
configuring logging.
|
||||
|
||||
::
|
||||
|
||||
# Verbosity should now be configured with logging
|
||||
my_config = {'verbose': sys.stderr}
|
||||
requests.get('http://httpbin.org/headers', config=my_config) # bad!
|
||||
|
||||
|
||||
Licensing
|
||||
~~~~~~~~~
|
||||
|
||||
One key difference that has nothing to do with the API is a change in the
|
||||
license from the ISC_ license to the `Apache 2.0`_ license. The Apache 2.0
|
||||
license ensures that contributions to requests are also covered by the Apache
|
||||
2.0 license.
|
||||
|
||||
.. _ISC: http://opensource.org/licenses/ISC
|
||||
.. _Apache 2.0: http://opensource.org/licenses/Apache-2.0
|
||||
|
||||
|
||||
@@ -54,30 +54,9 @@ Python 3 Support?
|
||||
Yes! Here's a list of Python platforms that are officially
|
||||
supported:
|
||||
|
||||
* cPython 2.6
|
||||
* cPython 2.7
|
||||
* cPython 3.1
|
||||
* cPython 3.2
|
||||
* PyPy-c 1.4
|
||||
* PyPy-c 1.5
|
||||
* PyPy-c 1.6
|
||||
* PyPy-c 1.7
|
||||
|
||||
|
||||
Keep-alive Support?
|
||||
-------------------
|
||||
|
||||
Yep!
|
||||
|
||||
|
||||
Proxy Support?
|
||||
--------------
|
||||
|
||||
You bet!
|
||||
|
||||
|
||||
SSL Verification?
|
||||
-----------------
|
||||
|
||||
Absolutely.
|
||||
|
||||
* Python 2.6
|
||||
* Python 2.7
|
||||
* Python 3.1
|
||||
* Python 3.2
|
||||
* Python 3.3
|
||||
* PyPy 1.9
|
||||
|
||||
@@ -1,27 +1,3 @@
|
||||
Modules
|
||||
=======
|
||||
|
||||
- `requests-oauth <https://github.com/maraujop/requests-oauth>`_, adds OAuth support to Requests.
|
||||
- `rauth <https://github.com/litl/rauth>`_, an alternative to requests-oauth, supports OAuth versions 1.0 and 2.0.
|
||||
- `FacePy <https://github.com/jgorset/facepy>`_, a Python wrapper to the Facebook API.
|
||||
- `robotframework-requests <https://github.com/bulkan/robotframework-requests>`_, a Robot Framework API wrapper.
|
||||
- `fullerene <https://github.com/bitprophet/fullerene>`_, a Graphite Dashboard.
|
||||
- `urbanairship-python <https://github.com/benjaminws/urbanairship-python>`_, a fork of the Urban Airship API wrapper.
|
||||
- `WhitespaceBot <https://github.com/Gunio/WhitespaceBot/>`_, a project that automatically forks repos, strips trailing whitespace, and sends a pull request.
|
||||
- `python-rexster <https://github.com/CulturePlex/python-rexster>`_, Rexter client that provides a simple interface for graph databases.
|
||||
- `daikon <https://github.com/neogenix/daikon>`_, a CLI for ElasticSearch.
|
||||
|
||||
Articles & Talks
|
||||
================
|
||||
- `Python for the Web <http://gun.io/blog/python-for-the-web/>`_ teaches how to use Python to interact with the web, using Requests.
|
||||
- `Daniel Greenfield's Review of Requests <http://pydanny.blogspot.com/2011/05/python-http-requests-for-humans.html>`_
|
||||
- `My 'Python for Humans' talk <http://python-for-humans.heroku.com>`_ ( `audio <http://codeconf.s3.amazonaws.com/2011/pycodeconf/talks/PyCodeConf2011%20-%20Kenneth%20Reitz.m4a>`_ )
|
||||
- `Issac Kelly's 'Consuming Web APIs' talk <http://issackelly.github.com/Consuming-Web-APIs-with-Python-Talk/slides/slides.html>`_
|
||||
- `Blog post about Requests via Yum <http://arunsag.wordpress.com/2011/08/17/new-package-python-requests-http-for-humans/>`_
|
||||
- `Russian blog post introducing Requests <http://habrahabr.ru/blogs/python/126262/>`_
|
||||
- `French blog post introducing Requests <http://www.nicosphere.net/requests-urllib2-de-python-simplifie-2432/>`_
|
||||
|
||||
|
||||
Integrations
|
||||
============
|
||||
|
||||
@@ -36,29 +12,22 @@ To give it a try, simply::
|
||||
|
||||
import requests
|
||||
|
||||
Python for iOS
|
||||
--------------
|
||||
|
||||
Managed Packages
|
||||
Requests is built into the wonderful `Python for iOS <https://itunes.apple.com/us/app/python-2.7-for-ios/id485729872?mt=Python8>`_ runtime!
|
||||
|
||||
To give it a try, simply::
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
Articles & Talks
|
||||
================
|
||||
|
||||
Requests is available in a number of popular package formats. Of course,
|
||||
the ideal way to install Requests is via The Cheeseshop.
|
||||
|
||||
|
||||
Ubuntu & Debian
|
||||
---------------
|
||||
|
||||
Requests is available installed as a Debian package! Debian Etch Ubuntu, since Oneiric::
|
||||
|
||||
$ apt-get install python-requests
|
||||
|
||||
|
||||
Fedora and RedHat
|
||||
-----------------
|
||||
|
||||
You can easily install Requests v0.6.1 with yum on rpm-based systems::
|
||||
|
||||
$ yum install python-requests
|
||||
|
||||
|
||||
|
||||
|
||||
- `Python for the Web <http://gun.io/blog/python-for-the-web/>`_ teaches how to use Python to interact with the web, using Requests.
|
||||
- `Daniel Greenfield's Review of Requests <http://pydanny.blogspot.com/2011/05/python-http-requests-for-humans.html>`_
|
||||
- `My 'Python for Humans' talk <http://python-for-humans.heroku.com>`_ ( `audio <http://codeconf.s3.amazonaws.com/2011/pycodeconf/talks/PyCodeConf2011%20-%20Kenneth%20Reitz.m4a>`_ )
|
||||
- `Issac Kelly's 'Consuming Web APIs' talk <http://issackelly.github.com/Consuming-Web-APIs-with-Python-Talk/slides/slides.html>`_
|
||||
- `Blog post about Requests via Yum <http://arunsag.wordpress.com/2011/08/17/new-package-python-requests-http-for-humans/>`_
|
||||
- `Russian blog post introducing Requests <http://habrahabr.ru/blogs/python/126262/>`_
|
||||
- `French blog post introducing Requests <http://www.nicosphere.net/requests-urllib2-de-python-simplifie-2432/>`_
|
||||
|
||||
@@ -43,7 +43,7 @@ master_doc = 'index'
|
||||
|
||||
# General information about the project.
|
||||
project = u'Requests'
|
||||
copyright = u'2012. A <a href="http://kennethreitz.com/pages/open-projects.html">Kenneth Reitz</a> Project'
|
||||
copyright = u'2013. A <a href="http://kennethreitz.com/pages/open-projects.html">Kenneth Reitz</a> Project'
|
||||
|
||||
# The version info for the project you're documenting, acts as replacement for
|
||||
# |version| and |release|, also used in various other places throughout the
|
||||
@@ -133,7 +133,6 @@ html_static_path = ['_static']
|
||||
# typographically correct entities.
|
||||
#html_use_smartypants = True
|
||||
|
||||
# Custom sidebar templates, maps document names to template names.
|
||||
# Custom sidebar templates, maps document names to template names.
|
||||
html_sidebars = {
|
||||
'index': ['sidebarintro.html', 'sourcelink.html', 'searchbox.html'],
|
||||
|
||||
41
docs/dev/philosophy.rst
Normal file
41
docs/dev/philosophy.rst
Normal file
@@ -0,0 +1,41 @@
|
||||
Development Philosophy
|
||||
======================
|
||||
|
||||
Requests is an open but opinionated library, created by an open but opinionated developer.
|
||||
|
||||
|
||||
Benevolent Dictator
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
`Kenneth Reitz <http://kennethreitz.org>`_ is the BDFL. He has final say in any decision related to Requests.
|
||||
|
||||
Values
|
||||
~~~~~~
|
||||
|
||||
- Simplicity is always better than functionality.
|
||||
- Listen to everyone, then disregard it.
|
||||
- The API is all that matters. Everything else is secondary.
|
||||
- Fit the 90% use-case. Ignore the nay-sayers.
|
||||
|
||||
Semantic Versioning
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
For many years, the open source community has been plagued with version number dystonia. Numbers vary so greatly from project to project, they are practically meaningless.
|
||||
|
||||
Requests uses `Semantic Versioning <http://semver.org>`_. This specification seeks to put an end to this madness with a small set of practical guidelines for you and your colleagues to use in your next project.
|
||||
|
||||
Standard Library?
|
||||
~~~~~~~~~~~~~~~~~
|
||||
|
||||
Requests has no *active* plans to be included in the standard library. This decision has been discussed at length with Guido as well as numerous core developers.
|
||||
|
||||
Essentially, the standard library is where a library goes to die. It is appropriate for a module to be included when active development is no longer necessary.
|
||||
|
||||
Requests just reached v1.0.0. This huge milestone marks a major step in the right direction.
|
||||
|
||||
Linux Distro Packages
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Distributions have been made for many Linux repositories, including: Ubuntu, Debian, RHEL, and Arch.
|
||||
|
||||
These distributions are sometimes divergent forks, or are otherwise not kept up-to-date with the latest code and bugfixes. PyPI (and its mirrors) and GitHub are the official distribution sources; alternatives are not supported by the requests project.
|
||||
@@ -3,41 +3,53 @@ How to Help
|
||||
|
||||
Requests is under active development, and contributions are more than welcome!
|
||||
|
||||
#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
|
||||
#. Check for open issues or open a fresh issue to start a discussion around a bug.
|
||||
There is a Contributor Friendly tag for issues that should be ideal for people who are not very
|
||||
familiar with the codebase yet.
|
||||
#. Fork `the repository <https://github.com/kennethreitz/requests>`_ on Github to start making your
|
||||
changes to the **develop** branch (or branch off of it).
|
||||
#. Write a test which shows that the bug was fixed or that the feature works as expected.
|
||||
#. Fork `the repository <https://github.com/kennethreitz/requests>`_ on Github andstart making your
|
||||
changes to a new branch.
|
||||
#. Write a test which shows that the bug was fixed.
|
||||
#. Send a pull request and bug the maintainer until it gets merged and published. :)
|
||||
Make sure to add yourself to `AUTHORS <https://github.com/kennethreitz/requests/blob/develop/AUTHORS.rst>`_.
|
||||
Make sure to add yourself to `AUTHORS <https://github.com/kennethreitz/requests/blob/master/AUTHORS.rst>`_.
|
||||
|
||||
Development dependencies
|
||||
Feature Freeze
|
||||
--------------
|
||||
|
||||
As of v1.0.0, Requests has now entered a feature freeze. Requests for new
|
||||
features and Pull Requests implementing those features will not be accepted.
|
||||
|
||||
Development Dependencies
|
||||
------------------------
|
||||
|
||||
You'll need to install ``gunicorn`` and ``httpbin`` and various other dependencies in
|
||||
order to run requests' test suite::
|
||||
You'll need to install py.test in order to run the Requests' test suite::
|
||||
|
||||
$ virtualenv env
|
||||
$ . env/bin/activate
|
||||
$ make
|
||||
$ make test
|
||||
$ pip install -r requirements.txt
|
||||
$ invoke test
|
||||
py.test
|
||||
platform darwin -- Python 2.7.3 -- pytest-2.3.4
|
||||
collected 25 items
|
||||
|
||||
The ``Makefile`` has various useful targets for testing. For example, if you
|
||||
want to see how your pull request will behave with Travis-CI you would run
|
||||
``make travis``.
|
||||
test_requests.py .........................
|
||||
25 passed in 3.50 seconds
|
||||
|
||||
Versions of Python to Test On
|
||||
-----------------------------
|
||||
Runtime Environments
|
||||
--------------------
|
||||
|
||||
Officially (as of 26-Nov-2012), requests supports python 2.6-3.3. In the
|
||||
future, support for 3.1 and 3.2 may be dropped. In general you will need to
|
||||
test on at least one python 2 and one python 3 version. You can also set up
|
||||
Travis CI for your own fork before you submit a pull request so that you are
|
||||
assured your fork works. To use Travis CI for your fork and other projects see
|
||||
their `documentation <http://about.travis-ci.org/docs/user/getting-started/>`_.
|
||||
Requests currently supports the following versions of Python:
|
||||
|
||||
What Needs to be Done
|
||||
---------------------
|
||||
- Python 2.6
|
||||
- Python 2.7
|
||||
- Python 3.1
|
||||
- Python 3.2
|
||||
- Python 3.3
|
||||
- PyPy 1.9
|
||||
|
||||
- Documentation needs a roadmap.
|
||||
Support for Python 3.1 and 3.2 may be dropped at any time.
|
||||
|
||||
Google App Engine will never be officially supported. Pull requests for compatiblity will be accepted, as long as they don't complicate the codebase.
|
||||
|
||||
|
||||
Are you crazy?
|
||||
--------------
|
||||
|
||||
- SPDY support would be awesome. No C extensions.
|
||||
|
||||
@@ -27,7 +27,7 @@ Things shouldn’t be this way. Not in Python.
|
||||
'utf-8'
|
||||
>>> r.text
|
||||
u'{"type":"User"...'
|
||||
>>> r.json
|
||||
>>> r.json()
|
||||
{u'private_gists': 419, u'total_private_repos': 77, ...}
|
||||
|
||||
See `similar code, without Requests <https://gist.github.com/973705>`_.
|
||||
@@ -38,7 +38,7 @@ Requests takes all of the work out of Python HTTP/1.1 — making your integrati
|
||||
Testimonials
|
||||
------------
|
||||
|
||||
Amazon, Google, Twilio, Mozilla, Heroku, PayPal, NPR, Obama for America, Transifex, Native Instruments, The Washington Post, Twitter, SoundCloud, Kippt, Readability, and Federal US Institutions use Requests internally. It has been downloaded over 1,000,000 times from PyPI.
|
||||
Her Majesty's Government, Amazon, Google, Twilio, Mozilla, Heroku, PayPal, NPR, Obama for America, Transifex, Native Instruments, The Washington Post, Twitter, SoundCloud, Kippt, Readability, and Federal US Institutions use Requests internally. It has been downloaded over 2,000,000 times from PyPI.
|
||||
|
||||
**Armin Ronacher**
|
||||
Requests is the perfect example how beautiful an API can be with the
|
||||
@@ -73,6 +73,7 @@ Requests is ready for today's web.
|
||||
- Multipart File Uploads
|
||||
- Connection Timeouts
|
||||
- ``.netrc`` support
|
||||
- Python 2.6—3.3
|
||||
- Thread-safe.
|
||||
|
||||
|
||||
@@ -90,6 +91,7 @@ instructions for getting the most out of Requests.
|
||||
user/install
|
||||
user/quickstart
|
||||
user/advanced
|
||||
user/authentication
|
||||
|
||||
|
||||
Community Guide
|
||||
@@ -118,8 +120,8 @@ this part of the documentation is for you.
|
||||
api
|
||||
|
||||
|
||||
Developer Guide
|
||||
---------------
|
||||
Contributor Guide
|
||||
-----------------
|
||||
|
||||
If you want to contribute to the project, this part of the documentation is for
|
||||
you.
|
||||
@@ -127,6 +129,7 @@ you.
|
||||
.. toctree::
|
||||
:maxdepth: 1
|
||||
|
||||
dev/philosophy
|
||||
dev/internals
|
||||
dev/todo
|
||||
dev/authors
|
||||
|
||||
@@ -17,7 +17,7 @@ A session object has all the methods of the main Requests API.
|
||||
|
||||
Let's persist some cookies across requests::
|
||||
|
||||
s = requests.session()
|
||||
s = requests.Session()
|
||||
|
||||
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
|
||||
r = s.get("http://httpbin.org/cookies")
|
||||
@@ -26,15 +26,15 @@ Let's persist some cookies across requests::
|
||||
# '{"cookies": {"sessioncookie": "123456789"}}'
|
||||
|
||||
|
||||
Sessions can also be used to provide default data to the request methods::
|
||||
Sessions can also be used to provide default data to the request methods. This
|
||||
is done by providing data to the properties on a session object::
|
||||
|
||||
headers = {'x-test': 'true'}
|
||||
auth = ('user', 'pass')
|
||||
s = requests.Session()
|
||||
s.auth = ('user', 'pass')
|
||||
s.headers.update({'x-test': 'true'})
|
||||
|
||||
with requests.session(auth=auth, headers=headers) as c:
|
||||
|
||||
# both 'x-test' and 'x-test2' are sent
|
||||
c.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
|
||||
# both 'x-test' and 'x-test2' are sent
|
||||
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
|
||||
|
||||
|
||||
Any dictionaries that you pass to a request method will be merged with the session-level values that are set. The method-level parameters override session parameters.
|
||||
@@ -56,11 +56,11 @@ contains all of the information returned by the server and also contains the
|
||||
``Request`` object you created originally. Here is a simple request to get some
|
||||
very important information from Wikipedia's servers::
|
||||
|
||||
>>> response = requests.get('http://en.wikipedia.org/wiki/Monty_Python')
|
||||
>>> r = requests.get('http://en.wikipedia.org/wiki/Monty_Python')
|
||||
|
||||
If we want to access the headers the server sent back to us, we do this::
|
||||
|
||||
>>> response.headers
|
||||
>>> r.headers
|
||||
{'content-length': '56170', 'x-content-type-options': 'nosniff', 'x-cache':
|
||||
'HIT from cp1006.eqiad.wmnet, MISS from cp1010.eqiad.wmnet', 'content-encoding':
|
||||
'gzip', 'age': '3080', 'content-language': 'en', 'vary': 'Accept-Encoding,Cookie',
|
||||
@@ -73,7 +73,7 @@ If we want to access the headers the server sent back to us, we do this::
|
||||
However, if we want to get the headers we sent the server, we simply access the
|
||||
request, and then the request's headers::
|
||||
|
||||
>>> response.request.headers
|
||||
>>> r.request.headers
|
||||
{'Accept-Encoding': 'identity, deflate, compress, gzip',
|
||||
'Accept': '*/*', 'User-Agent': 'python-requests/0.13.1'}
|
||||
|
||||
@@ -102,7 +102,7 @@ Requests can also ignore verifying the SSL certficate if you set ``verify`` to F
|
||||
|
||||
By default, ``verify`` is set to True. Option ``verify`` only applies to host certs.
|
||||
|
||||
You can also specify the local cert file either as a path or key value pair::
|
||||
You can also specify a local cert to use as client side certificate, as a single file (containing the private key and the certificate) or as a tuple of both file's path::
|
||||
|
||||
>>> requests.get('https://kennethreitz.com', cert=('/path/server.crt', '/path/key'))
|
||||
<Response [200]>
|
||||
@@ -129,12 +129,6 @@ At this point only the response headers have been downloaded and the connection
|
||||
|
||||
You can further control the workflow by use of the :class:`Response.iter_content` and :class:`Response.iter_lines` methods, or reading from the underlying urllib3 :class:`urllib3.HTTPResponse` at :class:`Response.raw`.
|
||||
|
||||
Configuring Requests
|
||||
--------------------
|
||||
|
||||
Sometimes you may want to configure a request to customize its behavior. To do
|
||||
this, you can pass in a ``config`` dictionary to a request or session. See the :ref:`Configuration API Docs <configurations>` to learn more.
|
||||
|
||||
|
||||
Keep-Alive
|
||||
----------
|
||||
@@ -144,6 +138,28 @@ Excellent news — thanks to urllib3, keep-alive is 100% automatic within a ses
|
||||
Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set ``stream`` to ``False`` or read the ``content`` property of the ``Response`` object.
|
||||
|
||||
|
||||
Streaming Uploads
|
||||
-----------------
|
||||
|
||||
Requests supports streaming uploads, which allow you to send large streams or files without reading them into memory. To stream and upload, simply provide a file-like object for your body::
|
||||
|
||||
with open('massive-body') as f:
|
||||
requests.post('http://some.url/streamed', data=f)
|
||||
|
||||
|
||||
Chunk-Encoded Requests
|
||||
----------------------
|
||||
|
||||
Requests also supports Chunked transfer encoding for outgoing and incoming requests. To send a chunk-encoded request, simply provide a generator (or any iterator without a length) for your body::
|
||||
|
||||
|
||||
def gen():
|
||||
yield 'hi'
|
||||
yield 'there'
|
||||
|
||||
requests.post('http://some.url/chunked', data=gen())
|
||||
|
||||
|
||||
Event Hooks
|
||||
-----------
|
||||
|
||||
@@ -201,6 +217,7 @@ Let's pretend that we have a web service that will only respond if the
|
||||
::
|
||||
|
||||
from requests.auth import AuthBase
|
||||
|
||||
class PizzaAuth(AuthBase):
|
||||
"""Attaches HTTP Pizza Authentication to the given Request object."""
|
||||
def __init__(self, username):
|
||||
@@ -245,8 +262,8 @@ If you need to use a proxy, you can configure individual requests with the
|
||||
import requests
|
||||
|
||||
proxies = {
|
||||
"http": "10.10.1.10:3128",
|
||||
"https": "10.10.1.10:1080",
|
||||
"http": "http://10.10.1.10:3128",
|
||||
"https": "http://10.10.1.10:1080",
|
||||
}
|
||||
|
||||
requests.get("http://example.org", proxies=proxies)
|
||||
@@ -255,8 +272,8 @@ You can also configure proxies by environment variables ``HTTP_PROXY`` and ``HTT
|
||||
|
||||
::
|
||||
|
||||
$ export HTTP_PROXY="10.10.1.10:3128"
|
||||
$ export HTTPS_PROXY="10.10.1.10:1080"
|
||||
$ export HTTP_PROXY="http://10.10.1.10:3128"
|
||||
$ export HTTPS_PROXY="http://10.10.1.10:1080"
|
||||
$ python
|
||||
>>> import requests
|
||||
>>> requests.get("http://example.org")
|
||||
@@ -318,17 +335,12 @@ out what type of content it is. Do this like so::
|
||||
...
|
||||
application/json; charset=utf-8
|
||||
|
||||
So, GitHub returns JSON. That's great, we can use the JSON module to turn it
|
||||
into Python objects. Because GitHub returned UTF-8, we should use the
|
||||
``r.text`` method, not the ``r.content`` method. ``r.content`` returns a
|
||||
bytestring, while ``r.text`` returns a Unicode-encoded string. I have no plans
|
||||
to perform byte-manipulation on this response, so I want any Unicode code
|
||||
points encoded.
|
||||
So, GitHub returns JSON. That's great, we can use the ``r.json`` method to
|
||||
parse it into Python objects.
|
||||
|
||||
::
|
||||
|
||||
>>> import json
|
||||
>>> commit_data = json.loads(r.text)
|
||||
>>> commit_data = r.json()
|
||||
>>> print commit_data.keys()
|
||||
[u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']
|
||||
>>> print commit_data[u'committer']
|
||||
@@ -385,7 +397,7 @@ Cool, we have three comments. Let's take a look at the last of them.
|
||||
>>> r = requests.get(r.url + u'/comments')
|
||||
>>> r.status_code
|
||||
200
|
||||
>>> comments = json.loads(r.text)
|
||||
>>> comments = r.json()
|
||||
>>> print comments[0].keys()
|
||||
[u'body', u'url', u'created_at', u'updated_at', u'user', u'id']
|
||||
>>> print comments[2][u'body']
|
||||
@@ -422,7 +434,7 @@ the very common Basic Auth.
|
||||
>>> r = requests.post(url=url, data=body, auth=auth)
|
||||
>>> r.status_code
|
||||
201
|
||||
>>> content = json.loads(r.text)
|
||||
>>> content = r.json()
|
||||
>>> print content[u'body']
|
||||
Sounds great! I'll get right on it.
|
||||
|
||||
@@ -485,9 +497,9 @@ GitHub uses these for `pagination <http://developer.github.com/v3/#pagination>`_
|
||||
|
||||
Requests will automatically parse these link headers and make them easily consumable::
|
||||
|
||||
>>> r.links['next']
|
||||
'https://api.github.com/users/kennethreitz/repos?page=2&per_page=10'
|
||||
>>> r.links["next"]
|
||||
{'url': 'https://api.github.com/users/kennethreitz/repos?page=2&per_page=10', 'rel': 'next'}
|
||||
|
||||
>>> r.links['last']
|
||||
'https://api.github.com/users/kennethreitz/repos?page=6&per_page=10'
|
||||
>>> r.links["last"]
|
||||
{'url': 'https://api.github.com/users/kennethreitz/repos?page=7&per_page=10', 'rel': 'last'}
|
||||
|
||||
|
||||
84
docs/user/authentication.rst
Normal file
84
docs/user/authentication.rst
Normal file
@@ -0,0 +1,84 @@
|
||||
.. _authentication:
|
||||
|
||||
Authentication
|
||||
==============
|
||||
|
||||
This document discusses using various kinds of authentication with Requests.
|
||||
|
||||
Many web services require authentication, and there are many different types.
|
||||
Below, we outline various forms of authentication available in Requests, from
|
||||
the simple to the complex.
|
||||
|
||||
|
||||
Basic Authentication
|
||||
--------------------
|
||||
|
||||
Many web services that require authentication accept HTTP Basic Auth. This is
|
||||
the simplest kind, and Requests supports it straight out of the box.
|
||||
|
||||
Making requests with HTTP Basic Auth is very simple::
|
||||
|
||||
>>> from requests.auth import HTTPBasicAuth
|
||||
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
|
||||
<Response [200]>
|
||||
|
||||
In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand
|
||||
for using it::
|
||||
|
||||
>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
|
||||
<Response [200]>
|
||||
|
||||
Providing the credentials in a tuple like this is exactly the same as the
|
||||
``HTTPBasicAuth`` example above.
|
||||
|
||||
|
||||
Digest Authentication
|
||||
---------------------
|
||||
|
||||
Another very popular form of HTTP Authentication is Digest Authentication,
|
||||
and Requests supports this out of the box as well::
|
||||
|
||||
>>> from requests.auth import HTTPDigestAuth
|
||||
>>> url = 'http://httpbin.org/digest-auth/auth/user/pass'
|
||||
>>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
|
||||
<Response [200]>
|
||||
|
||||
|
||||
Other Authentication
|
||||
--------------------
|
||||
|
||||
Requests is designed to allow other forms of authentication to be easily and
|
||||
quickly plugged in. Members of the open-source community frequently write
|
||||
authentication handlers for more complicated or less commonly-used forms of
|
||||
authentication. Some of the best have been brought together under the
|
||||
`Requests organization`_, including:
|
||||
|
||||
- OAuth_
|
||||
- Kerberos_
|
||||
- NTLM_
|
||||
|
||||
If you want to use any of these forms of authentication, go straight to their
|
||||
Github page and follow the instructions.
|
||||
|
||||
|
||||
New Forms of Authentication
|
||||
---------------------------
|
||||
|
||||
If you can't find a good implementation of the form of authentication you
|
||||
want, you can implement it yourself. Requests makes it easy to add your own
|
||||
forms of authentication.
|
||||
|
||||
To do so, subclass :class:`requests.auth.AuthBase` and implement the
|
||||
``__call__()`` method. When an authentication handler is attached to a request,
|
||||
it is called during request setup. The ``__call__`` method must therefore do
|
||||
whatever is required to make the authentication work. Some forms of
|
||||
authentication will additionally add hooks to provide further functionality.
|
||||
|
||||
Examples can be found under the `Requests organization`_ and in the
|
||||
``auth.py`` file.
|
||||
|
||||
.. _OAuth: https://github.com/requests/requests-oauthlib
|
||||
.. _Kerberos: https://github.com/requests/requests-kerberos
|
||||
.. _NTLM: https://github.com/requests/requests-ntlm
|
||||
.. _Requests organization: https://github.com/requests
|
||||
|
||||
@@ -19,7 +19,7 @@ Let's get started with some simple examples.
|
||||
|
||||
|
||||
Make a Request
|
||||
------------------
|
||||
--------------
|
||||
|
||||
Making a request with Requests is very simple.
|
||||
|
||||
@@ -128,10 +128,10 @@ There's also a builtin JSON decoder, in case you're dealing with JSON data::
|
||||
|
||||
>>> import requests
|
||||
>>> r = requests.get('https://github.com/timeline.json')
|
||||
>>> r.json
|
||||
>>> r.json()
|
||||
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
|
||||
|
||||
In case the JSON decoding fails, ``r.json`` simply returns ``None``.
|
||||
In case the JSON decoding fails, ``r.json`` raises an exception.
|
||||
|
||||
|
||||
Raw Response Content
|
||||
@@ -141,7 +141,7 @@ In the rare case that you'd like to get the raw socket response from the
|
||||
server, you can access ``r.raw``. If you want to do this, make sure you set
|
||||
``stream=True`` in your initial request. Once you do, you can do this::
|
||||
|
||||
>>> r = requests.get('https:/github.com/timeline.json', stream=True)
|
||||
>>> r = requests.get('https://github.com/timeline.json', stream=True)
|
||||
>>> r.raw
|
||||
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
|
||||
>>> r.raw.read(10)
|
||||
@@ -175,12 +175,12 @@ dictionary of data will automatically be form-encoded when the request is made::
|
||||
>>> r = requests.post("http://httpbin.org/post", data=payload)
|
||||
>>> print r.text
|
||||
{
|
||||
// ...snip... //
|
||||
...
|
||||
"form": {
|
||||
"key2": "value2",
|
||||
"key1": "value1"
|
||||
},
|
||||
// ...snip... //
|
||||
...
|
||||
}
|
||||
|
||||
There are many times that you want to send data that is not form-encoded. If you pass in a ``string`` instead of a ``dict``, that data will be posted directly.
|
||||
@@ -205,11 +205,11 @@ Requests makes it simple to upload Multipart-encoded files::
|
||||
>>> r = requests.post(url, files=files)
|
||||
>>> r.text
|
||||
{
|
||||
// ...snip... //
|
||||
...
|
||||
"files": {
|
||||
"file": "<censored...binary...data>"
|
||||
},
|
||||
// ...snip... //
|
||||
...
|
||||
}
|
||||
|
||||
You can set the filename explicitly::
|
||||
@@ -220,11 +220,11 @@ You can set the filename explicitly::
|
||||
>>> r = requests.post(url, files=files)
|
||||
>>> r.text
|
||||
{
|
||||
// ...snip... //
|
||||
...
|
||||
"files": {
|
||||
"file": "<censored...binary...data>"
|
||||
},
|
||||
// ...snip... //
|
||||
...
|
||||
}
|
||||
|
||||
If you want, you can send strings to be received as files::
|
||||
@@ -235,11 +235,11 @@ If you want, you can send strings to be received as files::
|
||||
>>> r = requests.post(url, files=files)
|
||||
>>> r.text
|
||||
{
|
||||
// ...snip... //
|
||||
...
|
||||
"files": {
|
||||
"file": "some,data,to,send\\nanother,row,to,send\\n"
|
||||
},
|
||||
// ...snip... //
|
||||
...
|
||||
}
|
||||
|
||||
|
||||
@@ -287,7 +287,6 @@ We can view the server's response headers using a Python dictionary::
|
||||
|
||||
>>> r.headers
|
||||
{
|
||||
'status': '200 OK',
|
||||
'content-encoding': 'gzip',
|
||||
'transfer-encoding': 'chunked',
|
||||
'connection': 'close',
|
||||
@@ -320,11 +319,11 @@ Cookies
|
||||
|
||||
If a response contains some Cookies, you can get quick access to them::
|
||||
|
||||
>>> url = 'http://httpbin.org/cookies/set/requests-is/awesome'
|
||||
>>> url = 'http://example.com/some/cookie/setting/url'
|
||||
>>> r = requests.get(url)
|
||||
|
||||
>>> r.cookies['requests-is']
|
||||
'awesome'
|
||||
>>> r.cookies['example_cookie_name']
|
||||
'example_cookie_value'
|
||||
|
||||
To send your own cookies to the server, you can use the ``cookies``
|
||||
parameter::
|
||||
@@ -337,80 +336,6 @@ parameter::
|
||||
'{"cookies": {"cookies_are": "working"}}'
|
||||
|
||||
|
||||
Basic Authentication
|
||||
--------------------
|
||||
|
||||
Many web services require authentication. There are many different types of
|
||||
authentication, but the most common is HTTP Basic Auth.
|
||||
|
||||
Making requests with Basic Auth is extremely simple::
|
||||
|
||||
>>> from requests.auth import HTTPBasicAuth
|
||||
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
|
||||
<Response [200]>
|
||||
|
||||
Due to the prevalence of HTTP Basic Auth, requests provides a shorthand for
|
||||
this authentication method::
|
||||
|
||||
>>> requests.get('https://api.github.com/user', auth=('user', 'pass'))
|
||||
<Response [200]>
|
||||
|
||||
Providing the credentials as a tuple in this fashion is functionally equivalent
|
||||
to the ``HTTPBasicAuth`` example above.
|
||||
|
||||
|
||||
Digest Authentication
|
||||
---------------------
|
||||
|
||||
Another popular form of web service protection is Digest Authentication::
|
||||
|
||||
>>> from requests.auth import HTTPDigestAuth
|
||||
>>> url = 'http://httpbin.org/digest-auth/auth/user/pass'
|
||||
>>> requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
|
||||
<Response [200]>
|
||||
|
||||
|
||||
OAuth Authentication
|
||||
--------------------
|
||||
|
||||
Requests features robust, built-in OAuth support!
|
||||
|
||||
OAuth takes many forms, so let's take a look at a few different forms::
|
||||
|
||||
import requests
|
||||
from requests.auth import OAuth1
|
||||
|
||||
url = u'https://api.twitter.com/1/account/settings.json'
|
||||
|
||||
client_key = u'...'
|
||||
client_secret = u'...'
|
||||
resource_owner_key = u'...'
|
||||
resource_owner_secret = u'...'
|
||||
|
||||
|
||||
Query signing::
|
||||
|
||||
queryoauth = OAuth1(client_key, client_secret,
|
||||
resource_owner_key, resource_owner_secret,
|
||||
signature_type='query')
|
||||
r = requests.get(url, auth=queryoauth)
|
||||
|
||||
Header signing::
|
||||
|
||||
headeroauth = OAuth1(client_key, client_secret,
|
||||
resource_owner_key, resource_owner_secret,
|
||||
signature_type='auth_header')
|
||||
r = requests.get(url, auth=headeroauth)
|
||||
|
||||
Body signing::
|
||||
|
||||
bodyoauth = OAuth1(client_key, client_secret,
|
||||
resource_owner_key, resource_owner_secret,
|
||||
signature_type='body')
|
||||
|
||||
r = requests.post(url, auth=bodyoauth)
|
||||
|
||||
|
||||
Redirection and History
|
||||
-----------------------
|
||||
|
||||
@@ -459,7 +384,8 @@ seconds with the ``timeout`` parameter::
|
||||
>>> requests.get('http://github.com', timeout=0.001)
|
||||
Traceback (most recent call last):
|
||||
File "<stdin>", line 1, in <module>
|
||||
requests.exceptions.Timeout: Request timed out.
|
||||
requests.exceptions.Timeout: HTTPConnectionPool(host='github.com', port=80): Request timed out. (timeout=0.001)
|
||||
|
||||
|
||||
.. admonition:: Note:
|
||||
|
||||
@@ -484,12 +410,6 @@ If a request exceeds the configured number of maximum redirections, a
|
||||
All exceptions that Requests explicitly raises inherit from
|
||||
:class:`requests.exceptions.RequestException`.
|
||||
|
||||
You can refer to :ref:`Configuration API Docs <configurations>` for immediate
|
||||
raising of :class:`HTTPError` exceptions via the ``danger_mode`` option or
|
||||
have Requests catch the majority of
|
||||
:class:`requests.exceptions.RequestException` exceptions with the ``safe_mode``
|
||||
option.
|
||||
|
||||
-----------------------
|
||||
|
||||
Ready for more? Check out the :ref:`advanced <advanced>` section.
|
||||
|
||||
5
invokefile.py
Normal file
5
invokefile.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from invoke import run, task
|
||||
|
||||
@task
|
||||
def build():
|
||||
print("Building!")
|
||||
@@ -36,17 +36,17 @@ usage:
|
||||
The other HTTP methods are supported - see `requests.api`. Full documentation
|
||||
is at <http://python-requests.org>.
|
||||
|
||||
:copyright: (c) 2012 by Kenneth Reitz.
|
||||
:copyright: (c) 2013 by Kenneth Reitz.
|
||||
:license: Apache 2.0, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'requests'
|
||||
__version__ = '1.0.1'
|
||||
__build__ = 0x01001
|
||||
__version__ = '1.2.0'
|
||||
__build__ = 0x010200
|
||||
__author__ = 'Kenneth Reitz'
|
||||
__license__ = 'Apache 2.0'
|
||||
__copyright__ = 'Copyright 2012 Kenneth Reitz'
|
||||
__copyright__ = 'Copyright 2013 Kenneth Reitz'
|
||||
|
||||
|
||||
from . import utils
|
||||
|
||||
@@ -8,15 +8,14 @@ This module contains the transport adapters that Requests uses to define
|
||||
and maintain connections.
|
||||
"""
|
||||
|
||||
import os
|
||||
import socket
|
||||
|
||||
from .models import Response
|
||||
from .auth import HTTPProxyAuth
|
||||
from .packages.urllib3.poolmanager import PoolManager, ProxyManager
|
||||
from .hooks import dispatch_hook
|
||||
from .compat import urlparse
|
||||
from .utils import DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers
|
||||
from .packages.urllib3.response import HTTPResponse
|
||||
from .compat import urlparse, basestring, urldefrag, unquote
|
||||
from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,
|
||||
prepend_scheme_if_needed, get_auth_from_url)
|
||||
from .structures import CaseInsensitiveDict
|
||||
from .packages.urllib3.exceptions import MaxRetryError
|
||||
from .packages.urllib3.exceptions import TimeoutError
|
||||
@@ -24,6 +23,7 @@ from .packages.urllib3.exceptions import SSLError as _SSLError
|
||||
from .packages.urllib3.exceptions import HTTPError as _HTTPError
|
||||
from .cookies import extract_cookies_to_jar
|
||||
from .exceptions import ConnectionError, Timeout, SSLError
|
||||
from .auth import _basic_auth_str
|
||||
|
||||
DEFAULT_POOLSIZE = 10
|
||||
DEFAULT_RETRIES = 0
|
||||
@@ -44,15 +44,34 @@ class BaseAdapter(object):
|
||||
|
||||
class HTTPAdapter(BaseAdapter):
|
||||
"""Built-In HTTP Adapter for Urllib3."""
|
||||
__attrs__ = ['max_retries', 'config', '_pool_connections', '_pool_maxsize']
|
||||
|
||||
def __init__(self, pool_connections=DEFAULT_POOLSIZE, pool_maxsize=DEFAULT_POOLSIZE):
|
||||
self.max_retries = DEFAULT_RETRIES
|
||||
self.config = {}
|
||||
|
||||
super(HTTPAdapter, self).__init__()
|
||||
|
||||
self._pool_connections = pool_connections
|
||||
self._pool_maxsize = pool_maxsize
|
||||
|
||||
self.init_poolmanager(pool_connections, pool_maxsize)
|
||||
|
||||
def __getstate__(self):
|
||||
return dict((attr, getattr(self, attr, None)) for attr in
|
||||
self.__attrs__)
|
||||
|
||||
def __setstate__(self, state):
|
||||
for attr, value in state.items():
|
||||
setattr(self, attr, value)
|
||||
|
||||
self.init_poolmanager(self._pool_connections, self._pool_maxsize)
|
||||
|
||||
def init_poolmanager(self, connections, maxsize):
|
||||
# save these values for pickling
|
||||
self._pool_connections = connections
|
||||
self._pool_maxsize = maxsize
|
||||
|
||||
self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize)
|
||||
|
||||
def cert_verify(self, conn, url, verify, cert):
|
||||
@@ -77,13 +96,12 @@ class HTTPAdapter(BaseAdapter):
|
||||
conn.ca_certs = None
|
||||
|
||||
if cert:
|
||||
if len(cert) == 2:
|
||||
if not isinstance(cert, basestring):
|
||||
conn.cert_file = cert[0]
|
||||
conn.key_file = cert[1]
|
||||
else:
|
||||
conn.cert_file = cert
|
||||
|
||||
|
||||
def build_response(self, req, resp):
|
||||
response = Response()
|
||||
|
||||
@@ -96,6 +114,7 @@ class HTTPAdapter(BaseAdapter):
|
||||
# Set encoding.
|
||||
response.encoding = get_encoding_from_headers(response.headers)
|
||||
response.raw = resp
|
||||
response.reason = response.raw.reason
|
||||
|
||||
if isinstance(req.url, bytes):
|
||||
response.url = req.url.decode('utf-8')
|
||||
@@ -109,8 +128,6 @@ class HTTPAdapter(BaseAdapter):
|
||||
response.request = req
|
||||
response.connection = self
|
||||
|
||||
# Run the Response hook.
|
||||
response = dispatch_hook('response', req.hooks, response)
|
||||
return response
|
||||
|
||||
def get_connection(self, url, proxies=None):
|
||||
@@ -119,13 +136,13 @@ class HTTPAdapter(BaseAdapter):
|
||||
proxy = proxies.get(urlparse(url).scheme)
|
||||
|
||||
if proxy:
|
||||
conn = ProxyManager(self.poolmanager.proxy_from_url(proxy))
|
||||
proxy = prepend_scheme_if_needed(proxy, urlparse(url).scheme)
|
||||
conn = ProxyManager(self.poolmanager.connection_from_url(proxy))
|
||||
else:
|
||||
conn = self.poolmanager.connection_from_url(url)
|
||||
|
||||
return conn
|
||||
|
||||
|
||||
def close(self):
|
||||
"""Dispose of any internal state.
|
||||
|
||||
@@ -134,27 +151,93 @@ class HTTPAdapter(BaseAdapter):
|
||||
"""
|
||||
self.poolmanager.clear()
|
||||
|
||||
def request_url(self, request, proxies):
|
||||
"""Obtain the url to use when making the final request.
|
||||
|
||||
If the message is being sent through a proxy, the full URL has to be
|
||||
used. Otherwise, we should only use the path portion of the URL."""
|
||||
proxies = proxies or {}
|
||||
proxy = proxies.get(urlparse(request.url).scheme)
|
||||
|
||||
if proxy:
|
||||
url, _ = urldefrag(request.url)
|
||||
else:
|
||||
url = request.path_url
|
||||
|
||||
return url
|
||||
|
||||
def add_headers(self, request, **kwargs):
|
||||
"""Add any headers needed by the connection. Currently this adds a
|
||||
Proxy-Authorization header."""
|
||||
proxies = kwargs.get('proxies', {})
|
||||
|
||||
if proxies is None:
|
||||
proxies = {}
|
||||
|
||||
proxy = proxies.get(urlparse(request.url).scheme)
|
||||
username, password = get_auth_from_url(proxy)
|
||||
|
||||
if username and password:
|
||||
# Proxy auth usernames and passwords will be urlencoded, we need
|
||||
# to decode them.
|
||||
username = unquote(username)
|
||||
password = unquote(password)
|
||||
request.headers['Proxy-Authorization'] = _basic_auth_str(username,
|
||||
password)
|
||||
|
||||
def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
|
||||
"""Sends PreparedRequest object. Returns Response object."""
|
||||
|
||||
conn = self.get_connection(request.url, proxies)
|
||||
|
||||
self.cert_verify(conn, request.url, verify, cert)
|
||||
url = self.request_url(request, proxies)
|
||||
self.add_headers(request, proxies=proxies)
|
||||
|
||||
chunked = not (request.body is None or 'Content-Length' in request.headers)
|
||||
|
||||
try:
|
||||
if not chunked:
|
||||
resp = conn.urlopen(
|
||||
method=request.method,
|
||||
url=url,
|
||||
body=request.body,
|
||||
headers=request.headers,
|
||||
redirect=False,
|
||||
assert_same_host=False,
|
||||
preload_content=False,
|
||||
decode_content=False,
|
||||
retries=self.max_retries,
|
||||
timeout=timeout
|
||||
)
|
||||
|
||||
# Send the request.
|
||||
resp = conn.urlopen(
|
||||
method=request.method,
|
||||
url=request.path_url,
|
||||
body=request.body,
|
||||
headers=request.headers,
|
||||
redirect=False,
|
||||
assert_same_host=False,
|
||||
preload_content=False,
|
||||
decode_content=False,
|
||||
retries=self.max_retries,
|
||||
timeout=timeout,
|
||||
)
|
||||
else:
|
||||
if hasattr(conn, 'proxy_pool'):
|
||||
conn = conn.proxy_pool
|
||||
|
||||
low_conn = conn._get_conn(timeout=timeout)
|
||||
low_conn.putrequest(request.method, url, skip_accept_encoding=True)
|
||||
|
||||
for header, value in request.headers.items():
|
||||
low_conn.putheader(header, value)
|
||||
|
||||
low_conn.endheaders()
|
||||
|
||||
for i in request.body:
|
||||
low_conn.send(hex(len(i))[2:].encode('utf-8'))
|
||||
low_conn.send(b'\r\n')
|
||||
low_conn.send(i)
|
||||
low_conn.send(b'\r\n')
|
||||
low_conn.send(b'0\r\n\r\n')
|
||||
|
||||
r = low_conn.getresponse()
|
||||
resp = HTTPResponse.from_httplib(r,
|
||||
pool=conn,
|
||||
connection=low_conn,
|
||||
preload_content=False,
|
||||
decode_content=False
|
||||
)
|
||||
|
||||
except socket.error as sockerr:
|
||||
raise ConnectionError(sockerr)
|
||||
@@ -168,7 +251,7 @@ class HTTPAdapter(BaseAdapter):
|
||||
elif isinstance(e, TimeoutError):
|
||||
raise Timeout(e)
|
||||
else:
|
||||
raise Timeout('Request timed out.')
|
||||
raise
|
||||
|
||||
r = self.build_response(request, resp)
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ def request(method, url, **kwargs):
|
||||
: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 bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
|
||||
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
|
||||
:param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
|
||||
@@ -32,6 +32,12 @@ def request(method, url, **kwargs):
|
||||
:param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
|
||||
: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.
|
||||
|
||||
Usage::
|
||||
|
||||
>>> import requests
|
||||
>>> req = requests.request('GET', 'http://httpbin.org/get')
|
||||
<Response [200]>
|
||||
"""
|
||||
|
||||
session = sessions.Session()
|
||||
@@ -67,6 +73,7 @@ def head(url, **kwargs):
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
kwargs.setdefault('allow_redirects', False)
|
||||
return request('head', url, **kwargs)
|
||||
|
||||
|
||||
@@ -74,7 +81,7 @@ def post(url, data=None, **kwargs):
|
||||
"""Sends a POST request. Returns :class:`Response` object.
|
||||
|
||||
:param url: URL for the new :class:`Request` object.
|
||||
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
@@ -85,7 +92,7 @@ def put(url, data=None, **kwargs):
|
||||
"""Sends a PUT request. Returns :class:`Response` object.
|
||||
|
||||
:param url: URL for the new :class:`Request` object.
|
||||
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
@@ -96,7 +103,7 @@ def patch(url, data=None, **kwargs):
|
||||
"""Sends a PATCH request. Returns :class:`Response` object.
|
||||
|
||||
:param url: URL for the new :class:`Request` object.
|
||||
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ class AuthBase(object):
|
||||
def __call__(self, r):
|
||||
raise NotImplementedError('Auth hooks must be callable.')
|
||||
|
||||
|
||||
class HTTPBasicAuth(AuthBase):
|
||||
"""Attaches HTTP Basic Authentication to the given Request object."""
|
||||
def __init__(self, username, password):
|
||||
@@ -68,18 +69,21 @@ class HTTPDigestAuth(AuthBase):
|
||||
realm = self.chal['realm']
|
||||
nonce = self.chal['nonce']
|
||||
qop = self.chal.get('qop')
|
||||
algorithm = self.chal.get('algorithm', 'MD5')
|
||||
opaque = self.chal.get('opaque', None)
|
||||
algorithm = self.chal.get('algorithm')
|
||||
opaque = self.chal.get('opaque')
|
||||
|
||||
algorithm = algorithm.upper()
|
||||
if algorithm is None:
|
||||
_algorithm = 'MD5'
|
||||
else:
|
||||
_algorithm = algorithm.upper()
|
||||
# lambdas assume digest modules are imported at the top level
|
||||
if algorithm == 'MD5':
|
||||
if _algorithm == 'MD5':
|
||||
def md5_utf8(x):
|
||||
if isinstance(x, str):
|
||||
x = x.encode('utf-8')
|
||||
return hashlib.md5(x).hexdigest()
|
||||
hash_utf8 = md5_utf8
|
||||
elif algorithm == 'SHA':
|
||||
elif _algorithm == 'SHA':
|
||||
def sha_utf8(x):
|
||||
if isinstance(x, str):
|
||||
x = x.encode('utf-8')
|
||||
@@ -126,25 +130,27 @@ class HTTPDigestAuth(AuthBase):
|
||||
|
||||
# XXX should the partial digests be encoded too?
|
||||
base = 'username="%s", realm="%s", nonce="%s", uri="%s", ' \
|
||||
'response="%s"' % (self.username, realm, nonce, path, respdig)
|
||||
'response="%s"' % (self.username, realm, nonce, path, respdig)
|
||||
if opaque:
|
||||
base += ', opaque="%s"' % opaque
|
||||
if algorithm:
|
||||
base += ', algorithm="%s"' % algorithm
|
||||
if entdig:
|
||||
base += ', digest="%s"' % entdig
|
||||
base += ', algorithm="%s"' % algorithm
|
||||
if qop:
|
||||
base += ', qop=auth, nc=%s, cnonce="%s"' % (ncvalue, cnonce)
|
||||
|
||||
return 'Digest %s' % (base)
|
||||
|
||||
def handle_401(self, r):
|
||||
def handle_401(self, r, **kwargs):
|
||||
"""Takes the given response and tries digest-auth, if needed."""
|
||||
|
||||
num_401_calls = r.request.hooks['response'].count(self.handle_401)
|
||||
num_401_calls = getattr(self, 'num_401_calls', 1)
|
||||
s_auth = r.headers.get('www-authenticate', '')
|
||||
|
||||
if 'digest' in s_auth.lower() and num_401_calls < 2:
|
||||
|
||||
setattr(self, 'num_401_calls', num_401_calls + 1)
|
||||
self.chal = parse_dict_header(s_auth.replace('Digest ', ''))
|
||||
|
||||
# Consume content and release the original connection
|
||||
@@ -153,11 +159,12 @@ class HTTPDigestAuth(AuthBase):
|
||||
r.raw.release_conn()
|
||||
|
||||
r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url)
|
||||
_r = r.connection.send(r.request)
|
||||
_r = r.connection.send(r.request, **kwargs)
|
||||
_r.history.append(r)
|
||||
|
||||
return _r
|
||||
|
||||
setattr(self, 'num_401_calls', 1)
|
||||
return r
|
||||
|
||||
def __call__(self, r):
|
||||
@@ -165,4 +172,4 @@ class HTTPDigestAuth(AuthBase):
|
||||
if self.last_nonce:
|
||||
r.headers['Authorization'] = self.build_digest_header(r.method, r.url)
|
||||
r.register_hook('response', self.handle_401)
|
||||
return r
|
||||
return r
|
||||
|
||||
@@ -1603,54 +1603,6 @@ vFcj4jjSm2jzVhKIT0J8uDHEtdvkyCE06UgRNe76x5JXxZ805Mf29w4LTJxoeHtxMcfrHuBnQfO3
|
||||
oKfN5XozNmr6mis=
|
||||
-----END CERTIFICATE-----
|
||||
|
||||
TURKTRUST Certificate Services Provider Root 1
|
||||
==============================================
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIID+zCCAuOgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBtzE/MD0GA1UEAww2VMOcUktUUlVTVCBF
|
||||
bGVrdHJvbmlrIFNlcnRpZmlrYSBIaXptZXQgU2HEn2xhecSxY8Sxc8SxMQswCQYDVQQGDAJUUjEP
|
||||
MA0GA1UEBwwGQU5LQVJBMVYwVAYDVQQKDE0oYykgMjAwNSBUw5xSS1RSVVNUIEJpbGdpIMSwbGV0
|
||||
acWfaW0gdmUgQmlsacWfaW0gR8O8dmVubGnEn2kgSGl6bWV0bGVyaSBBLsWeLjAeFw0wNTA1MTMx
|
||||
MDI3MTdaFw0xNTAzMjIxMDI3MTdaMIG3MT8wPQYDVQQDDDZUw5xSS1RSVVNUIEVsZWt0cm9uaWsg
|
||||
U2VydGlmaWthIEhpem1ldCBTYcSfbGF5xLFjxLFzxLExCzAJBgNVBAYMAlRSMQ8wDQYDVQQHDAZB
|
||||
TktBUkExVjBUBgNVBAoMTShjKSAyMDA1IFTDnFJLVFJVU1QgQmlsZ2kgxLBsZXRpxZ9pbSB2ZSBC
|
||||
aWxpxZ9pbSBHw7x2ZW5sacSfaSBIaXptZXRsZXJpIEEuxZ4uMIIBIjANBgkqhkiG9w0BAQEFAAOC
|
||||
AQ8AMIIBCgKCAQEAylIF1mMD2Bxf3dJ7XfIMYGFbazt0K3gNfUW9InTojAPBxhEqPZW8qZSwu5GX
|
||||
yGl8hMW0kWxsE2qkVa2kheiVfrMArwDCBRj1cJ02i67L5BuBf5OI+2pVu32Fks66WJ/bMsW9Xe8i
|
||||
Si9BB35JYbOG7E6mQW6EvAPs9TscyB/C7qju6hJKjRTP8wrgUDn5CDX4EVmt5yLqS8oUBt5CurKZ
|
||||
8y1UiBAG6uEaPj1nH/vO+3yC6BFdSsG5FOpU2WabfIl9BJpiyelSPJ6c79L1JuTm5Rh8i27fbMx4
|
||||
W09ysstcP4wFjdFMjK2Sx+F4f2VsSQZQLJ4ywtdKxnWKWU51b0dewQIDAQABoxAwDjAMBgNVHRME
|
||||
BTADAQH/MA0GCSqGSIb3DQEBBQUAA4IBAQAV9VX/N5aAWSGk/KEVTCD21F/aAyT8z5Aa9CEKmu46
|
||||
sWrv7/hg0Uw2ZkUd82YCdAR7kjCo3gp2D++Vbr3JN+YaDayJSFvMgzbC9UZcWYJWtNX+I7TYVBxE
|
||||
q8Sn5RTOPEFhfEPmzcSBCYsk+1Ql1haolgxnB2+zUEfjHCQo3SqYpGH+2+oSN7wBGjSFvW5P55Fy
|
||||
B0SFHljKVETd96y5y4khctuPwGkplyqjrhgjlxxBKot8KsF8kOipKMDTkcatKIdAaLX/7KfS0zgY
|
||||
nNN9aV3wxqUeJBujR/xpB2jn5Jq07Q+hh4cCzofSSE7hvP/L8XKSRGQDJereW26fyfJOrN3H
|
||||
-----END CERTIFICATE-----
|
||||
|
||||
TURKTRUST Certificate Services Provider Root 2
|
||||
==============================================
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEPDCCAySgAwIBAgIBATANBgkqhkiG9w0BAQUFADCBvjE/MD0GA1UEAww2VMOcUktUUlVTVCBF
|
||||
bGVrdHJvbmlrIFNlcnRpZmlrYSBIaXptZXQgU2HEn2xhecSxY8Sxc8SxMQswCQYDVQQGEwJUUjEP
|
||||
MA0GA1UEBwwGQW5rYXJhMV0wWwYDVQQKDFRUw5xSS1RSVVNUIEJpbGdpIMSwbGV0acWfaW0gdmUg
|
||||
QmlsacWfaW0gR8O8dmVubGnEn2kgSGl6bWV0bGVyaSBBLsWeLiAoYykgS2FzxLFtIDIwMDUwHhcN
|
||||
MDUxMTA3MTAwNzU3WhcNMTUwOTE2MTAwNzU3WjCBvjE/MD0GA1UEAww2VMOcUktUUlVTVCBFbGVr
|
||||
dHJvbmlrIFNlcnRpZmlrYSBIaXptZXQgU2HEn2xhecSxY8Sxc8SxMQswCQYDVQQGEwJUUjEPMA0G
|
||||
A1UEBwwGQW5rYXJhMV0wWwYDVQQKDFRUw5xSS1RSVVNUIEJpbGdpIMSwbGV0acWfaW0gdmUgQmls
|
||||
acWfaW0gR8O8dmVubGnEn2kgSGl6bWV0bGVyaSBBLsWeLiAoYykgS2FzxLFtIDIwMDUwggEiMA0G
|
||||
CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCpNn7DkUNMwxmYCMjHWHtPFoylzkkBH3MOrHUTpvqe
|
||||
LCDe2JAOCtFp0if7qnefJ1Il4std2NiDUBd9irWCPwSOtNXwSadktx4uXyCcUHVPr+G1QRT0mJKI
|
||||
x+XlZEdhR3n9wFHxwZnn3M5q+6+1ATDcRhzviuyV79z/rxAc653YsKpqhRgNF8k+v/Gb0AmJQv2g
|
||||
QrSdiVFVKc8bcLyEVK3BEx+Y9C52YItdP5qtygy/p1Zbj3e41Z55SZI/4PGXJHpsmxcPbe9TmJEr
|
||||
5A++WXkHeLuXlfSfadRYhwqp48y2WBmfJiGxxFmNskF1wK1pzpwACPI2/z7woQ8arBT9pmAPAgMB
|
||||
AAGjQzBBMB0GA1UdDgQWBBTZN7NOBf3Zz58SFq62iS/rJTqIHDAPBgNVHQ8BAf8EBQMDBwYAMA8G
|
||||
A1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAHJglrfJ3NgpXiOFX7KzLXb7iNcX/ntt
|
||||
Rbj2hWyfIvwqECLsqrkw9qtY1jkQMZkpAL2JZkH7dN6RwRgLn7Vhy506vvWolKMiVW4XSf/SKfE4
|
||||
Jl3vpao6+XF75tpYHdN0wgH6PmlYX63LaL4ULptswLbcoCb6dxriJNoaN+BnrdFzgw2lGh1uEpJ+
|
||||
hGIAF728JRhX8tepb1mIvDS3LoV4nZbcFMMsilKbloxSZj2GFotHuFEJjOp9zYhys2AzsfAKRO8P
|
||||
9Qk3iCQOLGsgOqL6EfJANZxEaGM7rDNvY7wsu/LSy3Z9fYjYHcgFHW68lKlmjHdxx/qR+i9Rnuk5
|
||||
UrbnBEI=
|
||||
-----END CERTIFICATE-----
|
||||
|
||||
SwissSign Gold CA - G2
|
||||
======================
|
||||
-----BEGIN CERTIFICATE-----
|
||||
|
||||
@@ -2,26 +2,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
ceritfi.py
|
||||
~~~~~~~~~~
|
||||
certs.py
|
||||
~~~~~~~~
|
||||
|
||||
This module returns the installation location of cacert.pem.
|
||||
This module returns the preferred default CA certificate bundle.
|
||||
|
||||
If you are packaging Requests, e.g., for a Linux distribution or a managed
|
||||
environment, you can change the definition of where() to return a separately
|
||||
packaged CA bundle.
|
||||
"""
|
||||
|
||||
import os
|
||||
try:
|
||||
import certifi
|
||||
except ImportError:
|
||||
certifi = None
|
||||
import os.path
|
||||
|
||||
|
||||
def where():
|
||||
|
||||
if certifi:
|
||||
return certifi.where()
|
||||
else:
|
||||
f = os.path.split(__file__)[0]
|
||||
return os.path.join(f, 'cacert.pem')
|
||||
"""Return the preferred certificate bundle."""
|
||||
# vendored bundle inside Requests
|
||||
return os.path.join(os.path.dirname(__file__), 'cacert.pem')
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(where())
|
||||
|
||||
@@ -98,7 +98,6 @@ if is_py2:
|
||||
numeric_types = (int, long, float)
|
||||
|
||||
|
||||
|
||||
elif is_py3:
|
||||
from urllib.parse import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, quote_plus, unquote_plus, urldefrag
|
||||
from urllib.request import parse_http_list
|
||||
|
||||
@@ -240,18 +240,28 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
||||
"""Dict-like __getitem__() for compatibility with client code. Throws exception
|
||||
if there are more than one cookie with name. In that case, use the more
|
||||
explicit get() method instead. Caution: operation is O(n), not O(1)."""
|
||||
|
||||
return self._find_no_duplicates(name)
|
||||
|
||||
def __setitem__(self, name, value):
|
||||
"""Dict-like __setitem__ for compatibility with client code. Throws exception
|
||||
if there is already a cookie of that name in the jar. In that case, use the more
|
||||
explicit set() method instead."""
|
||||
|
||||
self.set(name, value)
|
||||
|
||||
def __delitem__(self, name):
|
||||
"""Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""
|
||||
remove_cookie_by_name(self, name)
|
||||
|
||||
def update(self, other):
|
||||
"""Updates this jar with cookies from another CookieJar or dict-like"""
|
||||
if isinstance(other, cookielib.CookieJar):
|
||||
for cookie in other:
|
||||
self.set_cookie(cookie)
|
||||
else:
|
||||
super(RequestsCookieJar, self).update(other)
|
||||
|
||||
def _find(self, name, domain=None, path=None):
|
||||
"""Requests uses this method internally to get cookie values. Takes as args name
|
||||
and optional domain and path. Returns a cookie.value. If there are conflicting cookies,
|
||||
@@ -297,8 +307,10 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
||||
self._cookies_lock = threading.RLock()
|
||||
|
||||
def copy(self):
|
||||
"""This is not implemented. Calling this will throw an exception."""
|
||||
raise NotImplementedError
|
||||
"""Return a copy of this RequestsCookieJar."""
|
||||
new_cj = RequestsCookieJar()
|
||||
new_cj.update(self)
|
||||
return new_cj
|
||||
|
||||
|
||||
def create_cookie(name, value, **kwargs):
|
||||
|
||||
@@ -16,7 +16,11 @@ class RequestException(RuntimeError):
|
||||
|
||||
class HTTPError(RequestException):
|
||||
"""An HTTP error occurred."""
|
||||
response = None
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
""" Initializes HTTPError with optional `response` object. """
|
||||
self.response = kwargs.pop('response', None)
|
||||
super(HTTPError, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
class ConnectionError(RequestException):
|
||||
|
||||
@@ -16,6 +16,7 @@ Available hooks:
|
||||
|
||||
HOOKS = ['response']
|
||||
|
||||
|
||||
def default_hooks():
|
||||
hooks = {}
|
||||
for event in HOOKS:
|
||||
@@ -24,7 +25,8 @@ def default_hooks():
|
||||
|
||||
# TODO: response is the only one
|
||||
|
||||
def dispatch_hook(key, hooks, hook_data):
|
||||
|
||||
def dispatch_hook(key, hooks, hook_data, **kwargs):
|
||||
"""Dispatches a hook dictionary on a given piece of data."""
|
||||
|
||||
hooks = hooks or dict()
|
||||
@@ -36,7 +38,7 @@ def dispatch_hook(key, hooks, hook_data):
|
||||
hooks = [hooks]
|
||||
|
||||
for hook in hooks:
|
||||
_hook_data = hook(hook_data)
|
||||
_hook_data = hook(hook_data, **kwargs)
|
||||
if _hook_data is not None:
|
||||
hook_data = _hook_data
|
||||
|
||||
|
||||
@@ -9,36 +9,31 @@ This module contains the primary objects that power Requests.
|
||||
|
||||
import collections
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
from io import BytesIO
|
||||
from .hooks import dispatch_hook, default_hooks
|
||||
from .hooks import default_hooks
|
||||
from .structures import CaseInsensitiveDict
|
||||
from .status_codes import codes
|
||||
|
||||
from .auth import HTTPBasicAuth, HTTPProxyAuth
|
||||
from .cookies import cookiejar_from_dict, extract_cookies_to_jar, get_cookie_header
|
||||
from .auth import HTTPBasicAuth
|
||||
from .cookies import cookiejar_from_dict, get_cookie_header
|
||||
from .packages.urllib3.filepost import encode_multipart_formdata
|
||||
from .exceptions import (
|
||||
ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,
|
||||
URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)
|
||||
from .exceptions import HTTPError, RequestException, MissingSchema, InvalidURL
|
||||
from .utils import (
|
||||
get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,
|
||||
stream_decode_response_unicode, get_netrc_auth, get_environ_proxies,
|
||||
to_key_val_list, DEFAULT_CA_BUNDLE_PATH, parse_header_links, iter_slices,
|
||||
guess_json_utf)
|
||||
stream_untransfer, guess_filename, get_auth_from_url, requote_uri,
|
||||
stream_decode_response_unicode, to_key_val_list, parse_header_links,
|
||||
iter_slices, guess_json_utf, super_len)
|
||||
from .compat import (
|
||||
cookielib, urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,
|
||||
StringIO, is_py2, chardet, json, builtin_str, urldefrag, basestring)
|
||||
cookielib, urlparse, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
|
||||
is_py2, chardet, json, builtin_str, basestring)
|
||||
|
||||
REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)
|
||||
CONTENT_CHUNK_SIZE = 10 * 1024
|
||||
ITER_CHUNK_SIZE = 10 * 1024
|
||||
ITER_CHUNK_SIZE = 512
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RequestEncodingMixin(object):
|
||||
|
||||
@property
|
||||
def path_url(self):
|
||||
"""Build the path URL to use."""
|
||||
@@ -112,8 +107,12 @@ class RequestEncodingMixin(object):
|
||||
|
||||
for (k, v) in files:
|
||||
# support for explicit filename
|
||||
ft = None
|
||||
if isinstance(v, (tuple, list)):
|
||||
fn, fp = v
|
||||
if len(v) == 2:
|
||||
fn, fp = v
|
||||
else:
|
||||
fn, fp, ft = v
|
||||
else:
|
||||
fn = guess_filename(v) or k
|
||||
fp = v
|
||||
@@ -121,7 +120,12 @@ class RequestEncodingMixin(object):
|
||||
fp = StringIO(fp)
|
||||
if isinstance(fp, bytes):
|
||||
fp = BytesIO(fp)
|
||||
new_fields.append((k, (fn, fp.read())))
|
||||
|
||||
if ft:
|
||||
new_v = (fn, fp.read(), ft)
|
||||
else:
|
||||
new_v = (fn, fp.read())
|
||||
new_fields.append((k, new_v))
|
||||
|
||||
body, content_type = encode_multipart_formdata(new_fields)
|
||||
|
||||
@@ -150,7 +154,28 @@ class RequestHooksMixin(object):
|
||||
|
||||
|
||||
class Request(RequestHooksMixin):
|
||||
"""A user-created :class:`Request <Request>` object."""
|
||||
"""A user-created :class:`Request <Request>` object.
|
||||
|
||||
Used to prepare a :class:`PreparedRequest <PreparedRequest>`, which is sent to the server.
|
||||
|
||||
:param method: HTTP method to use.
|
||||
:param url: URL to send.
|
||||
:param headers: dictionary of headers to send.
|
||||
:param files: dictionary of {filename: fileobject} files to multipart upload.
|
||||
:param data: the body to attach the request. If a dictionary is provided, form-encoding will take place.
|
||||
:param params: dictionary of URL parameters to append to the URL.
|
||||
:param auth: Auth handler or (user, pass) tuple.
|
||||
:param cookies: dictionary or CookieJar of cookies to attach to this request.
|
||||
:param hooks: dictionary of callback hooks, for internal usage.
|
||||
|
||||
Usage::
|
||||
|
||||
>>> import requests
|
||||
>>> req = requests.Request('GET', 'http://httpbin.org/get')
|
||||
>>> req.prepare()
|
||||
<PreparedRequest [GET]>
|
||||
|
||||
"""
|
||||
def __init__(self,
|
||||
method=None,
|
||||
url=None,
|
||||
@@ -160,7 +185,6 @@ class Request(RequestHooksMixin):
|
||||
params=dict(),
|
||||
auth=None,
|
||||
cookies=None,
|
||||
timeout=None,
|
||||
hooks=None):
|
||||
|
||||
# Default empty dicts for dict params.
|
||||
@@ -182,39 +206,59 @@ class Request(RequestHooksMixin):
|
||||
self.params = params
|
||||
self.auth = auth
|
||||
self.cookies = cookies
|
||||
# self.allow_redirects = allow_redirects
|
||||
# self.proxies = proxies
|
||||
self.hooks = hooks
|
||||
|
||||
def __repr__(self):
|
||||
return '<Request [%s]>' % (self.method)
|
||||
|
||||
def prepare(self):
|
||||
"""Constructs a PreparedRequest for transmission and returns it."""
|
||||
"""Constructs a :class:`PreparedRequest <PreparedRequest>` for transmission and returns it."""
|
||||
p = PreparedRequest()
|
||||
|
||||
p.prepare_method(self.method)
|
||||
p.prepare_url(self.url, self.params)
|
||||
p.prepare_headers(self.headers)
|
||||
p.prepare_cookies(self.cookies)
|
||||
p.prepare_auth(self.auth)
|
||||
p.prepare_body(self.data, self.files)
|
||||
p.prepare_auth(self.auth, self.url)
|
||||
# Note that prepare_auth must be last to enable authentication schemes
|
||||
# such as OAuth to work on a fully prepared request.
|
||||
|
||||
# This MUST go after prepare_auth. Authenticators could add a hook
|
||||
p.prepare_hooks(self.hooks)
|
||||
|
||||
return p
|
||||
|
||||
|
||||
class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
"""The :class:`PreparedRequest <PreparedRequest>` object."""
|
||||
"""The fully mutable :class:`PreparedRequest <PreparedRequest>` object,
|
||||
containing the exact bytes that will be sent to the server.
|
||||
|
||||
Generated from either a :class:`Request <Request>` object or manually.
|
||||
|
||||
Usage::
|
||||
|
||||
>>> import requests
|
||||
>>> req = requests.Request('GET', 'http://httpbin.org/get')
|
||||
>>> r = req.prepare()
|
||||
<PreparedRequest [GET]>
|
||||
|
||||
>>> s = requests.Session()
|
||||
>>> s.send(r)
|
||||
<Response [200]>
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
#: HTTP verb to send to the server.
|
||||
self.method = None
|
||||
#: HTTP URL to send the request to.
|
||||
self.url = None
|
||||
#: dictionary of HTTP headers.
|
||||
self.headers = None
|
||||
#: request body to send to the server.
|
||||
self.body = None
|
||||
self.params = None
|
||||
self.auth = None
|
||||
self.allow_redirects = None
|
||||
self.proxies = None
|
||||
#: dictionary of callback hooks, for internal usage.
|
||||
self.hooks = default_hooks()
|
||||
|
||||
def __repr__(self):
|
||||
@@ -222,13 +266,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
|
||||
def prepare_method(self, method):
|
||||
"""Prepares the given HTTP method."""
|
||||
try:
|
||||
method = unicode(method)
|
||||
except NameError:
|
||||
# We're on Python 3.
|
||||
method = str(method)
|
||||
|
||||
self.method = method.upper()
|
||||
self.method = method
|
||||
if self.method is not None:
|
||||
self.method = self.method.upper()
|
||||
|
||||
def prepare_url(self, url, params):
|
||||
"""Prepares the given HTTP URL."""
|
||||
@@ -244,7 +284,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
# Support for unicode domain names and paths.
|
||||
scheme, netloc, path, _params, query, fragment = urlparse(url)
|
||||
|
||||
if not scheme:
|
||||
if not (scheme and netloc):
|
||||
raise MissingSchema("Invalid URL %r: No schema supplied" % url)
|
||||
|
||||
try:
|
||||
@@ -284,6 +324,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
"""Prepares the given HTTP headers."""
|
||||
|
||||
if headers:
|
||||
headers = dict((name.encode('ascii'), value) for name, value in headers.items())
|
||||
self.headers = CaseInsensitiveDict(headers)
|
||||
else:
|
||||
self.headers = CaseInsensitiveDict()
|
||||
@@ -291,42 +332,80 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
def prepare_body(self, data, files):
|
||||
"""Prepares the given HTTP body data."""
|
||||
|
||||
# If a generator is provided, error out.
|
||||
if isinstance(data, type(_ for _ in [])):
|
||||
raise NotImplementedError('Generator bodies are not supported yet.')
|
||||
# Check if file, fo, generator, iterator.
|
||||
# If not, run through normal process.
|
||||
|
||||
# Nottin' on you.
|
||||
body = None
|
||||
content_type = None
|
||||
length = None
|
||||
is_stream = False
|
||||
|
||||
is_stream = all([
|
||||
hasattr(data, '__iter__'),
|
||||
not isinstance(data, basestring),
|
||||
not isinstance(data, list),
|
||||
not isinstance(data, dict)
|
||||
])
|
||||
|
||||
try:
|
||||
length = super_len(data)
|
||||
except (TypeError, AttributeError):
|
||||
length = False
|
||||
|
||||
if is_stream:
|
||||
body = data
|
||||
|
||||
if files:
|
||||
raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
|
||||
|
||||
if length:
|
||||
self.headers['Content-Length'] = str(length)
|
||||
else:
|
||||
self.headers['Transfer-Encoding'] = 'chunked'
|
||||
# Check if file, fo, generator, iterator.
|
||||
# If not, run through normal process.
|
||||
|
||||
# Multi-part file uploads.
|
||||
if files:
|
||||
(body, content_type) = self._encode_files(files, data)
|
||||
else:
|
||||
if data:
|
||||
# Multi-part file uploads.
|
||||
if files:
|
||||
(body, content_type) = self._encode_files(files, data)
|
||||
else:
|
||||
if data:
|
||||
body = self._encode_params(data)
|
||||
if isinstance(data, str) or isinstance(data, builtin_str) or hasattr(data, 'read'):
|
||||
content_type = None
|
||||
else:
|
||||
content_type = 'application/x-www-form-urlencoded'
|
||||
|
||||
body = self._encode_params(data)
|
||||
if isinstance(data, str) or isinstance(data, builtin_str) or hasattr(data, 'read'):
|
||||
content_type = None
|
||||
else:
|
||||
content_type = 'application/x-www-form-urlencoded'
|
||||
self.prepare_content_length(body)
|
||||
|
||||
self.headers['Content-Length'] = '0'
|
||||
# Add content-type if it wasn't explicitly provided.
|
||||
if (content_type) and (not 'content-type' in self.headers):
|
||||
self.headers['Content-Type'] = content_type
|
||||
|
||||
self.body = body
|
||||
|
||||
def prepare_content_length(self, body):
|
||||
if hasattr(body, 'seek') and hasattr(body, 'tell'):
|
||||
body.seek(0, 2)
|
||||
self.headers['Content-Length'] = str(body.tell())
|
||||
body.seek(0, 0)
|
||||
elif body is not None:
|
||||
self.headers['Content-Length'] = str(len(body))
|
||||
l = super_len(body)
|
||||
if l:
|
||||
self.headers['Content-Length'] = str(l)
|
||||
elif self.method not in ('GET', 'HEAD'):
|
||||
self.headers['Content-Length'] = '0'
|
||||
|
||||
# Add content-type if it wasn't explicitly provided.
|
||||
if (content_type) and (not 'content-type' in self.headers):
|
||||
self.headers['Content-Type'] = content_type
|
||||
|
||||
self.body = body
|
||||
|
||||
def prepare_auth(self, auth):
|
||||
def prepare_auth(self, auth, url=''):
|
||||
"""Prepares the given HTTP auth data."""
|
||||
|
||||
# If no Auth is explicitly provided, extract it from the URL first.
|
||||
if auth is None:
|
||||
url_auth = get_auth_from_url(self.url)
|
||||
auth = url_auth if any(url_auth) else None
|
||||
|
||||
if auth:
|
||||
if isinstance(auth, tuple) and len(auth) == 2:
|
||||
# special-case basic HTTP auth
|
||||
@@ -338,6 +417,9 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
# Update self to reflect the auth changes.
|
||||
self.__dict__.update(r.__dict__)
|
||||
|
||||
# Recompute Content-Length
|
||||
self.prepare_content_length(self.body)
|
||||
|
||||
def prepare_cookies(self, cookies):
|
||||
"""Prepares the given HTTP cookie data."""
|
||||
|
||||
@@ -351,12 +433,15 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
|
||||
if cookie_header is not None:
|
||||
self.headers['Cookie'] = cookie_header
|
||||
|
||||
def prepare_hooks(self, hooks):
|
||||
"""Prepares the given hooks."""
|
||||
for event in hooks:
|
||||
self.register_hook(event, hooks[event])
|
||||
|
||||
|
||||
class Response(object):
|
||||
"""The core :class:`Response <Response>` object. All
|
||||
:class:`Request <Request>` objects contain a
|
||||
:class:`response <Response>` attribute, which is an instance
|
||||
of this class.
|
||||
"""The :class:`Response <Response>` object, which contains a
|
||||
server's response to an HTTP request.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
@@ -389,12 +474,15 @@ class Response(object):
|
||||
#: up here. The list is sorted from the oldest to the most recent request.
|
||||
self.history = []
|
||||
|
||||
|
||||
self.reason = None
|
||||
|
||||
#: A CookieJar of Cookies the server sent back.
|
||||
self.cookies = cookiejar_from_dict({})
|
||||
|
||||
#: The amount of time elapsed between sending the request
|
||||
#: and the arrival of the response (as a timedelta)
|
||||
self.elapsed = datetime.timedelta(0)
|
||||
|
||||
def __repr__(self):
|
||||
return '<Response [%s]>' % (self.status_code)
|
||||
|
||||
@@ -406,6 +494,10 @@ class Response(object):
|
||||
"""Returns true if :attr:`status_code` is 'OK'."""
|
||||
return self.ok
|
||||
|
||||
def __iter__(self):
|
||||
"""Allows you to use a response as an iterator."""
|
||||
return self.iter_content(128)
|
||||
|
||||
@property
|
||||
def ok(self):
|
||||
try:
|
||||
@@ -416,14 +508,16 @@ class Response(object):
|
||||
|
||||
@property
|
||||
def apparent_encoding(self):
|
||||
"""The apparent encoding, provided by the lovely Charade library."""
|
||||
"""The apparent encoding, provided by the lovely Charade library
|
||||
(Thanks, Ian!)."""
|
||||
return chardet.detect(self.content)['encoding']
|
||||
|
||||
def iter_content(self, chunk_size=1, decode_unicode=False):
|
||||
"""Iterates over the response data. This avoids reading the content
|
||||
at once into memory for large responses. The chunk size is the number
|
||||
of bytes it should read into memory. This is not necessarily the
|
||||
length of each item returned as decoding can take place.
|
||||
"""Iterates over the response data. When stream=True is set on the
|
||||
request, this avoids reading the content at once into memory for
|
||||
large responses. The chunk size is the number of bytes it should
|
||||
read into memory. This is not necessarily the length of each item
|
||||
returned as decoding can take place.
|
||||
"""
|
||||
if self._content_consumed:
|
||||
# simulate reading small chunks of the content
|
||||
@@ -445,16 +539,15 @@ class Response(object):
|
||||
return gen
|
||||
|
||||
def iter_lines(self, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None):
|
||||
"""Iterates over the response data, one line at a time. This
|
||||
avoids reading the content at once into memory for large
|
||||
responses.
|
||||
"""Iterates over the response data, one line at a time. When
|
||||
stream=True is set on the request, this avoids reading the
|
||||
content at once into memory for large responses.
|
||||
"""
|
||||
|
||||
pending = None
|
||||
|
||||
for chunk in self.iter_content(
|
||||
chunk_size=chunk_size,
|
||||
decode_unicode=decode_unicode):
|
||||
for chunk in self.iter_content(chunk_size=chunk_size,
|
||||
decode_unicode=decode_unicode):
|
||||
|
||||
if pending is not None:
|
||||
chunk = pending + chunk
|
||||
@@ -528,8 +621,11 @@ class Response(object):
|
||||
|
||||
return content
|
||||
|
||||
def json(self):
|
||||
"""Returns the json-encoded content of a response, if any."""
|
||||
def json(self, **kwargs):
|
||||
"""Returns the json-encoded content of a response, if any.
|
||||
|
||||
:param \*\*kwargs: Optional arguments that ``json.loads`` takes.
|
||||
"""
|
||||
|
||||
if not self.encoding and len(self.content) > 3:
|
||||
# No encoding set. JSON RFC 4627 section 3 states we should expect
|
||||
@@ -538,8 +634,8 @@ class Response(object):
|
||||
# a best guess).
|
||||
encoding = guess_json_utf(self.content)
|
||||
if encoding is not None:
|
||||
return json.loads(self.content.decode(encoding))
|
||||
return json.loads(self.text or self.content)
|
||||
return json.loads(self.content.decode(encoding), **kwargs)
|
||||
return json.loads(self.text or self.content, **kwargs)
|
||||
|
||||
@property
|
||||
def links(self):
|
||||
@@ -560,7 +656,7 @@ class Response(object):
|
||||
return l
|
||||
|
||||
def raise_for_status(self):
|
||||
"""Raises stored :class:`HTTPError` or :class:`URLError`, if one occurred."""
|
||||
"""Raises stored :class:`HTTPError`, if one occurred."""
|
||||
|
||||
http_error_msg = ''
|
||||
|
||||
@@ -571,9 +667,7 @@ class Response(object):
|
||||
http_error_msg = '%s Server Error: %s' % (self.status_code, self.reason)
|
||||
|
||||
if http_error_msg:
|
||||
http_error = HTTPError(http_error_msg)
|
||||
http_error.response = self
|
||||
raise http_error
|
||||
raise HTTPError(http_error_msg, response=self)
|
||||
|
||||
def close(self):
|
||||
return self.raw.release_conn()
|
||||
|
||||
@@ -15,10 +15,15 @@
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
__version__ = "1.0.1"
|
||||
__version__ = "1.0.3"
|
||||
from sys import version_info
|
||||
|
||||
|
||||
def detect(aBuf):
|
||||
if ((version_info < (3, 0) and isinstance(aBuf, unicode)) or
|
||||
(version_info >= (3, 0) and not isinstance(aBuf, bytes))):
|
||||
raise ValueError('Expected a bytes object, not a unicode object')
|
||||
|
||||
from . import universaldetector
|
||||
u = universaldetector.UniversalDetector()
|
||||
u.reset()
|
||||
|
||||
@@ -40,6 +40,7 @@ from .compat import wrap_ord
|
||||
ENOUGH_DATA_THRESHOLD = 1024
|
||||
SURE_YES = 0.99
|
||||
SURE_NO = 0.01
|
||||
MINIMUM_DATA_THRESHOLD = 3
|
||||
|
||||
|
||||
class CharDistributionAnalysis:
|
||||
@@ -82,7 +83,7 @@ class CharDistributionAnalysis:
|
||||
"""return confidence based on existing data"""
|
||||
# if we didn't receive any character in our consideration range,
|
||||
# return negative answer
|
||||
if self._mTotalChars <= 0:
|
||||
if self._mTotalChars <= 0 or self._mFreqChars <= MINIMUM_DATA_THRESHOLD:
|
||||
return SURE_NO
|
||||
|
||||
if self._mTotalChars != self._mFreqChars:
|
||||
|
||||
@@ -18,9 +18,17 @@
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
import sys
|
||||
|
||||
|
||||
if sys.version_info < (3, 0):
|
||||
base_str = (str, unicode)
|
||||
else:
|
||||
base_str = (bytes, str)
|
||||
|
||||
|
||||
def wrap_ord(a):
|
||||
if isinstance(a, str):
|
||||
if sys.version_info < (3, 0) and isinstance(a, base_str):
|
||||
return ord(a)
|
||||
elif isinstance(a, int):
|
||||
else:
|
||||
return a
|
||||
|
||||
44
requests/packages/charade/cp949prober.py
Normal file
44
requests/packages/charade/cp949prober.py
Normal file
@@ -0,0 +1,44 @@
|
||||
######################## BEGIN LICENSE BLOCK ########################
|
||||
# The Original Code is mozilla.org code.
|
||||
#
|
||||
# The Initial Developer of the Original Code is
|
||||
# Netscape Communications Corporation.
|
||||
# Portions created by the Initial Developer are Copyright (C) 1998
|
||||
# the Initial Developer. All Rights Reserved.
|
||||
#
|
||||
# Contributor(s):
|
||||
# Mark Pilgrim - port to Python
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU Lesser General Public
|
||||
# License as published by the Free Software Foundation; either
|
||||
# version 2.1 of the License, or (at your option) any later version.
|
||||
#
|
||||
# This library is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
# Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public
|
||||
# License along with this library; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
from .mbcharsetprober import MultiByteCharSetProber
|
||||
from .codingstatemachine import CodingStateMachine
|
||||
from .chardistribution import EUCKRDistributionAnalysis
|
||||
from .mbcssm import CP949SMModel
|
||||
|
||||
|
||||
class CP949Prober(MultiByteCharSetProber):
|
||||
def __init__(self):
|
||||
MultiByteCharSetProber.__init__(self)
|
||||
self._mCodingSM = CodingStateMachine(CP949SMModel)
|
||||
# NOTE: CP949 is a superset of EUC-KR, so the distribution should be
|
||||
# not different.
|
||||
self._mDistributionAnalyzer = EUCKRDistributionAnalysis()
|
||||
self.reset()
|
||||
|
||||
def get_charset_name(self):
|
||||
return "CP949"
|
||||
@@ -25,8 +25,6 @@
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
from . import constants
|
||||
|
||||
# KOI8-R language model
|
||||
# Character Mapping Table:
|
||||
KOI8R_CharToOrderMap = (
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
from . import constants
|
||||
|
||||
# 255: Control characters that usually does not exist in any text
|
||||
# 254: Carriage/Return
|
||||
# 253: symbol (punctuation) that does not belong to word
|
||||
|
||||
@@ -27,8 +27,6 @@
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
from . import constants
|
||||
|
||||
# 255: Control characters that usually does not exist in any text
|
||||
# 254: Carriage/Return
|
||||
# 253: symbol (punctuation) that does not belong to word
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
# 02110-1301 USA
|
||||
######################### END LICENSE BLOCK #########################
|
||||
|
||||
from . import constants
|
||||
|
||||
# 255: Control characters that usually does not exist in any text
|
||||
# 254: Carriage/Return
|
||||
# 253: symbol (punctuation) that does not belong to word
|
||||
|
||||
@@ -33,6 +33,7 @@ from .sjisprober import SJISProber
|
||||
from .eucjpprober import EUCJPProber
|
||||
from .gb2312prober import GB2312Prober
|
||||
from .euckrprober import EUCKRProber
|
||||
from .cp949prober import CP949Prober
|
||||
from .big5prober import Big5Prober
|
||||
from .euctwprober import EUCTWProber
|
||||
|
||||
@@ -46,6 +47,7 @@ class MBCSGroupProber(CharSetGroupProber):
|
||||
EUCJPProber(),
|
||||
GB2312Prober(),
|
||||
EUCKRProber(),
|
||||
CP949Prober(),
|
||||
Big5Prober(),
|
||||
EUCTWProber()
|
||||
]
|
||||
|
||||
@@ -78,6 +78,46 @@ Big5SMModel = {'classTable': BIG5_cls,
|
||||
'charLenTable': Big5CharLenTable,
|
||||
'name': 'Big5'}
|
||||
|
||||
# CP949
|
||||
|
||||
CP949_cls = (
|
||||
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,0,0, # 00 - 0f
|
||||
1,1,1,1,1,1,1,1, 1,1,1,0,1,1,1,1, # 10 - 1f
|
||||
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, # 20 - 2f
|
||||
1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, # 30 - 3f
|
||||
1,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4, # 40 - 4f
|
||||
4,4,5,5,5,5,5,5, 5,5,5,1,1,1,1,1, # 50 - 5f
|
||||
1,5,5,5,5,5,5,5, 5,5,5,5,5,5,5,5, # 60 - 6f
|
||||
5,5,5,5,5,5,5,5, 5,5,5,1,1,1,1,1, # 70 - 7f
|
||||
0,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6, # 80 - 8f
|
||||
6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6, # 90 - 9f
|
||||
6,7,7,7,7,7,7,7, 7,7,7,7,7,8,8,8, # a0 - af
|
||||
7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7, # b0 - bf
|
||||
7,7,7,7,7,7,9,2, 2,3,2,2,2,2,2,2, # c0 - cf
|
||||
2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, # d0 - df
|
||||
2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2, # e0 - ef
|
||||
2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,0, # f0 - ff
|
||||
)
|
||||
|
||||
CP949_st = (
|
||||
#cls= 0 1 2 3 4 5 6 7 8 9 # previous state =
|
||||
eError,eStart, 3,eError,eStart,eStart, 4, 5,eError, 6, # eStart
|
||||
eError,eError,eError,eError,eError,eError,eError,eError,eError,eError, # eError
|
||||
eItsMe,eItsMe,eItsMe,eItsMe,eItsMe,eItsMe,eItsMe,eItsMe,eItsMe,eItsMe, # eItsMe
|
||||
eError,eError,eStart,eStart,eError,eError,eError,eStart,eStart,eStart, # 3
|
||||
eError,eError,eStart,eStart,eStart,eStart,eStart,eStart,eStart,eStart, # 4
|
||||
eError,eStart,eStart,eStart,eStart,eStart,eStart,eStart,eStart,eStart, # 5
|
||||
eError,eStart,eStart,eStart,eStart,eError,eError,eStart,eStart,eStart, # 6
|
||||
)
|
||||
|
||||
CP949CharLenTable = (0, 1, 2, 0, 1, 1, 2, 2, 0, 2)
|
||||
|
||||
CP949SMModel = {'classTable': CP949_cls,
|
||||
'classFactor': 10,
|
||||
'stateTable': CP949_st,
|
||||
'charLenTable': CP949CharLenTable,
|
||||
'name': 'CP949'}
|
||||
|
||||
# EUC-JP
|
||||
|
||||
EUCJP_cls = (
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
from . import constants
|
||||
import sys
|
||||
import codecs
|
||||
from .latin1prober import Latin1Prober # windows-1252
|
||||
from .mbcsgroupprober import MBCSGroupProber # multi-byte character sets
|
||||
from .sbcsgroupprober import SBCSGroupProber # single-byte character sets
|
||||
@@ -70,31 +71,31 @@ class UniversalDetector:
|
||||
|
||||
if not self._mGotData:
|
||||
# If the data starts with BOM, we know it is UTF
|
||||
if aBuf[:3] == '\xEF\xBB\xBF':
|
||||
if aBuf[:3] == codecs.BOM:
|
||||
# EF BB BF UTF-8 with BOM
|
||||
self.result = {'encoding': "UTF-8", 'confidence': 1.0}
|
||||
elif aBuf[:4] == '\xFF\xFE\x00\x00':
|
||||
elif aBuf[:4] == codecs.BOM_UTF32_LE:
|
||||
# FF FE 00 00 UTF-32, little-endian BOM
|
||||
self.result = {'encoding': "UTF-32LE", 'confidence': 1.0}
|
||||
elif aBuf[:4] == '\x00\x00\xFE\xFF':
|
||||
elif aBuf[:4] == codecs.BOM_UTF32_BE:
|
||||
# 00 00 FE FF UTF-32, big-endian BOM
|
||||
self.result = {'encoding': "UTF-32BE", 'confidence': 1.0}
|
||||
elif aBuf[:4] == '\xFE\xFF\x00\x00':
|
||||
elif aBuf[:4] == b'\xFE\xFF\x00\x00':
|
||||
# FE FF 00 00 UCS-4, unusual octet order BOM (3412)
|
||||
self.result = {
|
||||
'encoding': "X-ISO-10646-UCS-4-3412",
|
||||
'confidence': 1.0
|
||||
}
|
||||
elif aBuf[:4] == '\x00\x00\xFF\xFE':
|
||||
elif aBuf[:4] == b'\x00\x00\xFF\xFE':
|
||||
# 00 00 FF FE UCS-4, unusual octet order BOM (2143)
|
||||
self.result = {
|
||||
'encoding': "X-ISO-10646-UCS-4-2143",
|
||||
'confidence': 1.0
|
||||
}
|
||||
elif aBuf[:2] == '\xFF\xFE':
|
||||
elif aBuf[:2] == codecs.BOM_LE:
|
||||
# FF FE UTF-16, little endian BOM
|
||||
self.result = {'encoding': "UTF-16LE", 'confidence': 1.0}
|
||||
elif aBuf[:2] == '\xFE\xFF':
|
||||
elif aBuf[:2] == codecs.BOM_BE:
|
||||
# FE FF UTF-16, big endian BOM
|
||||
self.result = {'encoding': "UTF-16BE", 'confidence': 1.0}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import socket
|
||||
import errno
|
||||
|
||||
from socket import error as SocketError, timeout as SocketTimeout
|
||||
from .util import resolve_cert_reqs, resolve_ssl_version
|
||||
|
||||
try: # Python 3
|
||||
from http.client import HTTPConnection, HTTPException
|
||||
@@ -80,31 +81,29 @@ class VerifiedHTTPSConnection(HTTPSConnection):
|
||||
ssl_version = None
|
||||
|
||||
def set_cert(self, key_file=None, cert_file=None,
|
||||
cert_reqs='CERT_NONE', ca_certs=None):
|
||||
ssl_req_scheme = {
|
||||
'CERT_NONE': ssl.CERT_NONE,
|
||||
'CERT_OPTIONAL': ssl.CERT_OPTIONAL,
|
||||
'CERT_REQUIRED': ssl.CERT_REQUIRED
|
||||
}
|
||||
cert_reqs=None, ca_certs=None):
|
||||
|
||||
self.key_file = key_file
|
||||
self.cert_file = cert_file
|
||||
self.cert_reqs = ssl_req_scheme.get(cert_reqs) or ssl.CERT_NONE
|
||||
self.cert_reqs = cert_reqs
|
||||
self.ca_certs = ca_certs
|
||||
|
||||
def connect(self):
|
||||
# Add certificate verification
|
||||
sock = socket.create_connection((self.host, self.port), self.timeout)
|
||||
|
||||
resolved_cert_reqs = resolve_cert_reqs(self.cert_reqs)
|
||||
resolved_ssl_version = resolve_ssl_version(self.ssl_version)
|
||||
|
||||
# Wrap socket using verification with the root certs in
|
||||
# trusted_root_certs
|
||||
self.sock = ssl_wrap_socket(sock, self.key_file, self.cert_file,
|
||||
cert_reqs=self.cert_reqs,
|
||||
cert_reqs=resolved_cert_reqs,
|
||||
ca_certs=self.ca_certs,
|
||||
server_hostname=self.host,
|
||||
ssl_version=self.ssl_version)
|
||||
ssl_version=resolved_ssl_version)
|
||||
|
||||
if self.ca_certs:
|
||||
if resolved_cert_reqs != ssl.CERT_NONE:
|
||||
match_hostname(self.sock.getpeercert(), self.host)
|
||||
|
||||
|
||||
@@ -514,7 +513,7 @@ class HTTPSConnectionPool(HTTPConnectionPool):
|
||||
strict=False, timeout=None, maxsize=1,
|
||||
block=False, headers=None,
|
||||
key_file=None, cert_file=None,
|
||||
cert_reqs='CERT_NONE', ca_certs=None, ssl_version=None):
|
||||
cert_reqs=None, ca_certs=None, ssl_version=None):
|
||||
|
||||
HTTPConnectionPool.__init__(self, host, port,
|
||||
strict, timeout, maxsize,
|
||||
@@ -548,10 +547,7 @@ class HTTPSConnectionPool(HTTPConnectionPool):
|
||||
connection.set_cert(key_file=self.key_file, cert_file=self.cert_file,
|
||||
cert_reqs=self.cert_reqs, ca_certs=self.ca_certs)
|
||||
|
||||
if self.ssl_version is None:
|
||||
connection.ssl_version = ssl.PROTOCOL_SSLv23
|
||||
else:
|
||||
connection.ssl_version = self.ssl_version
|
||||
connection.ssl_version = self.ssl_version
|
||||
|
||||
return connection
|
||||
|
||||
|
||||
@@ -58,6 +58,17 @@ class PoolManager(RequestMethods):
|
||||
self.pools = RecentlyUsedContainer(num_pools,
|
||||
dispose_func=lambda p: p.close())
|
||||
|
||||
def _new_pool(self, scheme, host, port):
|
||||
"""
|
||||
Create a new :class:`ConnectionPool` based on host, port and scheme.
|
||||
|
||||
This method is used to actually create the connection pools handed out
|
||||
by :meth:`connection_from_url` and companion methods. It is intended
|
||||
to be overridden for customization.
|
||||
"""
|
||||
pool_cls = pool_classes_by_scheme[scheme]
|
||||
return pool_cls(host, port, **self.connection_pool_kw)
|
||||
|
||||
def clear(self):
|
||||
"""
|
||||
Empty our store of pools and direct them all to close.
|
||||
@@ -74,6 +85,7 @@ class PoolManager(RequestMethods):
|
||||
If ``port`` isn't given, it will be derived from the ``scheme`` using
|
||||
``urllib3.connectionpool.port_by_scheme``.
|
||||
"""
|
||||
scheme = scheme or 'http'
|
||||
port = port or port_by_scheme.get(scheme, 80)
|
||||
|
||||
pool_key = (scheme, host, port)
|
||||
@@ -85,11 +97,8 @@ class PoolManager(RequestMethods):
|
||||
return pool
|
||||
|
||||
# Make a fresh ConnectionPool of the desired type
|
||||
pool_cls = pool_classes_by_scheme[scheme]
|
||||
pool = pool_cls(host, port, **self.connection_pool_kw)
|
||||
|
||||
pool = self._new_pool(scheme, host, port)
|
||||
self.pools[pool_key] = pool
|
||||
|
||||
return pool
|
||||
|
||||
def connection_from_url(self, url):
|
||||
@@ -138,14 +147,24 @@ class PoolManager(RequestMethods):
|
||||
class ProxyManager(RequestMethods):
|
||||
"""
|
||||
Given a ConnectionPool to a proxy, the ProxyManager's ``urlopen`` method
|
||||
will make requests to any url through the defined proxy.
|
||||
will make requests to any url through the defined proxy. The ProxyManager
|
||||
class will automatically set the 'Host' header if it is not provided.
|
||||
"""
|
||||
|
||||
def __init__(self, proxy_pool):
|
||||
self.proxy_pool = proxy_pool
|
||||
|
||||
def _set_proxy_headers(self, headers=None):
|
||||
def _set_proxy_headers(self, url, headers=None):
|
||||
"""
|
||||
Sets headers needed by proxies: specifically, the Accept and Host
|
||||
headers. Only sets headers not provided by the user.
|
||||
"""
|
||||
headers_ = {'Accept': '*/*'}
|
||||
|
||||
host = parse_url(url).host
|
||||
if host:
|
||||
headers_['Host'] = host
|
||||
|
||||
if headers:
|
||||
headers_.update(headers)
|
||||
|
||||
@@ -154,7 +173,7 @@ class ProxyManager(RequestMethods):
|
||||
def urlopen(self, method, url, **kw):
|
||||
"Same as HTTP(S)ConnectionPool.urlopen, ``url`` must be absolute."
|
||||
kw['assert_same_host'] = False
|
||||
kw['headers'] = self._set_proxy_headers(kw.get('headers'))
|
||||
kw['headers'] = self._set_proxy_headers(url, headers=kw.get('headers'))
|
||||
return self.proxy_pool.urlopen(method, url, **kw)
|
||||
|
||||
|
||||
|
||||
@@ -145,7 +145,17 @@ class HTTPResponse(object):
|
||||
# cStringIO doesn't like amt=None
|
||||
data = self._fp.read()
|
||||
else:
|
||||
return self._fp.read(amt)
|
||||
data = self._fp.read(amt)
|
||||
if amt != 0 and not data: # Platform-specific: Buggy versions of Python.
|
||||
# Close the connection when no data is returned
|
||||
#
|
||||
# This is redundant to what httplib/http.client _should_
|
||||
# already do. However, versions of python released before
|
||||
# December 15, 2012 (http://bugs.python.org/issue16298) do not
|
||||
# properly close the connection in all cases. There is no harm
|
||||
# in redundantly calling close.
|
||||
self._fp.close()
|
||||
return data
|
||||
|
||||
try:
|
||||
if decode_content and decoder:
|
||||
|
||||
@@ -22,6 +22,7 @@ try: # Test for SSL features
|
||||
SSLContext = None
|
||||
HAS_SNI = False
|
||||
|
||||
import ssl
|
||||
from ssl import wrap_socket, CERT_NONE, SSLError, PROTOCOL_SSLv23
|
||||
from ssl import SSLContext # Modern SSL?
|
||||
from ssl import HAS_SNI # Has SNI?
|
||||
@@ -263,10 +264,48 @@ def is_connection_dropped(conn):
|
||||
return True
|
||||
|
||||
|
||||
def resolve_cert_reqs(candidate):
|
||||
"""
|
||||
Resolves the argument to a numeric constant, which can be passed to
|
||||
the wrap_socket function/method from the ssl module.
|
||||
Defaults to :data:`ssl.CERT_NONE`.
|
||||
If given a string it is assumed to be the name of the constant in the
|
||||
:mod:`ssl` module or its abbrevation.
|
||||
(So you can specify `REQUIRED` instead of `CERT_REQUIRED`.
|
||||
If it's neither `None` nor a string we assume it is already the numeric
|
||||
constant which can directly be passed to wrap_socket.
|
||||
"""
|
||||
if candidate is None:
|
||||
return CERT_NONE
|
||||
|
||||
if isinstance(candidate, str):
|
||||
res = getattr(ssl, candidate, None)
|
||||
if res is None:
|
||||
res = getattr(ssl, 'CERT_' + candidate)
|
||||
return res
|
||||
|
||||
return candidate
|
||||
|
||||
|
||||
def resolve_ssl_version(candidate):
|
||||
"""
|
||||
like resolve_cert_reqs
|
||||
"""
|
||||
if candidate is None:
|
||||
return PROTOCOL_SSLv23
|
||||
|
||||
if isinstance(candidate, str):
|
||||
res = getattr(ssl, candidate, None)
|
||||
if res is None:
|
||||
res = getattr(ssl, 'PROTOCOL_' + candidate)
|
||||
return res
|
||||
|
||||
return candidate
|
||||
|
||||
if SSLContext is not None: # Python 3.2+
|
||||
def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=CERT_NONE,
|
||||
def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,
|
||||
ca_certs=None, server_hostname=None,
|
||||
ssl_version=PROTOCOL_SSLv23):
|
||||
ssl_version=None):
|
||||
"""
|
||||
All arguments except `server_hostname` have the same meaning as for
|
||||
:func:`ssl.wrap_socket`
|
||||
@@ -279,8 +318,9 @@ if SSLContext is not None: # Python 3.2+
|
||||
if ca_certs:
|
||||
try:
|
||||
context.load_verify_locations(ca_certs)
|
||||
except TypeError as e: # Reraise as SSLError
|
||||
# FIXME: This block needs a test.
|
||||
# Py32 raises IOError
|
||||
# Py33 raises FileNotFoundError
|
||||
except Exception as e: # Reraise as SSLError
|
||||
raise SSLError(e)
|
||||
if certfile:
|
||||
# FIXME: This block needs a test.
|
||||
@@ -290,9 +330,9 @@ if SSLContext is not None: # Python 3.2+
|
||||
return context.wrap_socket(sock)
|
||||
|
||||
else: # Python 3.1 and earlier
|
||||
def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=CERT_NONE,
|
||||
def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,
|
||||
ca_certs=None, server_hostname=None,
|
||||
ssl_version=PROTOCOL_SSLv23):
|
||||
ssl_version=None):
|
||||
return wrap_socket(sock, keyfile=keyfile, certfile=certfile,
|
||||
ca_certs=ca_certs, cert_reqs=cert_reqs,
|
||||
ssl_version=ssl_version)
|
||||
|
||||
@@ -9,11 +9,12 @@ requests (cookies, auth, proxies).
|
||||
|
||||
"""
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from .compat import cookielib
|
||||
from .cookies import cookiejar_from_dict
|
||||
from .models import Request
|
||||
from .hooks import dispatch_hook, default_hooks
|
||||
from .models import Request, PreparedRequest
|
||||
from .hooks import default_hooks, dispatch_hook
|
||||
from .utils import from_key_val_list, default_headers
|
||||
from .exceptions import TooManyRedirects, InvalidSchema
|
||||
|
||||
@@ -23,7 +24,12 @@ from .adapters import HTTPAdapter
|
||||
from .utils import requote_uri, get_environ_proxies, get_netrc_auth
|
||||
|
||||
from .status_codes import codes
|
||||
REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)
|
||||
REDIRECT_STATI = (
|
||||
codes.moved, # 301
|
||||
codes.found, # 302
|
||||
codes.other, # 303
|
||||
codes.temporary_moved, # 307
|
||||
)
|
||||
DEFAULT_REDIRECT_LIMIT = 30
|
||||
|
||||
|
||||
@@ -49,9 +55,20 @@ def merge_kwargs(local_kwarg, default_kwarg):
|
||||
default_kwarg = from_key_val_list(default_kwarg)
|
||||
local_kwarg = from_key_val_list(local_kwarg)
|
||||
|
||||
# Update new values.
|
||||
# Update new values in a case-insensitive way
|
||||
def get_original_key(original_keys, new_key):
|
||||
"""
|
||||
Finds the key from original_keys that case-insensitive matches new_key.
|
||||
"""
|
||||
for original_key in original_keys:
|
||||
if key.lower() == original_key.lower():
|
||||
return original_key
|
||||
return new_key
|
||||
|
||||
kwargs = default_kwarg.copy()
|
||||
kwargs.update(local_kwarg)
|
||||
original_keys = kwargs.keys()
|
||||
for key, value in local_kwarg.items():
|
||||
kwargs[get_original_key(original_keys, key)] = value
|
||||
|
||||
# Remove keys that are set to None.
|
||||
for (k, v) in local_kwarg.items():
|
||||
@@ -62,19 +79,25 @@ def merge_kwargs(local_kwarg, default_kwarg):
|
||||
|
||||
|
||||
class SessionRedirectMixin(object):
|
||||
|
||||
def resolve_redirects(self, resp, req, stream=False, timeout=None, verify=True, cert=None, proxies=None):
|
||||
def resolve_redirects(self, resp, req, stream=False, timeout=None,
|
||||
verify=True, cert=None, proxies=None):
|
||||
"""Receives a Response. Returns a generator of Responses."""
|
||||
|
||||
i = 0
|
||||
prepared_request = PreparedRequest()
|
||||
prepared_request.body = req.body
|
||||
prepared_request.headers = req.headers.copy()
|
||||
prepared_request.hooks = req.hooks
|
||||
prepared_request.method = req.method
|
||||
prepared_request.url = req.url
|
||||
|
||||
cookiejar = cookiejar_from_dict({})
|
||||
cookiejar.update(self.cookies)
|
||||
cookiejar.update(resp.cookies)
|
||||
|
||||
# ((resp.status_code is codes.see_other))
|
||||
while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):
|
||||
|
||||
# Persist cookies.
|
||||
for cookie in resp.cookies:
|
||||
self.cookies.set_cookie(cookie)
|
||||
|
||||
resp.content # Consume socket so it can be released
|
||||
|
||||
if i >= self.max_redirects:
|
||||
@@ -84,7 +107,7 @@ class SessionRedirectMixin(object):
|
||||
resp.close()
|
||||
|
||||
url = resp.headers['location']
|
||||
method = req.method
|
||||
method = prepared_request.method
|
||||
|
||||
# Handle redirection without scheme (see: RFC 1808 Section 4)
|
||||
if url.startswith('//'):
|
||||
@@ -97,45 +120,70 @@ class SessionRedirectMixin(object):
|
||||
# Compliant with RFC3986, we percent encode the url.
|
||||
url = urljoin(resp.url, requote_uri(url))
|
||||
|
||||
prepared_request.url = url
|
||||
|
||||
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4
|
||||
if resp.status_code is codes.see_other:
|
||||
if (resp.status_code == codes.see_other and
|
||||
prepared_request.method != 'HEAD'):
|
||||
method = 'GET'
|
||||
|
||||
# Do what the browsers do, despite standards...
|
||||
if resp.status_code in (codes.moved, codes.found) and req.method == 'POST':
|
||||
if (resp.status_code in (codes.moved, codes.found) and
|
||||
prepared_request.method == 'POST'):
|
||||
method = 'GET'
|
||||
|
||||
if (resp.status_code == 303) and req.method != 'HEAD':
|
||||
method = 'GET'
|
||||
prepared_request.method = method
|
||||
|
||||
# Remove the cookie headers that were sent.
|
||||
headers = req.headers
|
||||
# https://github.com/kennethreitz/requests/issues/1084
|
||||
if resp.status_code not in (codes.temporary, codes.resume):
|
||||
if 'Content-Length' in prepared_request.headers:
|
||||
del prepared_request.headers['Content-Length']
|
||||
|
||||
prepared_request.body = None
|
||||
|
||||
headers = prepared_request.headers
|
||||
try:
|
||||
del headers['Cookie']
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
resp = self.request(
|
||||
url=url,
|
||||
method=method,
|
||||
headers=headers,
|
||||
params=req.params,
|
||||
auth=req.auth,
|
||||
cookies=req.cookies,
|
||||
allow_redirects=False,
|
||||
stream=stream,
|
||||
timeout=timeout,
|
||||
verify=verify,
|
||||
cert=cert,
|
||||
proxies=proxies
|
||||
)
|
||||
prepared_request.prepare_cookies(cookiejar)
|
||||
|
||||
resp = self.send(
|
||||
prepared_request,
|
||||
stream=stream,
|
||||
timeout=timeout,
|
||||
verify=verify,
|
||||
cert=cert,
|
||||
proxies=proxies,
|
||||
allow_redirects=False,
|
||||
)
|
||||
|
||||
cookiejar.update(resp.cookies)
|
||||
|
||||
i += 1
|
||||
yield resp
|
||||
|
||||
resp.cookies.update(cookiejar)
|
||||
|
||||
|
||||
class Session(SessionRedirectMixin):
|
||||
"""A Requests session."""
|
||||
"""A Requests session.
|
||||
|
||||
Provides cookie persistience, connection-pooling, and configuration.
|
||||
|
||||
Basic Usage::
|
||||
|
||||
>>> import requests
|
||||
>>> s = requests.Session()
|
||||
>>> s.get('http://httpbin.org/get')
|
||||
200
|
||||
"""
|
||||
|
||||
__attrs__ = [
|
||||
'headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks',
|
||||
'params', 'verify', 'cert', 'prefetch', 'adapters', 'stream',
|
||||
'trust_env', 'max_redirects']
|
||||
|
||||
def __init__(self):
|
||||
|
||||
@@ -144,7 +192,7 @@ class Session(SessionRedirectMixin):
|
||||
#: :class:`Session <Session>`.
|
||||
self.headers = default_headers()
|
||||
|
||||
#: Authentication tuple or object to attach to
|
||||
#: Default Authentication tuple or object to attach to
|
||||
#: :class:`Request <Request>`.
|
||||
self.auth = None
|
||||
|
||||
@@ -161,19 +209,19 @@ class Session(SessionRedirectMixin):
|
||||
#: representing multivalued query parameters.
|
||||
self.params = {}
|
||||
|
||||
#: Stream response content.
|
||||
#: Stream response content default.
|
||||
self.stream = False
|
||||
|
||||
#: SSL Verification.
|
||||
#: SSL Verification default.
|
||||
self.verify = True
|
||||
|
||||
#: SSL certificate.
|
||||
#: SSL certificate default.
|
||||
self.cert = None
|
||||
|
||||
#: Maximum number of redirects to follow.
|
||||
self.max_redirects = DEFAULT_REDIRECT_LIMIT
|
||||
|
||||
#: Should we trust the environment
|
||||
#: Should we trust the environment?
|
||||
self.trust_env = True
|
||||
|
||||
# Set up a CookieJar to be used by default
|
||||
@@ -204,6 +252,39 @@ class Session(SessionRedirectMixin):
|
||||
stream=None,
|
||||
verify=None,
|
||||
cert=None):
|
||||
"""Constructs a :class:`Request <Request>`, prepares it and sends it.
|
||||
Returns :class:`Response <Response>` object.
|
||||
|
||||
: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 bytes to send in the body of the
|
||||
:class:`Request`.
|
||||
:param headers: (optional) Dictionary of HTTP Headers to send with the
|
||||
:class:`Request`.
|
||||
:param cookies: (optional) Dict or CookieJar object to send with the
|
||||
:class:`Request`.
|
||||
:param files: (optional) Dictionary of 'filename': file-like-objects
|
||||
for multipart encoding upload.
|
||||
:param auth: (optional) Auth tuple or callable to enable
|
||||
Basic/Digest/Custom HTTP Auth.
|
||||
:param timeout: (optional) Float describing the timeout of the
|
||||
request.
|
||||
:param allow_redirects: (optional) Boolean. Set to True by default.
|
||||
:param proxies: (optional) Dictionary mapping protocol to the URL of
|
||||
the proxy.
|
||||
:param return_response: (optional) If False, an un-sent Request object
|
||||
will returned.
|
||||
:param config: (optional) A configuration dictionary. See
|
||||
``request.defaults`` for allowed keys and their default values.
|
||||
:param prefetch: (optional) whether to immediately download the response
|
||||
content. Defaults to ``True``.
|
||||
:param verify: (optional) if ``True``, the SSL cert will be verified.
|
||||
A CA_BUNDLE path can also be provided.
|
||||
:param cert: (optional) if String, path to ssl client cert file (.pem).
|
||||
If Tuple, ('cert', 'key') pair.
|
||||
"""
|
||||
|
||||
cookies = cookies or {}
|
||||
proxies = proxies or {}
|
||||
@@ -212,9 +293,10 @@ class Session(SessionRedirectMixin):
|
||||
if not isinstance(cookies, cookielib.CookieJar):
|
||||
cookies = cookiejar_from_dict(cookies)
|
||||
|
||||
# Bubble down session cookies.
|
||||
for cookie in self.cookies:
|
||||
cookies.set_cookie(cookie)
|
||||
# Merge with session cookies
|
||||
merged_cookies = self.cookies.copy()
|
||||
merged_cookies.update(cookies)
|
||||
cookies = merged_cookies
|
||||
|
||||
# Gather clues from the surrounding environment.
|
||||
if self.trust_env:
|
||||
@@ -235,7 +317,6 @@ class Session(SessionRedirectMixin):
|
||||
if not verify and verify is not False:
|
||||
verify = os.environ.get('CURL_CA_BUNDLE')
|
||||
|
||||
|
||||
# Merge all the kwargs.
|
||||
params = merge_kwargs(params, self.params)
|
||||
headers = merge_kwargs(headers, self.headers)
|
||||
@@ -246,10 +327,9 @@ class Session(SessionRedirectMixin):
|
||||
verify = merge_kwargs(verify, self.verify)
|
||||
cert = merge_kwargs(cert, self.cert)
|
||||
|
||||
|
||||
# Create the Request.
|
||||
req = Request()
|
||||
req.method = method
|
||||
req.method = method.upper()
|
||||
req.url = url
|
||||
req.headers = headers
|
||||
req.files = files
|
||||
@@ -263,22 +343,18 @@ class Session(SessionRedirectMixin):
|
||||
prep = req.prepare()
|
||||
|
||||
# Send the request.
|
||||
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
|
||||
send_kwargs = {
|
||||
'stream': stream,
|
||||
'timeout': timeout,
|
||||
'verify': verify,
|
||||
'cert': cert,
|
||||
'proxies': proxies,
|
||||
'allow_redirects': allow_redirects,
|
||||
}
|
||||
resp = self.send(prep, **send_kwargs)
|
||||
|
||||
# Redirect resolving generator.
|
||||
gen = self.resolve_redirects(resp, req, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
|
||||
|
||||
# Resolve redirects if allowed.
|
||||
history = [r for r in gen] if allow_redirects else []
|
||||
|
||||
# Shuffle things around if there's history.
|
||||
if history:
|
||||
history.insert(0, resp)
|
||||
resp = history.pop()
|
||||
resp.history = tuple(history)
|
||||
|
||||
# Response manipulation hook.
|
||||
self.response = dispatch_hook('response', hooks, resp)
|
||||
# Persist cookies.
|
||||
self.cookies.update(resp.cookies)
|
||||
|
||||
return resp
|
||||
|
||||
@@ -316,7 +392,7 @@ class Session(SessionRedirectMixin):
|
||||
"""Sends a POST request. Returns :class:`Response` object.
|
||||
|
||||
:param url: URL for the new :class:`Request` object.
|
||||
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
@@ -326,7 +402,7 @@ class Session(SessionRedirectMixin):
|
||||
"""Sends a PUT request. Returns :class:`Response` object.
|
||||
|
||||
:param url: URL for the new :class:`Request` object.
|
||||
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
@@ -336,7 +412,7 @@ class Session(SessionRedirectMixin):
|
||||
"""Sends a PATCH request. Returns :class:`Response` object.
|
||||
|
||||
:param url: URL for the new :class:`Request` object.
|
||||
:param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
|
||||
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
|
||||
:param \*\*kwargs: Optional arguments that ``request`` takes.
|
||||
"""
|
||||
|
||||
@@ -353,8 +429,57 @@ class Session(SessionRedirectMixin):
|
||||
|
||||
def send(self, request, **kwargs):
|
||||
"""Send a given PreparedRequest."""
|
||||
# Set defaults that the hooks can utilize to ensure they always have
|
||||
# the correct parameters to reproduce the previous request.
|
||||
kwargs.setdefault('stream', self.stream)
|
||||
kwargs.setdefault('verify', self.verify)
|
||||
kwargs.setdefault('cert', self.cert)
|
||||
kwargs.setdefault('proxies', self.proxies)
|
||||
|
||||
# It's possible that users might accidentally send a Request object.
|
||||
# Guard against that specific failure case.
|
||||
if getattr(request, 'prepare', None):
|
||||
raise ValueError('You can only send PreparedRequests.')
|
||||
|
||||
# Set up variables needed for resolve_redirects and dispatching of
|
||||
# hooks
|
||||
allow_redirects = kwargs.pop('allow_redirects', True)
|
||||
stream = kwargs.get('stream')
|
||||
timeout = kwargs.get('timeout')
|
||||
verify = kwargs.get('verify')
|
||||
cert = kwargs.get('cert')
|
||||
proxies = kwargs.get('proxies')
|
||||
hooks = request.hooks
|
||||
|
||||
# Get the appropriate adapter to use
|
||||
adapter = self.get_adapter(url=request.url)
|
||||
|
||||
# Start time (approximately) of the request
|
||||
start = datetime.utcnow()
|
||||
# Send the request
|
||||
r = adapter.send(request, **kwargs)
|
||||
# Total elapsed time of the request (approximately)
|
||||
r.elapsed = datetime.utcnow() - start
|
||||
|
||||
# Response manipulation hooks
|
||||
r = dispatch_hook('response', hooks, r, **kwargs)
|
||||
|
||||
# Redirect resolving generator.
|
||||
gen = self.resolve_redirects(r, request, stream=stream,
|
||||
timeout=timeout, verify=verify, cert=cert,
|
||||
proxies=proxies)
|
||||
|
||||
# Resolve redirects if allowed.
|
||||
history = [resp for resp in gen] if allow_redirects else []
|
||||
|
||||
# Shuffle things around if there's history.
|
||||
if history:
|
||||
# Insert the first (original) request at the start
|
||||
history.insert(0, r)
|
||||
# Get the last request made
|
||||
r = history.pop()
|
||||
r.history = tuple(history)
|
||||
|
||||
return r
|
||||
|
||||
def get_adapter(self, url):
|
||||
@@ -365,7 +490,12 @@ class Session(SessionRedirectMixin):
|
||||
return adapter
|
||||
|
||||
# Nothing matches :-/
|
||||
raise InvalidSchema('No connection adapters were found for \'%s\'' % url)
|
||||
raise InvalidSchema("No connection adapters were found for '%s'" % url)
|
||||
|
||||
def close(self):
|
||||
"""Closes all adapters and as such the session"""
|
||||
for _, v in self.adapters.items():
|
||||
v.close()
|
||||
|
||||
def mount(self, prefix, adapter):
|
||||
"""Registers a connection adapter to a prefix."""
|
||||
@@ -379,7 +509,7 @@ class Session(SessionRedirectMixin):
|
||||
setattr(self, attr, value)
|
||||
|
||||
|
||||
def session(**kwargs):
|
||||
def session():
|
||||
"""Returns a :class:`Session` for context-management."""
|
||||
|
||||
return Session(**kwargs)
|
||||
return Session()
|
||||
|
||||
@@ -8,6 +8,30 @@ Data structures that power Requests.
|
||||
|
||||
"""
|
||||
|
||||
import os
|
||||
from itertools import islice
|
||||
|
||||
|
||||
class IteratorProxy(object):
|
||||
"""docstring for IteratorProxy"""
|
||||
def __init__(self, i):
|
||||
self.i = i
|
||||
# self.i = chain.from_iterable(i)
|
||||
|
||||
def __iter__(self):
|
||||
return self.i
|
||||
|
||||
def __len__(self):
|
||||
if hasattr(self.i, '__len__'):
|
||||
return len(self.i)
|
||||
if hasattr(self.i, 'len'):
|
||||
return self.i.len
|
||||
if hasattr(self.i, 'fileno'):
|
||||
return os.fstat(self.i.fileno()).st_size
|
||||
|
||||
def read(self, n):
|
||||
return "".join(islice(self.i, None, n))
|
||||
|
||||
|
||||
class CaseInsensitiveDict(dict):
|
||||
"""Case-insensitive Dictionary
|
||||
|
||||
@@ -19,49 +19,16 @@ import zlib
|
||||
from netrc import netrc, NetrcParseError
|
||||
|
||||
from . import __version__
|
||||
from . import certs
|
||||
from .compat import parse_http_list as _parse_list_header
|
||||
from .compat import quote, quote_plus, urlparse, basestring, bytes, str, OrderedDict
|
||||
from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse
|
||||
from .cookies import RequestsCookieJar, cookiejar_from_dict
|
||||
|
||||
_hush_pyflakes = (RequestsCookieJar,)
|
||||
|
||||
CERTIFI_BUNDLE_PATH = None
|
||||
try:
|
||||
# see if requests's own CA certificate bundle is installed
|
||||
from . import certs
|
||||
path = certs.where()
|
||||
if os.path.exists(path):
|
||||
CERTIFI_BUNDLE_PATH = certs.where()
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
NETRC_FILES = ('.netrc', '_netrc')
|
||||
|
||||
# common paths for the OS's CA certificate bundle
|
||||
POSSIBLE_CA_BUNDLE_PATHS = [
|
||||
# Red Hat, CentOS, Fedora and friends (provided by the ca-certificates package):
|
||||
'/etc/pki/tls/certs/ca-bundle.crt',
|
||||
# Ubuntu, Debian, and friends (provided by the ca-certificates package):
|
||||
'/etc/ssl/certs/ca-certificates.crt',
|
||||
# FreeBSD (provided by the ca_root_nss package):
|
||||
'/usr/local/share/certs/ca-root-nss.crt',
|
||||
# openSUSE (provided by the ca-certificates package), the 'certs' directory is the
|
||||
# preferred way but may not be supported by the SSL module, thus it has 'ca-bundle.pem'
|
||||
# as a fallback (which is generated from pem files in the 'certs' directory):
|
||||
'/etc/ssl/ca-bundle.pem',
|
||||
]
|
||||
|
||||
|
||||
def get_os_ca_bundle_path():
|
||||
"""Try to pick an available CA certificate bundle provided by the OS."""
|
||||
for path in POSSIBLE_CA_BUNDLE_PATHS:
|
||||
if os.path.exists(path):
|
||||
return path
|
||||
return None
|
||||
|
||||
# if certifi is installed, use its CA bundle;
|
||||
# otherwise, try and use the OS bundle
|
||||
DEFAULT_CA_BUNDLE_PATH = CERTIFI_BUNDLE_PATH or get_os_ca_bundle_path()
|
||||
DEFAULT_CA_BUNDLE_PATH = certs.where()
|
||||
|
||||
|
||||
def dict_to_sequence(d):
|
||||
@@ -73,6 +40,15 @@ def dict_to_sequence(d):
|
||||
return d
|
||||
|
||||
|
||||
def super_len(o):
|
||||
if hasattr(o, '__len__'):
|
||||
return len(o)
|
||||
if hasattr(o, 'len'):
|
||||
return o.len
|
||||
if hasattr(o, 'fileno'):
|
||||
return os.fstat(o.fileno()).st_size
|
||||
|
||||
|
||||
def get_netrc_auth(url):
|
||||
"""Returns the Requests tuple auth for a given url from netrc."""
|
||||
|
||||
@@ -113,7 +89,7 @@ def guess_filename(obj):
|
||||
"""Tries to guess the filename of the given object."""
|
||||
name = getattr(obj, 'name', None)
|
||||
if name and name[0] != '<' and name[-1] != '>':
|
||||
return name
|
||||
return os.path.basename(name)
|
||||
|
||||
|
||||
def from_key_val_list(value):
|
||||
@@ -254,7 +230,6 @@ def unquote_header_value(value, is_filename=False):
|
||||
return value
|
||||
|
||||
|
||||
|
||||
def dict_from_cookiejar(cj):
|
||||
"""Returns a key/value dictionary from a CookieJar.
|
||||
|
||||
@@ -277,8 +252,7 @@ def add_dict_to_cookiejar(cj, cookie_dict):
|
||||
"""
|
||||
|
||||
cj2 = cookiejar_from_dict(cookie_dict)
|
||||
for cookie in cj2:
|
||||
cj.set_cookie(cookie)
|
||||
cj.update(cj2)
|
||||
return cj
|
||||
|
||||
|
||||
@@ -492,11 +466,9 @@ def default_user_agent():
|
||||
if _implementation == 'CPython':
|
||||
_implementation_version = platform.python_version()
|
||||
elif _implementation == 'PyPy':
|
||||
_implementation_version = '%s.%s.%s' % (
|
||||
sys.pypy_version_info.major,
|
||||
_implementation_version = '%s.%s.%s' % (sys.pypy_version_info.major,
|
||||
sys.pypy_version_info.minor,
|
||||
sys.pypy_version_info.micro
|
||||
)
|
||||
sys.pypy_version_info.micro)
|
||||
if sys.pypy_version_info.releaselevel != 'final':
|
||||
_implementation_version = ''.join([_implementation_version, sys.pypy_version_info.releaselevel])
|
||||
elif _implementation == 'Jython':
|
||||
@@ -513,11 +485,10 @@ def default_user_agent():
|
||||
p_system = 'Unknown'
|
||||
p_release = 'Unknown'
|
||||
|
||||
return " ".join([
|
||||
'python-requests/%s' % __version__,
|
||||
'%s/%s' % (_implementation, _implementation_version),
|
||||
'%s/%s' % (p_system, p_release),
|
||||
])
|
||||
return " ".join(['python-requests/%s' % __version__,
|
||||
'%s/%s' % (_implementation, _implementation_version),
|
||||
'%s/%s' % (p_system, p_release)])
|
||||
|
||||
|
||||
def default_headers():
|
||||
return {
|
||||
@@ -550,7 +521,7 @@ def parse_header_links(value):
|
||||
|
||||
for param in params.split(";"):
|
||||
try:
|
||||
key,value = param.split("=")
|
||||
key, value = param.split("=")
|
||||
except ValueError:
|
||||
break
|
||||
|
||||
@@ -594,3 +565,27 @@ def guess_json_utf(data):
|
||||
return 'utf-32-le'
|
||||
# Did not detect a valid UTF-32 ascii-range character
|
||||
return None
|
||||
|
||||
|
||||
def prepend_scheme_if_needed(url, new_scheme):
|
||||
'''Given a URL that may or may not have a scheme, prepend the given scheme.
|
||||
Does not replace a present scheme with the one provided as an argument.'''
|
||||
scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)
|
||||
|
||||
# urlparse is a finicky beast, and sometimes decides that there isn't a
|
||||
# netloc present. Assume that it's being over-cautious, and switch netloc
|
||||
# and path if urlparse decided there was no netloc.
|
||||
if not netloc:
|
||||
netloc, path = path, netloc
|
||||
|
||||
return urlunparse((scheme, netloc, path, params, query, fragment))
|
||||
|
||||
|
||||
def get_auth_from_url(url):
|
||||
"""Given a url with authentication components, extract them into a tuple of
|
||||
username,password."""
|
||||
if url:
|
||||
parsed = urlparse(url)
|
||||
return (parsed.username, parsed.password)
|
||||
else:
|
||||
return ('', '')
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pytest
|
||||
sphinx
|
||||
py==1.4.12
|
||||
pytest==2.3.4
|
||||
invoke==0.2.0
|
||||
2
setup.py
2
setup.py
@@ -17,7 +17,7 @@ if sys.argv[-1] == 'publish':
|
||||
packages = [
|
||||
'requests',
|
||||
'requests.packages',
|
||||
'requests.packages.charade',
|
||||
'requests.packages.charade',
|
||||
'requests.packages.urllib3',
|
||||
'requests.packages.urllib3.packages',
|
||||
'requests.packages.urllib3.packages.ssl_match_hostname'
|
||||
|
||||
32
tasks.py
Normal file
32
tasks.py
Normal file
@@ -0,0 +1,32 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import requests
|
||||
from invoke import run, task
|
||||
|
||||
@task
|
||||
def test():
|
||||
run('py.test', pty=True)
|
||||
|
||||
@task
|
||||
def deps():
|
||||
print('Vendoring urllib3...')
|
||||
|
||||
run('rm -fr requests/packages/urllib3')
|
||||
run('git clone https://github.com/shazow/urllib3.git')
|
||||
run('mv urllib3/urllib3 requests/packages/')
|
||||
run('rm -fr urllib3')
|
||||
|
||||
print('Vendoring Charade...')
|
||||
|
||||
run('rm -fr requests/packages/charade')
|
||||
run('git clone https://github.com/sigmavirus24/charade.git')
|
||||
run('mv charade/charade requests/packages/')
|
||||
run('rm -fr charade')
|
||||
|
||||
@task
|
||||
def certs():
|
||||
print('Grabbing latest CA Bundle...')
|
||||
r = requests.get('https://raw.github.com/kennethreitz/certifi/master/certifi/cacert.pem')
|
||||
|
||||
with open('requests/cacert.pem', 'w') as f:
|
||||
f.write(r.content)
|
||||
183
test_requests.py
183
test_requests.py
@@ -3,18 +3,29 @@
|
||||
|
||||
"""Tests for Requests."""
|
||||
|
||||
from __future__ import division
|
||||
import json
|
||||
import os
|
||||
import unittest
|
||||
import pickle
|
||||
|
||||
import requests
|
||||
from requests.auth import HTTPDigestAuth
|
||||
from requests.compat import str
|
||||
|
||||
try:
|
||||
import StringIO
|
||||
except ImportError:
|
||||
import io as StringIO
|
||||
|
||||
HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')
|
||||
|
||||
|
||||
def httpbin(*suffix):
|
||||
"""Returns url for HTTPBIN resource."""
|
||||
return HTTPBIN + '/'.join(suffix)
|
||||
|
||||
|
||||
class RequestsTestCase(unittest.TestCase):
|
||||
|
||||
_multiprocess_can_split_ = True
|
||||
@@ -27,9 +38,6 @@ class RequestsTestCase(unittest.TestCase):
|
||||
"""Teardown."""
|
||||
pass
|
||||
|
||||
def test_assertion(self):
|
||||
assert 1
|
||||
|
||||
def test_entry_points(self):
|
||||
|
||||
requests.session
|
||||
@@ -53,6 +61,11 @@ class RequestsTestCase(unittest.TestCase):
|
||||
assert pr.url == req.url
|
||||
assert pr.body == 'life=42'
|
||||
|
||||
def test_no_content_length(self):
|
||||
get_req = requests.Request('GET', httpbin('get')).prepare()
|
||||
self.assertTrue('Content-Length' not in get_req.headers)
|
||||
head_req = requests.Request('HEAD', httpbin('head')).prepare()
|
||||
self.assertTrue('Content-Length' not in head_req.headers)
|
||||
|
||||
def test_path_is_not_double_encoded(self):
|
||||
request = requests.Request('GET', "http://0.0.0.0/get/test case").prepare()
|
||||
@@ -69,10 +82,6 @@ class RequestsTestCase(unittest.TestCase):
|
||||
self.assertEqual(request.url,
|
||||
"http://example.com/path?key=value&a=b#fragment")
|
||||
|
||||
def test_HTTP_200_OK_GET(self):
|
||||
r = requests.get(httpbin('get'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_HTTP_200_OK_GET_ALTERNATIVE(self):
|
||||
r = requests.Request('GET', httpbin('get'))
|
||||
s = requests.Session()
|
||||
@@ -85,6 +94,10 @@ class RequestsTestCase(unittest.TestCase):
|
||||
r = requests.get(httpbin('redirect', '1'))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
# def test_HTTP_302_ALLOW_REDIRECT_POST(self):
|
||||
# r = requests.post(httpbin('status', '302'), data={'some': 'data'})
|
||||
# self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_HTTP_200_OK_GET_WITH_PARAMS(self):
|
||||
heads = {'User-agent': 'Mozilla/5.0'}
|
||||
|
||||
@@ -99,19 +112,29 @@ class RequestsTestCase(unittest.TestCase):
|
||||
r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_set_cookie_on_301(self):
|
||||
s = requests.session()
|
||||
url = httpbin('cookies/set?foo=bar')
|
||||
r = s.get(url)
|
||||
self.assertTrue(s.cookies['foo'] == 'bar')
|
||||
|
||||
def test_cookie_sent_on_redirect(self):
|
||||
s = requests.session()
|
||||
s.get(httpbin('cookies/set?foo=bar'))
|
||||
r = s.get(httpbin('redirect/1')) # redirects to httpbin('get')
|
||||
self.assertTrue("Cookie" in r.json()["headers"])
|
||||
|
||||
def test_user_agent_transfers(self):
|
||||
|
||||
heads = {
|
||||
'User-agent':
|
||||
'Mozilla/5.0 (github.com/kennethreitz/requests)'
|
||||
'User-agent': 'Mozilla/5.0 (github.com/kennethreitz/requests)'
|
||||
}
|
||||
|
||||
r = requests.get(httpbin('user-agent'), headers=heads)
|
||||
self.assertTrue(heads['User-agent'] in r.text)
|
||||
|
||||
heads = {
|
||||
'user-agent':
|
||||
'Mozilla/5.0 (github.com/kennethreitz/requests)'
|
||||
'user-agent': 'Mozilla/5.0 (github.com/kennethreitz/requests)'
|
||||
}
|
||||
|
||||
r = requests.get(httpbin('user-agent'), headers=heads)
|
||||
@@ -126,8 +149,6 @@ class RequestsTestCase(unittest.TestCase):
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_BASICAUTH_TUPLE_HTTP_200_OK_GET(self):
|
||||
|
||||
|
||||
auth = ('user', 'pass')
|
||||
url = httpbin('basic-auth', 'user', 'pass')
|
||||
|
||||
@@ -158,6 +179,18 @@ class RequestsTestCase(unittest.TestCase):
|
||||
r = s.get(url)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_DIGEST_STREAM(self):
|
||||
|
||||
auth = HTTPDigestAuth('user', 'pass')
|
||||
url = httpbin('digest-auth', 'auth', 'user', 'pass')
|
||||
|
||||
r = requests.get(url, auth=auth, stream=True)
|
||||
self.assertNotEqual(r.raw.read(), b'')
|
||||
|
||||
r = requests.get(url, auth=auth, stream=False)
|
||||
self.assertEqual(r.raw.read(), b'')
|
||||
|
||||
|
||||
def test_DIGESTAUTH_WRONG_HTTP_401_GET(self):
|
||||
|
||||
auth = HTTPDigestAuth('user', 'wrongpass')
|
||||
@@ -237,13 +270,135 @@ class RequestsTestCase(unittest.TestCase):
|
||||
requests.get(url, params={'foo': 'foo'})
|
||||
requests.get(httpbin('ø'), params={'foo': 'foo'})
|
||||
|
||||
def test_unicode_header_name(self):
|
||||
requests.put(httpbin('put'), headers={str('Content-Type'): 'application/octet-stream'}, data='\xff') # compat.str is unicode.
|
||||
|
||||
def test_urlencoded_get_query_multivalued_param(self):
|
||||
|
||||
r = requests.get(httpbin('get'), params=dict(test=['foo', 'baz']))
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertEqual(r.url, httpbin('get?test=foo&test=baz'))
|
||||
|
||||
def test_different_encodings_dont_break_post(self):
|
||||
r = requests.post(httpbin('post'),
|
||||
data={'stuff': json.dumps({'a': 123})},
|
||||
params={'blah': 'asdf1234'},
|
||||
files={'file': ('test_requests.py', open(__file__, 'rb'))})
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_custom_content_type(self):
|
||||
r = requests.post(httpbin('post'),
|
||||
data={'stuff': json.dumps({'a': 123})},
|
||||
files={'file1': ('test_requests.py', open(__file__, 'rb')),
|
||||
'file2': ('test_requests', open(__file__, 'rb'),
|
||||
'text/py-content-type')})
|
||||
self.assertEqual(r.status_code, 200)
|
||||
self.assertTrue(b"text/py-content-type" in r.request.body)
|
||||
|
||||
def test_hook_receives_request_arguments(self):
|
||||
def hook(resp, **kwargs):
|
||||
assert resp is not None
|
||||
assert kwargs != {}
|
||||
|
||||
requests.Request('GET', HTTPBIN, hooks={'response': hook})
|
||||
|
||||
def test_prepared_request_hook(self):
|
||||
def hook(resp, **kwargs):
|
||||
resp.hook_working = True
|
||||
return resp
|
||||
|
||||
req = requests.Request('GET', HTTPBIN, hooks={'response': hook})
|
||||
prep = req.prepare()
|
||||
|
||||
s = requests.Session()
|
||||
resp = s.send(prep)
|
||||
|
||||
self.assertTrue(hasattr(resp, 'hook_working'))
|
||||
|
||||
def test_links(self):
|
||||
r = requests.Response()
|
||||
r.headers = {
|
||||
'cache-control': 'public, max-age=60, s-maxage=60',
|
||||
'connection': 'keep-alive',
|
||||
'content-encoding': 'gzip',
|
||||
'content-type': 'application/json; charset=utf-8',
|
||||
'date': 'Sat, 26 Jan 2013 16:47:56 GMT',
|
||||
'etag': '"6ff6a73c0e446c1f61614769e3ceb778"',
|
||||
'last-modified': 'Sat, 26 Jan 2013 16:22:39 GMT',
|
||||
'link': ('<https://api.github.com/users/kennethreitz/repos?'
|
||||
'page=2&per_page=10>; rel="next", <https://api.github.'
|
||||
'com/users/kennethreitz/repos?page=7&per_page=10>; '
|
||||
' rel="last"'),
|
||||
'server': 'GitHub.com',
|
||||
'status': '200 OK',
|
||||
'vary': 'Accept',
|
||||
'x-content-type-options': 'nosniff',
|
||||
'x-github-media-type': 'github.beta',
|
||||
'x-ratelimit-limit': '60',
|
||||
'x-ratelimit-remaining': '57'
|
||||
}
|
||||
self.assertEqual(r.links['next']['rel'], 'next')
|
||||
|
||||
def test_cookie_parameters(self):
|
||||
key = 'some_cookie'
|
||||
value = 'some_value'
|
||||
secure = True
|
||||
domain = 'test.com'
|
||||
rest = {'HttpOnly': True}
|
||||
|
||||
jar = requests.cookies.RequestsCookieJar()
|
||||
jar.set(key, value, secure=secure, domain=domain, rest=rest)
|
||||
|
||||
self.assertEqual(len(jar), 1)
|
||||
self.assertTrue('some_cookie' in jar)
|
||||
|
||||
cookie = list(jar)[0]
|
||||
self.assertEqual(cookie.secure, secure)
|
||||
self.assertEqual(cookie.domain, domain)
|
||||
self.assertEqual(cookie._rest['HttpOnly'], rest['HttpOnly'])
|
||||
|
||||
def test_time_elapsed_blank(self):
|
||||
r = requests.get(httpbin('get'))
|
||||
td = r.elapsed
|
||||
total_seconds = ((td.microseconds + (td.seconds + td.days * 24 * 3600)
|
||||
* 10**6) / 10**6)
|
||||
self.assertTrue(total_seconds > 0.0)
|
||||
|
||||
def test_response_is_iterable(self):
|
||||
r = requests.Response()
|
||||
io = StringIO.StringIO('abc')
|
||||
r.raw = io
|
||||
self.assertTrue(next(iter(r)))
|
||||
io.close()
|
||||
|
||||
def test_get_auth_from_url(self):
|
||||
url = 'http://user:pass@complex.url.com/path?query=yes'
|
||||
self.assertEqual(('user', 'pass'),
|
||||
requests.utils.get_auth_from_url(url))
|
||||
|
||||
def test_cannot_send_unprepared_requests(self):
|
||||
r = requests.Request(url=HTTPBIN)
|
||||
self.assertRaises(ValueError, requests.Session().send, r)
|
||||
|
||||
def test_http_error(self):
|
||||
error = requests.exceptions.HTTPError()
|
||||
self.assertEqual(error.response, None)
|
||||
response = requests.Response()
|
||||
error = requests.exceptions.HTTPError(response=response)
|
||||
self.assertEqual(error.response, response)
|
||||
error = requests.exceptions.HTTPError('message', response=response)
|
||||
self.assertEqual(str(error), 'message')
|
||||
self.assertEqual(error.response, response)
|
||||
|
||||
def test_session_pickling(self):
|
||||
r = requests.Request('GET', httpbin('get'))
|
||||
s = requests.Session()
|
||||
|
||||
s = pickle.loads(pickle.dumps(s))
|
||||
|
||||
r = s.send(r.prepare())
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user