Compare commits

..

522 Commits
v1.0.3 ... 2.0

Author SHA1 Message Date
Kenneth Reitz
22701d149a Merge pull request #1500 from gavrie/master
Update urllib3 to d89d508
2013-07-31 18:23:43 -07:00
Kenneth Reitz
d5fd3d3331 Merge pull request #1487 from dpursehouse/rewrite-test_mixed_case_scheme_acceptable
Rewrite test cases to remove dependency on httpbin.org and example.com
2013-07-31 18:23:34 -07:00
Kenneth Reitz
d5a8816926 Merge pull request #1498 from Lukasa/incompleteread
Wrap IncompleteRead in ChunkedEncodingError.
2013-07-31 18:23:14 -07:00
Kenneth Reitz
6b13ecdc85 Merge pull request #1501 from sigmavirus24/remove_setting_of_hooks
Fix duplication of efforts caught by @dofelw
2013-07-31 18:22:58 -07:00
Kenneth Reitz
7eb9a9298e Merge pull request #1506 from Lukasa/mockrequest
Provide 'host' parameter to MockRequest.
2013-07-31 18:22:02 -07:00
Kenneth Reitz
3e6e68baa1 Merge pull request #1507 from buzztabapp/prepared-session-requests
Allow preparing of Requests from Session settings without sending.
2013-07-31 18:21:28 -07:00
Robert Estelle
0ab7a52f7c Merge settings when creating PreparedRequest, don't copy Request. 2013-07-31 13:42:02 -07:00
Robert Estelle
0eccb547a2 Add Session.prepare_request test case. 2013-07-30 23:02:13 -07:00
Robert Estelle
d4385f6fc8 Add Request.copy() test case. 2013-07-30 23:01:56 -07:00
Robert Estelle
ee90f0af60 Shallow copy of Request fields in Request.copy()
This prevents e.g. modifying the headers of a copied request from
affecting the headers of its source and vice versa. Copying is used with
the intent to mutuate, so allowing this kind of mutation of fields makes
sense.

Is a deep copy better?
2013-07-30 22:59:53 -07:00
Robert Estelle
9edba838b3 Skip unneccessary Request.copy in Session.request 2013-07-30 22:59:11 -07:00
Robert Estelle
b5c136152f Prepare requests with session settings separately from sending. 2013-07-30 21:39:36 -07:00
Cory Benfield
6ef3710970 Provide 'host' parameter to MockRequest. 2013-07-30 18:21:37 +01:00
Ian Cordasco
37786613e0 Fix duplication of efforts caught by @dofelw 2013-07-29 08:09:07 -05:00
Gavrie Philipson
ea2b639d31 Update urllib3 to d89d508 2013-07-29 11:29:41 +03:00
Cory Benfield
79f3e69f5e Wrap IncompleteRead in ChunkedEncodingError. 2013-07-28 07:42:17 +01:00
Cory Benfield
9473f15909 Merge pull request #1494 from Lukasa/close
Document the Response.close() method.
2013-07-27 23:04:34 -07:00
Kenneth Reitz
77bd9c4a9d Merge pull request #1476 from sigmavirus24/add_copy_to_prepared_requests
[2.0] Add copy method to PreparedRequest objects
2013-07-26 18:16:00 -07:00
Cory Benfield
f401287afb Document the Response.close() method. 2013-07-25 19:10:42 +01:00
Cory Benfield
3becc47366 Comment markups, courtesy of @sigmavirus24 2013-07-24 16:03:24 +01:00
Kenneth Reitz
7d3d074e66 Merge pull request #1490 from Lukasa/cookiedocs
Better cookie docs on sessions.
2013-07-24 06:12:10 -07:00
Cory Benfield
f40a1d6a8a Better cookie docs on sessions. 2013-07-24 13:25:59 +01:00
David Pursehouse
82c9aa912e Check the response URL in test_uppercase_scheme_redirect
Update the test to check that the URL in the response is the
one that we expect, i.e. the one it was supposed to redirect to.
2013-07-24 17:37:35 +09:00
David Pursehouse
56b5f02551 Rewrite test_uppercase_scheme_redirect to use local httpbin
Instead of redirecting to hard-coded 'example.com', use the URL
defined in `HTTPBIN_URL` with the path set to the 'html' endpoint.
2013-07-24 17:18:16 +09:00
David Pursehouse
c4f16c351c Remove redundant test case test_uppercase_scheme
This test verifies that an upper case scheme ('HTTP') works correctly,
but this is already tested in `test_mixed_case_scheme_acceptable`.
2013-07-24 16:58:09 +09:00
David Pursehouse
012cc33d44 Rewrite test_mixed_case_scheme_acceptable to work with local httpbin
Instead of using hard-coded urls to httpbin.org, use the url defined
in `HTTPBIN_URL` replacing the scheme as necessary to test the mixed
cases.

Refs #1485
2013-07-24 16:36:10 +09:00
David Pursehouse
415fb53cb4 Remove redundant session in test_mixed_case_scheme_acceptable 2013-07-24 12:15:46 +09:00
Kenneth Reitz
b65b8c5ed3 Merge pull request #1486 from sigmavirus24/use_urljoin
Use urlparse.urljointo construct httpbin url
2013-07-23 09:03:05 -07:00
Ian Cordasco
c0cb698191 Derp. Use compat 2013-07-23 10:48:45 -05:00
Ian Cordasco
d2176cf052 Use urlparse.urljointo construct httpbin url 2013-07-23 10:24:30 -05:00
Kenneth Reitz
f71bff1226 Merge pull request #1482 from dpursehouse/fix-testcases-with-proxy
Fix test cases that fail when running behind a proxy
2013-07-23 05:23:35 -07:00
Kenneth Reitz
47f5a0db64 Merge pull request #1483 from dpursehouse/httpbin-url-without-trailing-slash
Test cases fail when `HTTPBIN_URL` does not have trailing slash
2013-07-23 05:23:11 -07:00
David Pursehouse
9e771aa79c Fix test cases that fail when running behind a proxy
When sending a request via `Session.send()` the proxies must be
explicitly given with the `proxies` argument.  This is not done
in the test cases, which means that they fail when run on a system
that is behind a proxy.

Update test cases to make sure the proxies are set in the sessions.
2013-07-23 16:55:27 +09:00
David Pursehouse
29db7b7216 Test cases fail when HTTPBIN_URL does not have trailing slash
Test cases can be run against a local httpbin server defined by
the `HTTPBIN_URL` environment variable, but it causes tests to
fail if the given URL does not end with a slash.

Ensure that the URL always ends with a slash.
2013-07-23 15:29:36 +09:00
Kenneth Reitz
66de6b528d Merge pull request #1479 from dpursehouse/fix-warnings
Fix a few warnings flagged by PyDev
2013-07-22 01:31:16 -07:00
David Pursehouse
4f64938ff0 Fix a few warnings flagged by PyDev
- Unused import of urlparse
- Unnecessary definition of variable
- Incorrect indentation
2013-07-22 17:14:45 +09:00
Kenneth Reitz
e5597dfcce Merge pull request #1478 from dpursehouse/add-me-to-authors
Add myself to the authors list
2013-07-22 00:09:35 -07:00
David Pursehouse
787c1e8c6e Add myself to the authors list 2013-07-22 15:19:10 +09:00
Cory Benfield
ec6584f2d1 Merge pull request #1477 from dpursehouse/other-auth-basic-example
Add a simple example of custom authentication in the documentation
2013-07-21 23:04:31 -07:00
David Pursehouse
62f0df4434 Add a simple example of custom authentication in the documentation
Refs #1471
2013-07-22 09:14:33 +09:00
David Pursehouse
5cdcf58b3b Wrap long lines in the authentication documentation 2013-07-22 09:08:47 +09:00
Ian Cordasco
b84547d786 Add copy method to PreparedRequest objects 2013-07-20 17:08:35 -04:00
Kenneth Reitz
d83919a6f7 Merge pull request #1474 from Kwpolska/docfix
Some tiny fixes to the documentation
2013-07-20 05:15:06 -07:00
Kwpolska
2ef782b8e6 further treatment
Signed-off-by: Kwpolska <kwpolska@gmail.com>
2013-07-20 13:54:33 +02:00
Kwpolska
b738c97ec3 session object should be uppercase
Signed-off-by: Kwpolska <kwpolska@gmail.com>
2013-07-20 13:39:50 +02:00
Kwpolska
022c370bcd fixed underline
Signed-off-by: Kwpolska <kwpolska@gmail.com>
2013-07-20 13:31:37 +02:00
Kwpolska
587385bfe0 Grammar fixes and such
Signed-off-by: Kwpolska <kwpolska@gmail.com>
2013-07-20 12:56:25 +02:00
Kwpolska
ca033b83fe Some cosmetic updates to the docs
Signed-off-by: Kwpolska <kwpolska@gmail.com>
2013-07-20 12:12:57 +02:00
Cory Benfield
949a29c34f Merge pull request #1470 from Lukasa/carset
Remove charset from JSON types: not valid.
2013-07-19 06:45:56 -07:00
Cory Benfield
be884341f4 Merge pull request #1465 from dpursehouse/doc-netrc-authentication
Improve documentation of netrc authentication
2013-07-19 06:45:17 -07:00
David Pursehouse
c88fd8cc9d Move netrc authentication documentation under the Basic Auth section 2013-07-19 22:37:57 +09:00
Cory Benfield
8385fb397b Remove charset from JSON types: not valid. 2013-07-19 14:03:20 +01:00
Cory Benfield
e0df4b5f18 Merge pull request #1466 from s7v7nislands/fix_doc
Fix doc
2013-07-19 04:12:29 -07:00
s7v7nislands
39ad5e7388 merge 2013-07-19 17:12:01 +08:00
s7v7nislands
0df505bd7c fix doc 2013-07-19 17:04:53 +08:00
David Pursehouse
6f6a920a68 Improve documentation of netrc authentication
The documentation does not make it clear that when the credentials
from netrc are used, Requests authenticates with HTTP Basic Auth.

I just spent ages trying to figure out why it wasn't working, and
it was because although the credentials in the netrc were correct,
the server actually required HTTP Digest Auth.

Add a section in the authentication documentation to make it clear
that HTTP Basic Auth is used.
2013-07-19 17:30:54 +09:00
Cory Benfield
32293655ff Merge pull request #1464 from dpursehouse/document-none-not-sent
Fix #1322: Add note in docs about None not being sent as data
2013-07-19 01:19:55 -07:00
David Pursehouse
07ad75ee04 Fix #1322: Add note in docs about None not being sent as data
In the case:

  payload = {'key1': 'value1', 'key2': 'value2', 'key3': None}
  r = requests.get("http://httpbin.org", params=payload)

the parameter `key3` will not be sent as a parameter in the URL.

Mention this in the documentation.
2013-07-19 17:14:59 +09:00
Kenneth Reitz
e4b41320e6 Merge pull request #1463 from AudriusButkevicius/master
Update urllib3 to a43319f
2013-07-18 15:00:28 -07:00
Audrius Butkevicius
9f119ee420 Update urllib3 to a43319f 2013-07-18 21:06:19 +00:00
Kenneth Reitz
e0df2168b7 Merge remote-tracking branch 'origin/master' 2013-07-16 02:20:28 -04:00
Kenneth Reitz
d7e8073198 badge.fury.io 2013-07-16 02:20:23 -04:00
Kenneth Reitz
65a0fd88a9 Merge pull request #1440 from fcurella/patch-0
unquote double-quotes cookie values
2013-07-15 06:54:00 -07:00
Kenneth Reitz
4b7cf389e4 Merge pull request #1439 from voberoi/master
Make sure netrc doesn't override any authentication settings explicitly set by the client
2013-07-15 06:22:53 -07:00
Kenneth Reitz
86d466c745 Merge pull request #1441 from Lukasa/1395
Remove urllib3-specific kwargs from non-urllib3 branch.
2013-07-15 06:22:41 -07:00
Kenneth Reitz
de9d84489b Merge pull request #1456 from phndiaye/master
Changed the "im_used" informational status code for the value given by IANA (226)
2013-07-15 06:21:31 -07:00
Philippe Ndiaye
18a736fbe6 Set 208 status_code to "already_reported" 2013-07-13 11:08:01 +02:00
Philippe Ndiaye
7bdf37bc27 Changed the "im_used" informational status code for the value given by IANA (226)
See RFC 3229 at http://tools.ietf.org/html/rfc3229#section-10.4.1 and HTTP status codes at http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
2013-07-13 09:55:50 +02:00
Kenneth Reitz
41517e2111 Delete invokefile.py 2013-07-06 14:04:50 +10:00
Cory Benfield
555472bf1e Remove urllib3-specific kwargs from general code 2013-07-04 10:34:43 +01:00
Flavio Curella
7da16c584a Added myself to AUTHORS 2013-07-01 15:54:59 -05:00
Flavio Curella
a8cf5b8502 keep the double quotes, but don't escape them 2013-07-01 15:48:48 -05:00
Flavio Curella
cdab4fabf4 unquote double-quotes cookie values 2013-07-01 13:51:40 -05:00
Vikram Oberoi
d9c49ad30d Add test to verify .netrc authentication behavior.
Here's what should happen:

- If no credentials are given, use netrc if there's a netrc entry.
- If credentials are given, they should override netrc.
2013-06-27 17:16:42 -04:00
Vikram Oberoi
188e7609b3 .netrc settings shouldn't blow away explicit auth settings on a session 2013-06-27 16:43:40 -04:00
Cory Benfield
2b62980e26 Merge pull request #1437 from lukaszb/patch-1
Fixed wrong method call at streaming example
2013-06-27 11:18:56 -07:00
Lukasz Balcerzak
805abee9b4 Fixed wrong method call at streaming example
405 is returned if POST request is performed to http://httpbin.org/stream/20
2013-06-27 13:37:39 +02:00
Kenneth Reitz
a1bb7fb0d2 Merge pull request #1435 from chinux23/master
@1434 https://github.com/kennethreitz/requests/issues/1434
2013-06-25 15:49:04 -07:00
Chen Huang
9083735963 @1434 Fix https://github.com/kennethreitz/requests/issues/1434 2013-06-25 18:38:59 -04:00
Cory Benfield
dcf20c4cfe Merge pull request #1429 from Lukasa/docs
Packaging warning, in via @andrewgross.
2013-06-24 00:48:48 -07:00
Cory Benfield
d57a5efab3 Packaging warning, in via @andrewgross. 2013-06-22 15:02:18 +01:00
Kenneth Reitz
3781c96125 Merge pull request #1425 from Lukasa/stream
Use new urllib3 'stream' parameter.
2013-06-21 02:23:10 -07:00
Cory Benfield
1faa76a86f Use the new urllib3 stream generator. 2013-06-18 17:56:35 +01:00
Cory Benfield
ecf57cac5c Update urllib3 to cffbd6b317 2013-06-18 17:47:23 +01:00
Kenneth Reitz
25db83fe85 Merge pull request #1419 from kevinburke/exception-links
Exception links
2013-06-14 22:50:44 -07:00
Kevin Burke
ba0db9db86 Add self to AUTHORS 2013-06-12 15:56:10 -07:00
Kevin Burke
a6415cf895 Link to the actual exception references
Sphinx has a neat cross-referencing feature where if you include the tilde
character in front of a :py: class, it'll link to the full object but provide
only the last part of class name in the text. For more info see
http://sphinx-doc.org/domains.html#cross-referencing-syntax
2013-06-12 15:53:09 -07:00
Kenneth Reitz
df935f5b03 Merge pull request #1414 from jschneier/master
fixed typo in docs
2013-06-12 11:12:17 -07:00
Josh Schneier
43a64d9515 fix doc typo 2013-06-09 11:14:14 -04:00
Kenneth Reitz
70a6d035d9 Merge pull request #1315 from reclosedev/fix-redirects
Don't reuse the same prepared request for all redirects
2013-06-08 03:53:16 -07:00
Roman Haritonov
716b627c1e Don't reuse PreparedRequest on redirects 2013-06-08 14:41:34 +04:00
Roman Haritonov
798a1ffdec new failing test_requests_in_history_are_not_overridden() 2013-06-08 14:41:34 +04:00
Kenneth Reitz
8028103b71 Merge pull request #1400 from jam/master
Retrieve environment proxies using standard library functions
2013-06-08 03:20:18 -07:00
Kenneth Reitz
8430e6ccbb Merge pull request #1334 from rcarz/master
resolve_redirects no longer throws an InvalidSchema exception when the scheme is uppercase
2013-06-08 03:18:25 -07:00
Kenneth Reitz
d4c461042b Merge pull request #1385 from ViktorHaag/master
Cope with mixed-case URL schemes (like 'HTTP') by lower-ifying 'url' string before calling startswith() on it
2013-06-08 03:17:38 -07:00
Kenneth Reitz
60b1a0ba13 Merge pull request #1381 from expandrive/master
Don't force chunked transfer on 0-length file-like object.
2013-06-08 03:16:32 -07:00
Kenneth Reitz
cfd84d74ee Merge pull request #1392 from revolunet/patch-2
Update quickstart.rst
2013-06-08 03:16:21 -07:00
Kenneth Reitz
eec9bdd434 Merge pull request #1408 from wasw100/master
cookies.morsel_to_cookie morsel['expires'] can't be strtime, and morsel['max-age'] convert to expires problem repair
2013-06-08 03:14:32 -07:00
Kenneth Reitz
f74d5e96dd Merge pull request #1412 from t-8ch/update_urllib3
update urllib3 to 60ba176f5d
2013-06-08 03:13:34 -07:00
Kenneth Reitz
e63e68ef48 Merge pull request #1413 from Lukasa/docs
Assorted docs updates
2013-06-08 03:13:14 -07:00
Cory Benfield
73425fbffb Remove development reference from docs sidebar. 2013-06-08 11:10:07 +01:00
Cory Benfield
f53aed1611 Document blocking calls. 2013-06-08 11:09:39 +01:00
Thomas Weißschuh
2ed976ea71 update urllib3 to 60ba176f5d 2013-06-08 08:22:15 +00:00
wasw100
767f758aac cookies.morsel_to_cookie morsel['expires'] can't be strtime, and max-age convert to expires problem repair 2013-06-07 00:02:45 +08:00
wasw100
961790f95c cookies.morsel_to_cookie(morsel) raise TypeError repaired.
morsel_to_cookie(mosel) method raise TypeError: create_cookie() got unexpected keyword arguments: ['path_specified', 'domain_specified', 'port_specified', 'domain_initial_dot'].

so we should remove these param from create_cookie(...)
2013-06-06 19:15:09 +08:00
James Clarke
f705c41c1d Added myself to AUTHORS. 2013-05-31 18:19:42 -07:00
James Clarke
93be6916f9 Use urllib to retrieve environment proxies.
This has the added benefit of including proxies defined by the OS X System Configuration framework and in the Windows registry, rather than only checking os.environ.
2013-05-31 18:19:34 -07:00
Julien Bouquillon
340931ba8e Update quickstart.rst
rephrase misleading info about `raise_for_status`
2013-05-29 12:57:49 +03:00
Bob Carroll
2c5ea69e5d rebased with upstream/master 2013-05-26 14:46:36 -07:00
Bob Carroll
d59510481a this didn't merge properly 2013-05-26 14:45:49 -07:00
Bob Carroll
f3036adc4f added assertion to test_uppercase_scheme_redirect for the response code 2013-05-26 14:44:37 -07:00
Bob Carroll
489a412f96 I probably should add my name too 2013-05-26 14:44:37 -07:00
Bob Carroll
72e155e529 resolve_redirects now checks for a scheme before converting the scheme to lowercase, added tests or the scheme casing 2013-05-26 14:44:22 -07:00
Bob Carroll
e715d7184b resolve_redirects no longer throws an InvalidSchema exception when the scheme is uppercase 2013-05-26 14:43:00 -07:00
Kenneth Reitz
3bb13f8fbb v1.2.3 2013-05-25 12:48:10 -04:00
Viktor Haag
01993d21dc added tests for mixed-case scheme URLs, changed adapters passing down URLs into urllib3 by lower-ifying them so that the underlying pool manager can effectively pool by scheme as dictionary key 2013-05-24 16:14:14 -04:00
Viktor Haag
5e94f38001 - fixed func call syntax on lower to lower()
- added test cases for trying to test GETS on mixed-case schemas
2013-05-24 14:01:30 -04:00
Viktor Haag
3004ad5398 Lower-ify url before checking against prefix with startswith() 2013-05-24 11:26:00 -04:00
Jeff Mancuso
e7c9bbb96f Only switch to chunked if we don't know the length of a file like object. This fixes the case of trying to upload a 0-length file - chunked upload was being forced. Services like S3 that disallow chunked upload will fail. 2013-05-23 11:21:29 -04:00
Kenneth Reitz
00ab8fbfea Merge remote-tracking branch 'origin/master' 2013-05-21 17:43:53 -04:00
Kenneth Reitz
9930c9c737 v1.2.2 2013-05-21 17:43:13 -04:00
Kenneth Reitz
2ee814d924 Merge pull request #1375 from t-8ch/fix_digest_auth
don't replace 'Digest' in digest header value
2013-05-21 14:33:25 -07:00
Kenneth Reitz
8b55a253b1 Merge remote-tracking branch 'origin/master' 2013-05-21 17:27:15 -04:00
Kenneth Reitz
786fe94ac4 nope 2013-05-21 17:27:06 -04:00
Kenneth Reitz
fede8e5af3 Merge pull request #1376 from papaeye/master
Fix typo, %t -> %r
2013-05-21 14:25:49 -07:00
papaeye
2a34335dc3 Fix #1374 2013-05-22 04:30:19 +09:00
Thomas Weißschuh
3b0d8b8e39 don't replace 'Digest' in digest header value
See https://github.com/kennethreitz/requests/issues/1358
2013-05-21 17:48:30 +00:00
papaeye
715a57dec8 Fix typo, %t -> %r 2013-05-22 02:20:51 +09:00
Kenneth Reitz
2aabb71dc8 Merge pull request #1373 from ib-lundgren/unicode_fields_to_urllib
Only pass unicode multipart fieldnames to urllib3.
2013-05-21 06:19:48 -07:00
Ib Lundgren
003c795afe Only pass unicode fieldnames to urllib3. 2013-05-21 09:46:28 +01:00
Kenneth Reitz
1621015e00 Merge pull request #1343 from gazpachoking/#1321
Refactor merge_kwargs
2013-05-20 19:14:54 -07:00
Chase Sterling
98114245c6 Refactor merge_kwargs for clarity and to fix a few bugs 2013-05-20 21:20:51 -04:00
Kenneth Reitz
8eb4243f12 Merge remote-tracking branch 'origin/master' 2013-05-20 16:10:43 -04:00
Kenneth Reitz
569b59f495 v1.2.1 2013-05-20 16:10:23 -04:00
Kenneth Reitz
8f42369aa9 Merge pull request #1356 from Zoramite/ConnectionPoolArgs
Adding an argument to the adapter for passing a block argument
2013-05-20 13:05:02 -07:00
Kenneth Reitz
f4e7809e7c Merge pull request #1361 from Lukasa/1360
Always percent-encode location headers.
2013-05-20 13:04:08 -07:00
Kenneth Reitz
2afe3b2e99 Merge pull request #1370 from sigmavirus24/origin_req_host
Add an extra property for Python 3.3
2013-05-20 12:57:38 -07:00
Ian Cordasco
9ce7f0bf5b Attempt a fix for @michaelhelmick 2013-05-20 10:58:03 -04:00
Kenneth Reitz
7430a49f45 Merge remote-tracking branch 'origin/master' 2013-05-17 09:31:11 +02:00
Kenneth Reitz
538dbaecbe download image! 2013-05-17 09:31:01 +02:00
Kenneth Reitz
5943afa25d Merge pull request #1363 from dave-shawley/master
Fix for #1362
2013-05-16 23:23:04 -07:00
Dave Shawley
6e76ab7188 Fix for #1362.
`PreparedRequest.prepare_url` incorrectly applied IDNA encoding to the
URLs entire `netloc`.  It should only be encoding the hostname portion
of the URL.  IDNA encoding was limiting the user info, host, and port
segments to be a maximum of 63 characters which causes problems for
all by the most trivial user + password combinations.

- Replaced usage of `urlparse` in `PreparedRequest.prepare_url` with
  `urllib3` equivalent.
- Modified IDNA encoding section so that it only encodes the host
  portion of the URL.
2013-05-16 13:12:34 -04:00
Cory Benfield
2b6ebd2521 Always percent-encode location headers. 2013-05-16 12:02:46 +01:00
Kenneth Reitz
eacb91afb1 Merge pull request #1327 from ambv/adapter_order
Fixes #1320: transport adapters stored in ordered form
2013-05-15 09:43:57 -07:00
Łukasz Langa
4c8af1fff4 Fixes #1320: transport adapters stored in ordered form 2013-05-15 13:34:09 +02:00
Randy Merrill
c03e14242b Adding the _pool_bloc to the list of attrs. 2013-05-11 18:30:31 -07:00
Randy Merrill
9cb3d6444d Fixing the call to init_poolmanagers to correctly unpickle the adapter. 2013-05-11 18:13:08 -07:00
Randy Merrill
053613688b Moving the order of the arguments in the init to not interfere with existing usage. 2013-05-11 18:04:43 -07:00
Randy Merrill
2eb682671d Adding an argument to the adapter for passing a block argument to the connection pool.
This allows for blocking when using threading to prevent the pool from creating more connections that the max-size allows.

Specifically was seeing the following errors without the block=True:

    WARNING:requests.packages.urllib3.connectionpool:HttpConnectionPool is full, discarding connection: www.example.com
2013-05-10 23:46:54 -07:00
Kenneth Reitz
e7786ec14f Merge pull request #1355 from sigmavirus24/fix_links
Prevent a KeyError when there's no link headers
2013-05-09 01:03:49 -07:00
Ian Cordasco
88fda94218 Prevent a KeyError when there's no link headers 2013-05-08 23:26:49 -04:00
Kenneth Reitz
b599886beb Merge pull request #1352 from nicoddemus/docs-link-on-readme
Adding link to documentation in README.rst
2013-05-07 10:01:32 -07:00
Kenneth Reitz
7aedc6bb22 Merge pull request #1342 from mexicat/master
Fixed example in authentication docs
2013-05-07 10:01:18 -07:00
Bruno Oliveira
efefc6b778 Adding link to documentation in README.rst
Just adding a link to the documentation at python-requests.org
2013-05-06 23:46:59 -03:00
Kenneth Reitz
50e592209f Merge pull request #1347 from hobarrera/master
Issue #749: Add optional SNI support for python2.
2013-05-04 13:11:46 -07:00
Hugo Osvaldo Barrera
18857a0eed Issue #749: Add optional SNI support for python2.
SNI support will be enabled for python2 if ndg-httpsclient and pyopenssl are available.
2013-05-03 21:05:42 -03:00
giacomo
8781b56a0e Fixed example in authentication docs 2013-05-02 18:16:19 +03:00
Kenneth Reitz
e585e496c9 Merge pull request #1341 from cdunklau/docstring_test_1329
Minor update to tests, added docstrings
2013-05-01 11:40:02 -07:00
Colin Dunklau
f93275c47f Minor update to tests, added docstrings
Requested by @sigmavirus24.
2013-05-01 12:51:09 -05:00
Kenneth Reitz
4f83ef8328 Merge pull request #1314 from duailibe/master
Fix small typo in the Support docs
2013-05-01 10:29:40 -07:00
Kenneth Reitz
0f1bb96f01 Merge pull request #1337 from amercader/master
Fix minor issues on the API docs
2013-05-01 10:28:18 -07:00
Kenneth Reitz
e6e9b550c0 Merge pull request #1339 from cdunklau/case_insensitive_headers_cleanup
Rewrite CaseInsensitiveDict to work correctly/sanely
2013-05-01 10:27:58 -07:00
Kenneth Reitz
f8f1db8458 Merge pull request #1340 from ambv/tox_ini
Remove 3.1 and 3.2 from setup.py, add tox.ini to simplify testing on multiple versions
2013-05-01 10:27:02 -07:00
Łukasz Langa
7e825acd9b Remove Python 3.1 and 3.2 from the trove classifiers in setup.py 2013-05-01 19:26:12 +02:00
Colin Dunklau
f7596c75dc Rewrite CaseInsensitiveDict to work correctly/sanely
Fixes #649 and #1329 by making Session.headers a CaseInsensitiveDict,
and fixing the implementation of CID. Credit for the brilliant idea
to map `lowercased_key -> (cased_key, mapped_value)` goes to
@gazpachoking, thanks a bunch.

Changes from original implementation of CaseInsensitiveDict:

1.  CID is rewritten as a subclass of `collections.MutableMapping`.
2.  CID remembers the case of the last-set key, but `__setitem__`
    and `__delitem__` will handle keys without respect to case.
3.  CID returns the key case as remembered for the `keys`, `items`,
    and `__iter__` methods.
4.  Query operations (`__getitem__` and `__contains__`) are done in
    a case-insensitive manner: `cid['foo']` and `cid['FOO']` will
    return the same value.
5.  The constructor as well as `update` and `__eq__` have undefined
    behavior when given multiple keys that have the same `lower()`.
6.  The new method `lower_items` is like `iteritems`, but keys are
    all lowercased.
7.  CID raises `KeyError` for `__getitem__` as normal dicts do. The
    old implementation returned
6.  The `__repr__` now makes it obvious that it's not a normal dict.

See PR #1333 for the discussions that lead up to this implementation
2013-04-30 14:52:27 -05:00
amercader
505d2487e3 Fix API documentation issues
* 'Cookies' and 'Encodings' sections were not built because the
  reference to the functions was wrong.
* 'Exceptions' section had a wrong anchor link ('module-requests', same
   one as the main heading).
* Remove 'decode_gzip' function, which is no longer present.
2013-04-30 16:15:56 +02:00
Kenneth Reitz
ab36f3cc6f Merge pull request #1332 from bboe/urllib3
Update urllib3 to 59de03e6163c6928dc01832ed6e48e9f6c34c795.
2013-04-28 16:58:08 -07:00
Bryce Boe
49ca72c802 Update urllib3 to 59de03e6163c6928dc01832ed6e48e9f6c34c795.
This update includes two fixes:

* https://github.com/shazow/urllib3/issues/149
* https://github.com/shazow/urllib3/issues/174
2013-04-27 14:33:39 -07:00
Kenneth Reitz
d2ba5798ec Merge pull request #1330 from Kwpolska/patch-1
Fixing a tiny typo, noticed while working on the Polish translation
2013-04-27 04:53:37 -07:00
Chris Warrick
c3bef6b0d9 A tiny typo, noticed while working on the Polish translation. 2013-04-27 14:52:35 +03:00
Kenneth Reitz
98dea03710 Merge pull request #1328 from gazpachoking/session_cj_fix
Session CookieJar fix
2013-04-26 00:02:28 -07:00
Chase Sterling
0c609b59ab Fix session CookieJars without breaking more stuff this time 2013-04-25 23:15:50 -04:00
Chase Sterling
a7fef58756 Add another session cookie test 2013-04-25 23:11:43 -04:00
Chase Sterling
3ff8d43801 Fix cookielib import 2013-04-25 22:40:24 -04:00
Chase Sterling
9763a671da Fix crash when session.cookies was not a RequestsCookieJar 2013-04-25 22:34:51 -04:00
Chase Sterling
1866f7596e Add test for session cookiejars other than RequestsCookieJar 2013-04-25 22:32:03 -04:00
Kenneth Reitz
8f3f4e2744 milla 2013-04-20 00:22:13 -04:00
Kenneth Reitz
4438bfcc44 Merge remote-tracking branch 'origin/master' 2013-04-20 00:18:33 -04:00
Kenneth Reitz
65b6ef6036 pt 2013-04-20 00:18:27 -04:00
Lucas Duailibe
1ca423ec80 Fix small typo in the Support docs 2013-04-18 20:59:17 -03:00
Kenneth Reitz
770c3d7548 Merge pull request #1313 from iurisilvio/fix_max_redirects_docs
Fix max_redirects docs issue #1301
2013-04-17 12:39:03 -07:00
Iuri de Silvio
736e8cd735 Fix max_redirects docs issue #1301 2013-04-17 12:27:06 -07:00
Kenneth Reitz
6046fcffe8 Merge pull request #1279 from jemerick/unicode_multipart_post
Unicode strings in multipart post requests
2013-04-16 12:55:58 -07:00
Kenneth Reitz
d13bf9f113 support for draft-tbray-http-legally-restricted-status-02
http://datatracker.ietf.org/doc/draft-tbray-http-legally-restricted-stat
us/?include_text=1
2013-04-15 22:47:20 -04:00
Kenneth Reitz
ae5419db17 translations 2013-04-15 03:22:56 -04:00
Kenneth Reitz
9c2f9f61ef Translatoins 2013-04-15 03:22:48 -04:00
Kenneth Reitz
636476e704 Merge remote-tracking branch 'origin/master' 2013-04-15 03:21:40 -04:00
Kenneth Reitz
aafca7acdc translations 2013-04-15 03:21:32 -04:00
Kenneth Reitz
a527ecfabd Merge pull request #1242 from oviboy/master
HTTP Digest Auth case insensitive replacement of "Digest "
2013-04-13 20:28:43 -07:00
Kenneth Reitz
967cfbe663 Merge pull request #1299 from schlamar/fix-decompression
Use streaming decompression feature of urllib3.
2013-04-13 20:27:46 -07:00
Kenneth Reitz
9df450193f Merge pull request #1302 from ssbarnea/master
Documents the actual logging methods #1297
2013-04-13 20:25:47 -07:00
Kenneth Reitz
082029ff2e switch streaming requests to httpbin 2013-04-13 15:26:49 -04:00
Kenneth Reitz
c0baec1431 Merge pull request #1305 from michaelhelmick/patch-1
Fix Advanced docs Twitter Streaming example
2013-04-13 12:24:21 -07:00
Kenneth Reitz
561366cab0 Merge pull request #1309 from toastdriven/better-max-retries
Changed HTTPAdapter to allow max retries to be specified when initializing.
2013-04-13 12:05:13 -07:00
Kenneth Reitz
f0950a9e6c Merge pull request #1311 from sigmavirus24/fix1303
Change the method when it isn't already GET/HEAD
2013-04-13 12:02:29 -07:00
Ian Cordasco
c5d0a0931e Change the method when it isn't already GET/HEAD
For some reason it was only change the method when a POST was being made. This
is almost certainly my fault.

Fixes #1303
2013-04-13 12:31:22 -04:00
Daniel Lindsley
36dcce1a06 Changed HTTPAdapter to allow max retries to be specified when initializing. 2013-04-12 16:40:39 -07:00
Mike Helmick
ed0242ae3a Fix Advanced docs Twitter Streaming example
Twitter API v1.1 Streaming now requires OAuth Authentication rather than XAuth.
I believe v1 is being blacked out sometime at the beginning of May.
2013-04-11 20:19:51 -04:00
Kenneth Reitz
eda4b55f7a Merge pull request #1306 from pombredanne/patch-1
Updated NOTICE copyright from 2022 to 2012
2013-04-11 09:04:07 -07:00
pombredanne
6e3cfe5dd5 Updated NOTICE copyright from 2022 to 2012
Which is likely what you meant, eh?
2013-04-11 17:08:27 +03:00
Sorin Sbarnea
fb49481ddf Updated documentation indicating that logging is done via requests.packages.urllib3 instead of requests.
modified:   docs/api.rst
2013-04-10 16:46:05 +01:00
Sorin Sbarnea
17ecb6891c * Documented the logging, requested in #1297
* Added build directory and *.egg to .gitignore
* Added sphinx as setup requirement in order to be able to build documentation with `pyhton setup.py build_sphinx`

modified:   .gitignore
modified:   docs/api.rst
modified:   setup.py
2013-04-10 16:15:28 +01:00
schlamar
4c3432b759 Fix test with StringIO. 2013-04-10 08:24:33 +02:00
schlamar
59f916ca4a Use streaming decompression feature of urllib3. 2013-04-10 08:08:33 +02:00
schlamar
6d6252aa9f Update urllib3 to 71f84f9. 2013-04-10 08:00:36 +02:00
Kenneth Reitz
2426eeb371 Merge pull request #1298 from Lukasa/adapter_doc
Transport Adapter Docs
2013-04-09 13:17:19 -07:00
Cory Benfield
e5f1053b30 Shell of Transport Adapter documentation. 2013-04-09 20:58:59 +01:00
Cory Benfield
c73f653352 Add HTTPAdapter to API docs. 2013-04-09 19:54:47 +01:00
Kenneth Reitz
5b937b384b Merge pull request #1296 from sursh/master
Add helpful error message to r.json() method
2013-04-08 11:01:36 -07:00
Sasha Laundy
8ebeb3dc3f Make json error message more specific 2013-04-08 13:03:13 -04:00
sursh
dfa59c2d97 Fix markdown 2013-04-08 13:01:02 -03:00
sursh
511cc4c034 Make json error on empty response more specific 2013-04-08 13:00:27 -03:00
Kenneth Reitz
fa9234da05 Merge pull request #1295 from sigmavirus24/issue1293
Fix #1293
2013-04-06 17:27:23 -07:00
Ian Cordasco
afcc883d7f Fix #1293 2013-04-06 11:26:52 -04:00
Kenneth Reitz
1e465fd255 Merge pull request #1291 from gazpachoking/dont_set_session_cookies_on_response
Don't set all session cookies on response.cookies
2013-04-04 20:41:52 -07:00
Chase Sterling
49a3664222 Don't set all session cookies on response.cookies 2013-04-04 23:30:52 -04:00
Kenneth Reitz
848f2c297e Merge pull request #1290 from gazpachoking/#1287
fix #1287: Make sure expired cookies get removed from session.cookies
2013-04-04 20:22:38 -07:00
Chase Sterling
5c47ce1136 Make sure unit test works on python 2.6 2013-04-04 22:48:14 -04:00
Chase Sterling
88f13598f3 Add a unit test for server expiring cookies from session 2013-04-04 22:40:27 -04:00
Chase Sterling
d22ac00098 fix #1287: Make sure expired cookies get removed from session.cookies 2013-04-04 22:11:38 -04:00
Kenneth Reitz
dccfc5ba3c Merge pull request #1283 from sigmavirus24/master
Closes #1280
2013-04-03 18:04:40 -07:00
Jason Emerick
e7247ce3f6 model the encode_files data handling after encode_params 2013-04-02 14:22:49 -04:00
Jason Emerick
168c3e6913 add a few more variations to the unicode multipart post test 2013-04-02 14:22:12 -04:00
Jason Emerick
82d36d8259 add additional test for unicode multipart post 2013-04-02 11:41:07 -04:00
Ian Cordasco
b9e5cce2d2 Add PreparedRequest recipe to the docs 2013-04-02 10:07:37 -04:00
Ian Cordasco
1abd13700b Closes #1280
Correct the doc-string for Session#request that I copied without thinking
about.
2013-04-02 09:27:25 -04:00
Kenneth Reitz
54ed5ed469 Revert "Fix for the issue https://github.com/kennethreitz/requests/issues/1280"
This reverts commit ca0aea640d.
2013-04-02 08:13:46 -04:00
Kenneth Reitz
cdb700737b Merge pull request #1281 from KamilSzot/Issue_kennethreitz_requests_issues_1280
Fix for the issue https://github.com/kennethreitz/requests/issues/1280
2013-04-02 04:44:59 -07:00
Kamil Szot
ca0aea640d Fix for the issue https://github.com/kennethreitz/requests/issues/1280 2013-04-02 13:42:40 +02:00
Kenneth Reitz
20a8a9b681 Merge pull request #1278 from t-8ch/patch-1
fix tiny typo in HISTORY.rst
2013-04-01 21:36:49 -07:00
Kenneth Reitz
9c18febf45 Merge pull request #1277 from pborreli/typos
Fixed typos
2013-04-01 21:36:39 -07:00
Kenneth Reitz
848aca21be Merge pull request #1276 from alex/patch-1
is should not be used for comparing numbers
2013-04-01 20:42:22 -07:00
Jason Emerick
f37b968475 use compat.str instead of compat.builtin_str 2013-04-01 18:14:52 -04:00
Jason Emerick
f0660e33a2 add test for unicode multipart post 2013-04-01 18:10:12 -04:00
Thomas Weißschuh
333fa87489 fix tiny typo in HISTORY.rst 2013-04-01 18:47:20 +00:00
Pascal Borreli
037b38badb Fixed typos 2013-04-01 18:02:18 +00:00
Alex Gaynor
39acf1dbd2 is should not be used for comparing numbers 2013-03-31 23:20:46 -07:00
Kenneth Reitz
59b69d1fb8 Merge remote-tracking branch 'origin/master' 2013-03-31 08:28:57 +03:00
Kenneth Reitz
d06908d655 v1.2.0 2013-03-31 08:28:22 +03:00
Kenneth Reitz
5b5ffc9714 Merge pull request #1268 from t-8ch/update_urllib3
update vendored urllib3
2013-03-30 22:27:59 -07:00
Kenneth Reitz
75703d57e6 fix syntax error 2013-03-31 08:22:44 +03:00
Kenneth Reitz
bd6b981d12 Merge pull request #1267 from sigmavirus24/master
One last pull request before 1.2 ideally
2013-03-30 22:21:52 -07:00
Kenneth Reitz
6a2eea8b21 Merge pull request #1270 from makto/add_attr
add 'max_redirects' to Session's __attrs__
2013-03-30 22:20:02 -07:00
Ian Cordasco
325ea7b7e2 Use session defaults instead of arbitrary ones 2013-03-29 20:18:58 -04:00
makto
4ffae38627 add 'max_redirects' to Session's __attrs__ to ensure proper serialization of Session 2013-03-29 20:58:15 +08:00
Thomas Weißschuh
d7908a9fde update vendored urllib3 2013-03-28 12:49:04 +00:00
Ian Cordasco
0cd23d8d6e Fix the tests and unseparate comments from code
See the comments on the previous few commits on GitHub.
2013-03-28 08:33:34 -04:00
Ian Cordasco
aca91e06f2 Restore Session.request docstring
Resolves #1251
2013-03-27 23:46:21 -04:00
Ian Cordasco
88177ec33f Finally resolve #1084
Send body on redirect when POSTing or PUTing.
2013-03-27 23:30:00 -04:00
Ian Cordasco
1d5c4f3f0f This should take care of #1266
We were sending 'None' as the Content-Length on requests where the body was a
generator. This commit should prevent that entirely.
2013-03-27 23:26:11 -04:00
Ian Cordasco
478d49027f Add correct defaults in Session.send()
Resolves #1258

Also fixed the tests to reflect the necessary changes.
2013-03-27 23:17:34 -04:00
Ian Cordasco
46a770e03f Update the HISTORY for v1.2 2013-03-27 23:10:59 -04:00
Kenneth Reitz
ba25184ed5 sp 2013-03-26 16:38:11 -04:00
Kenneth Reitz
7f1ad5c127 Merge remote-tracking branch 'origin/master' 2013-03-26 16:32:25 -04:00
Kenneth Reitz
ab19b79375 Rezzy the Request Sea Turtle 2013-03-26 16:32:20 -04:00
Kenneth Reitz
c63472e328 Merge pull request #1263 from justin-factual/master
iter_lines documentation for #1260
2013-03-25 12:48:45 -07:00
Justin Fenn
6963b8490c Clarify streaming behavior in iter_lines doc 2013-03-25 12:20:33 -07:00
Ovidiu Negrut
9d16c72767 compiled regex expression in digest auth, this also works in python 2.6.x 2013-03-25 12:28:25 +02:00
Ovidiu Negrut
186a589783 Merge branch 'master' of git://github.com/kennethreitz/requests 2013-03-25 12:26:45 +02:00
Kenneth Reitz
12ef43e407 /s/make/invoke 2013-03-22 15:52:08 -04:00
Kenneth Reitz
92f7478174 first 2013-03-22 15:48:27 -04:00
Kenneth Reitz
19d38d502f actually cleanup url authentication 2013-03-22 15:47:20 -04:00
Kenneth Reitz
52d328ec3c cp949prover for charade 2013-03-22 15:21:28 -04:00
Kenneth Reitz
a554828931 invoke! 2013-03-22 15:21:04 -04:00
Kenneth Reitz
80a861cb84 upgrade to charade v1.0.3 2013-03-22 15:20:49 -04:00
Kenneth Reitz
0d07d1afb3 remove makefile 2013-03-22 15:20:27 -04:00
Kenneth Reitz
030b9763b5 fix get_auth_from_url 2013-03-22 00:15:06 -04:00
Kenneth Reitz
03a3ca5004 get_auth_from_url return None if nothing is found 2013-03-22 00:13:08 -04:00
Kenneth Reitz
1325409560 simplify get_auth_from_url call 2013-03-22 00:12:58 -04:00
Kenneth Reitz
13aeb9cb06 Merge pull request #1254 from jkakar/url-authentication
url-authentication
2013-03-21 21:06:23 -07:00
Jamu Kakar
7d217bf9bd - Pull credentials out of the URL when possible. 2013-03-21 17:55:08 -07:00
Kenneth Reitz
cb2116dcce Merge pull request #1239 from miikka/fix-issue-1228
Use session cookies when following redirects
2013-03-20 03:37:25 -07:00
Kenneth Reitz
db90d85a8d Merge pull request #1245 from Damgaard/master
Remove duplicate comment.
2013-03-14 08:29:53 -07:00
Andreas Damgaard Pedersen
af791d48c6 Remove duplicate comment. 2013-03-14 16:12:45 +01:00
Miikka Koskinen
120a2f385a Do not pass cookies to resolve_redirects
SessionRedirectMixin is extending Session, so we can just use
self.cookies.
2013-03-12 18:45:11 +02:00
Miikka Koskinen
5bb2be9a23 Use session cookies when following redirects
When a redirect was followed, only the cookies set by the initial
response were used in the follow-up request. Fixes #1228.
2013-03-12 18:43:58 +02:00
Miikka Koskinen
e958511df0 Add failing test case for #1228 2013-03-12 18:43:58 +02:00
Kenneth Reitz
1642996798 Merge pull request #1244 from jajadinimueter/master
Fixed further pickeling issues in Session and HTTPAdapter
2013-03-12 08:23:21 -07:00
Florian Mueller
c41932e184 Fixed some pickeling issues with HTTPAdapter and Session
Added trust_env and stream to Session.__attrs__. Initialize
self._pool_connections and self._pool_maxsize in HTTPAdapter.
2013-03-12 16:04:19 +01:00
Kenneth Reitz
25ea8cdb38 Merge pull request #1243 from darjus-amzn/master
Pickling of Session and HTTPAdapter + a test
2013-03-12 07:37:55 -07:00
Darjus Loktevic
e706d18cf8 Pickling of Session and HTTPAdapter + a test
This is for issue #1088
2013-03-11 18:12:34 +00:00
Ovidiu Negrut
e752455b6f Digest auth: case insensitive replacement of 'digest ' string with '' from WWW-Authenticate 2013-03-11 10:28:37 +02:00
Kenneth Reitz
3e72f234d0 Merge pull request #1238 from oczkers/master
cleanup (remove max_retries test)
2013-03-08 11:51:42 -08:00
Piotr Staroszczyk
25dc07e8ed cleanup (remove max_retries test) 2013-03-08 11:17:17 +01:00
Kenneth Reitz
13de69299f Merge pull request #1234 from Vassius/issue-1225
Fix issue #1225 (Documentation: response object status/reason)
2013-03-05 07:31:10 -08:00
Markus Wiik
90e109c241 Fix issue #1225 (Documentation: response object status/reason) 2013-03-05 16:18:24 +01:00
Kenneth Reitz
d25ba77b2e Merge remote-tracking branch 'origin/master' 2013-03-04 17:54:32 -05:00
Kenneth Reitz
c0d4b23cea Merge branch 'httperror_init' of git://github.com/dmedvinsky/requests
Conflicts:
	AUTHORS.rst
	test_requests.py
2013-03-04 17:54:25 -05:00
Kenneth Reitz
99eead01a3 Merge pull request #1223 from andrewjesaitis/master
Fixes __getstate__ for session pickling
2013-03-04 14:52:31 -08:00
Andrew Jesaitis
d60845303b Adds __attrs__ back to Session object 2013-03-04 11:07:29 -07:00
Kenneth Reitz
b14584f36e Revert "[kennethreitz/requests#1208] adding unit test for max_retries"
This reverts commit 18b29ea005.
2013-03-03 12:01:47 -05:00
Kenneth Reitz
23d8522285 Revert "[kennethreitz/requests#1208] adding a max_retries argument"
This reverts commit 796d3225dd.
2013-03-03 12:01:38 -05:00
Dmitry Medvinsky
c4f9340fb4 Add ability to pass response to HTTPError()
Just a little refactoring, but it seems nicer to me to be able to pass
the response when constructing the `HTTPError` instance instead of
constructing it and then changing the member variable.
2013-03-03 10:05:42 +04:00
Kenneth Reitz
d372a5b10b Merge pull request #1190 from mkomitee/master
Pass user options to hooks
2013-03-02 13:04:00 -08:00
Kenneth Reitz
7aad3fd08c Merge pull request #1219 from Wilfred/master
Adding max_retries as an argument
2013-03-02 13:02:26 -08:00
Kenneth Reitz
76c4b68e4a Merge pull request #1226 from dmedvinsky/fix-typos
Fix couple of typos in HISTORY.rst
2013-03-02 13:00:48 -08:00
Kenneth Reitz
07441c8951 Merge pull request #1229 from sprt/master
Doc fix: URLError doesn't exist anymore
2013-03-02 12:59:41 -08:00
Kenneth Reitz
98ff29f793 Merge pull request #1230 from davidfischer/trivial-docs-fix
Github URL fix
2013-03-02 12:59:10 -08:00
Kenneth Reitz
08cab48431 Merge pull request #1231 from davidfischer/docs-migration1.0
Initial docs patch for migrating from pre 1.0
2013-03-02 12:58:49 -08:00
David Fischer
7e7c275504 Session isn't advertised as a context manager 2013-03-02 08:59:19 -08:00
David Fischer
73abf84ace Capitalize s in Session 2013-03-02 08:51:55 -08:00
David Fischer
7eba5ffe48 Logic on streaming responses was changed in 1.0
* prefetch=False in 0.x is now stream=True
2013-03-02 08:44:14 -08:00
David Fischer
1d2ee524d8 Initial docs patch for migrating from pre 1.0 2013-03-01 23:21:08 -08:00
David Fischer
38f2581d6f Github URL fix 2013-03-01 20:36:42 -08:00
sprt
b80f8aa475 URLError doesn't exist anymore 2013-03-01 20:20:53 +01:00
Dmitry Medvinsky
057f32924a Fix couple of typos in HISTORY.rst
One typo and a couple of auto-completions, I guess.
2013-03-01 11:42:43 +04:00
Andrew Jesaitis
6a0845c984 Checks __attrs__ on session instance prior to iterating. 2013-02-28 15:13:57 -07:00
Wilfred Hughes
18b29ea005 [kennethreitz/requests#1208] adding unit test for max_retries 2013-02-27 16:01:36 +00:00
Wilfred Hughes
796d3225dd [kennethreitz/requests#1208] adding a max_retries argument 2013-02-27 16:01:23 +00:00
Ian Cordasco
be62645dd5 Revert "If Content-Length is already set.."[1]
This reverts commit 544d08d0f6.

[1]"If Content-Length is already set, don't over ride it"
2013-02-25 09:29:05 -05:00
Kenneth Reitz
603fd42fe6 Merge pull request #1205 from sigmavirus24/fix1203
Missing line was allowing redirects with HEAD
2013-02-25 02:31:21 -08:00
Kenneth Reitz
1fc567449c Merge pull request #1210 from Lukasa/urlencode_proxy
Unquote proxy usernames and passwords.
2013-02-25 02:31:00 -08:00
Kenneth Reitz
c01bc5be5b Revert "Lukasa is lazy"
This reverts commit 178ff62b93.
2013-02-22 08:23:34 -05:00
Cory Benfield
577dba2bf7 Unquote proxy usernames and passwords. 2013-02-22 11:33:01 +11:00
Ian Cordasco
a788cb7271 Missing line was allowing redirects with HEAD
Closes #1203
2013-02-20 08:57:37 -05:00
Kenneth Reitz
178ff62b93 Lukasa is lazy 2013-02-20 02:11:41 -05:00
Kenneth Reitz
aa99525537 Merge pull request #1193 from Lukasa/timeout_exception
Throw more informative exceptions.
2013-02-19 23:10:44 -08:00
Kenneth Reitz
7e0b9cf409 Merge pull request #1200 from gazpachoking/session_cookies_fix
Make sure session cookies do not overwrite explicit request cookies
2013-02-19 22:50:10 -08:00
Kenneth Reitz
017c027629 Merge pull request #1199 from t-8ch/docs_explicit_proxy_scheme
Use explicit scheme for proxies in the docs
2013-02-19 05:40:15 -08:00
Cory Benfield
c2480f65e6 Rethrow underlying exceptions. 2013-02-18 16:30:16 +11:00
Chase Sterling
3f86e22a07 Make sure session cookies do not overwrite explicit request cookies
Implement RequestsCookieJar.copy
Use RequestsCookieJar.update when merging cookiejars
2013-02-16 00:56:59 -05:00
Thomas Weißschuh
b53975327f use explicit scheme for proxies in the docs
Issue #1192 tried to force user to provide a scheme for proxy urls.
As this would break backwards compability change the docs instead.
2013-02-15 16:32:50 +00:00
Kenneth Reitz
d0390d4f27 Merge pull request #1194 from gazpachoking/cookiejar_update
Allow RequestsCookieJar to be updated with cookies from a CookieJar
2013-02-14 22:42:49 -08:00
Chase Sterling
f3393fb24c Remove ability to from RequestCookieJar __getitem__, __setitem__ to use cookies as keys 2013-02-14 22:52:31 -05:00
Michael Komitee
4c21106222 Fixing test for python3 2013-02-14 21:33:01 -05:00
Chase Sterling
87d9d9643c Allow RequestsCookieJar to be updated with cookies from a CookieJar 2013-02-14 01:05:42 -05:00
Michael Komitee
df5dcb8a7d New tests fail on python 3.x because read() returns bytes and the test checks for strings 2013-02-13 22:42:56 -05:00
Michael Komitee
69ba64380b Adding test to ensure options like stream function with authentication
This test demonstrates the reason why we need to pass kwargs to hooks. Without
it, features like stream cannot work with authentication.
2013-02-13 21:28:32 -05:00
Michael Komitee
d0285fac42 Use user supplied options when resending authenticated requests
Hooks sometimes have to send requests (e.g. when responding to a 401 during
authentication).

All keyword arguments should be passed along when hooks are dispatched so that
if a user wanted to use a timeout, stream, specify a cert location with the
verify flag, etc, their specification can be followed.
2013-02-13 19:11:38 -05:00
Kenneth Reitz
f73bda06e9 Merge pull request #1185 from sigmavirus24/fix_hook_dispatching
Dispatch hooks before following redirects
2013-02-13 02:13:31 -08:00
Ian Cordasco
4dfd6f3fc1 Dispatch hooks before following redirects
Fixes #1183
2013-02-12 23:00:06 -05:00
Kenneth Reitz
cdec20af65 Merge pull request #1181 from denis-ryzhkov/master
Fix of UnicodeDecodeError on unicode header name that can be converted to ascii.
2013-02-12 08:43:02 -08:00
Kenneth Reitz
09a7251245 Merge pull request #1184 from piotr-dobrogost/numeric_codes
small cleanup of redirect codes
2013-02-12 00:27:05 -08:00
Denis Ryzhkov
56f4b7ca68 Deleted is_py2 check from unicode_header_name fix thanks to Lukasa. 2013-02-12 09:51:46 +03:00
Piotr Dobrogost
4c8d1b9a7d removed no longer used redirect codes from models
added numeric values of redirect codes in comments
2013-02-11 23:07:12 +01:00
Denis Ryzhkov
6da7e22a4a Fix of UnicodeDecodeError on unicode header name that can be converted to ascii. 2013-02-11 15:37:58 +03:00
Kenneth Reitz
5d9fcc711b Merge pull request #1180 from sigmavirus24/master
Fix Session level Cookie Handling
2013-02-10 16:43:21 -08:00
Ian Cordasco
2e31696156 Test and perfection for cookie handling.
I also fixed up some of the RequestsCookieJar methods so using
jar.update(other_jar) works without a problem. This cleans up some of the code
in sessions and the resolve_redirects method.
2013-02-10 19:36:36 -05:00
Ian Cordasco
0fb13e0b6c And tests 2013-02-10 17:49:49 -05:00
Kenneth Reitz
1da1490bcd urllib3 update
#1053
2013-02-10 17:43:58 -05:00
Ian Cordasco
9cdc8325ae Fix Setting a cookie on redirect 2013-02-10 17:43:37 -05:00
Kenneth Reitz
12d66cfc41 Merge pull request #1173 from sigmavirus24/use_send_in_resolve_redirects
Use send in resolve redirects
2013-02-10 14:21:13 -08:00
Ian Cordasco
9c8660dbb6 Resolve @piotr-dobrogost's concerns
Piotr had good objections to my not re-sending the body of the request on 307.
2013-02-10 17:14:45 -05:00
Ian Cordasco
e7bc9bf1b2 Preserve the original request.
Let's make a copy to preserve it.
2013-02-10 17:11:16 -05:00
Ian Cordasco
e2ad0d0fe8 We shouldn't be sending the data on redirect.
As such, we should remove the body from the old request as well as the
Content-Length header.
2013-02-10 17:11:16 -05:00
Ian Cordasco
3299771e34 Fix the potential issue mentioned in #1151
See: https://github.com/kennethreitz/requests/pull/1151#issuecomment-12905796

This is solved by just reusing the PreparedRequest from the last request.
2013-02-10 17:11:16 -05:00
Kenneth Reitz
f2be5fae27 Merge pull request #1161 from sigmavirus24/fix1159
If Content-Length is already set, don't override
2013-02-10 14:07:08 -08:00
Kenneth Reitz
90cd37fda0 Merge pull request #1119 from Lukasa/diags
Better diagnostics when trying to send unprepared request
2013-02-10 14:06:38 -08:00
Cory Benfield
b0657cf163 Guard against users sending unprepared requests. 2013-02-10 06:13:02 +00:00
Kenneth Reitz
03f6908fe9 Merge pull request #1162 from Lukasa/proxy_auth
Proxy Authorization Headers
2013-02-06 13:05:04 -08:00
Kenneth Reitz
c68f928254 Merge pull request #1168 from Lukasa/freeze
Feature freeze.
2013-02-06 11:53:55 -08:00
Cory Benfield
ff954e16f1 Feature freeze. 2013-02-06 19:01:24 +00:00
Cory Benfield
d437c338d0 Add Proxy-Auth header when proxies have credentials. 2013-02-06 18:40:35 +00:00
Kenneth Reitz
9fc7059140 Merge pull request #1151 from sigmavirus24/fix1133
Move redirect handling from Session.request to Session.send
2013-02-05 11:42:51 -08:00
Ian Cordasco
544d08d0f6 If Content-Length is already set, don't override
Re: #1159
2013-02-01 11:58:23 -05:00
Kenneth Reitz
ae55676a50 Merge pull request #1158 from yehudasa/master
Fix check that breaks handling of 303 response in some cases (v2)
2013-02-01 07:30:47 -08:00
Yehuda Sadeh
b07c1ebd85 Fix POST 303 redirect resonse handling
This fixes issue #1156.

Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
2013-01-31 11:23:09 -08:00
Kenneth Reitz
113b67c069 moar 2013-01-31 14:35:42 +00:00
Kenneth Reitz
12246afab8 HMG 2013-01-31 13:20:14 +00:00
Kenneth Reitz
85da715f40 Merge pull request #1153 from cbare/master
use only base filename in multipart file upload
2013-01-30 15:56:21 -08:00
Kenneth Reitz
b554b0c1cb Merge pull request #1152 from ekatsah/patch-1
Update docs/user/advanced.rst
2013-01-30 15:54:20 -08:00
Christopher Bare
9ff8cbe5fb shorten filename by os.path.basename in guess_filename(obj) 2013-01-30 15:33:43 -08:00
ekatsah
79727abb32 Update docs/user/advanced.rst
Rephrasing a line in the docs, add explicite mention of "client side certificate" (for indexing and clarity)
2013-01-30 21:22:24 +01:00
Ian Cordasco
ba485913b6 Thanks to @maxcountryman for the code review 2013-01-30 10:58:32 -05:00
Ian Cordasco
a95bfd3032 Fix the elapsed test and #1133 2013-01-29 23:50:37 -05:00
Kenneth Reitz
207a2744aa Merge pull request #1143 from Lukasa/master
Response as iterator
2013-01-28 20:52:17 -08:00
Cory Benfield
95084c9ca8 Fix test for Py3. 2013-01-28 18:35:41 +00:00
Cory Benfield
2f7923bc05 More useful docstring. 2013-01-28 18:35:41 +00:00
Cory Benfield
2ac3913733 Make Response objects iterable. 2013-01-28 18:35:41 +00:00
Kenneth Reitz
231e343a3b Merge pull request #1148 from sigmavirus24/timing_switch
Move the timing work to Session.send
2013-01-28 09:40:10 -08:00
Ian Cordasco
c8ad4f0b73 Move the timing work to Session.send
per @kennethreitz's request
2013-01-28 11:53:02 -05:00
Kenneth Reitz
25545b393b Merge pull request #1146 from oczkers/master
It`s more pep8 now
2013-01-27 19:11:15 -08:00
Kenneth Reitz
dfa89e299a Merge pull request #1138 from clee/master
Time each request
2013-01-27 19:10:16 -08:00
Chris Lee
318300848f Use timedelta and change attribute name back to elapsed 2013-01-27 19:07:48 -08:00
Chris Lee
d4aaef1e9a Time how long each request takes
Stored as attribute Response.time_taken
2013-01-27 19:06:50 -08:00
oczkers
40a060cf57 it`s more pep8 now 2013-01-27 02:04:12 +01:00
Kenneth Reitz
ac5e6807fa Merge pull request #1145 from sigmavirus24/master
Mock the links test
2013-01-26 09:05:19 -08:00
Ian Cordasco
250896b7f3 Mock the links test. 2013-01-26 11:55:08 -05:00
Kenneth Reitz
9d1d927420 Merge pull request #1103 from dmckeone/master
Restore the ability to use a list of 2-tuples for the data keyword argument with requests.post.
2013-01-25 21:02:01 -08:00
Kenneth Reitz
440aeca5d4 Merge pull request #1123 from andrewjesaitis/master
Fixes DigestAuth with Sessions
2013-01-25 21:00:33 -08:00
Kenneth Reitz
b6af86225d Merge pull request #1140 from ralphbean/cookie-test
Test for cookie parameters.
2013-01-25 20:58:35 -08:00
Kenneth Reitz
c50f8f7cc8 Merge pull request #1142 from theaeolianmachine/removeGETContentLength
Remove default Content-Length from GET requests.
2013-01-25 20:58:20 -08:00
Johnny Goodnow
8d8865aadb Remove Content-Length from GET/HEAD by default.
Now, Content-Length is only auto-added for non-GET/HEAD requests.
2013-01-25 20:50:58 -08:00
Johnny Goodnow
f7c10ca74d Always add Content-Length to HTTP PATCH. 2013-01-25 12:07:24 -08:00
Johnny Goodnow
4cac9f07c4 Add myself to AUTHORS. 2013-01-25 00:19:09 -08:00
Johnny Goodnow
f453892960 Fix python 2.6 unittest compatability issue. 2013-01-24 21:25:14 -08:00
Johnny Goodnow
44b1e7ebea Remove default Content-Length from GET requests.
Fix #1051.
2013-01-24 21:13:32 -08:00
Ralph Bean
4ce6440795 purported py2.6 support for cookies test. 2013-01-25 00:10:51 -05:00
Kenneth Reitz
22623bd8c2 Merge pull request #1099 from sprt/master
Make hooks work with prepared requests
2013-01-24 10:00:24 -08:00
Ralph Bean
5861254d8f Test for cookie parameters. 2013-01-24 12:47:55 -05:00
sprt
ab2e7df8a9 Merge branch 'master' of git://github.com/kennethreitz/requests
Conflicts:
	test_requests.py
2013-01-24 18:36:53 +01:00
Kenneth Reitz
beec48c8f8 Merge pull request #1137 from whit537/unperson_test
Remove a test per @kennethreitz in IRC
2013-01-23 17:37:13 -08:00
Kenneth Reitz
b2427223c1 Merge pull request #1136 from steveklabnik/test_link_header
introduce a test for link headers.
2013-01-23 17:34:55 -08:00
Chad Whitacre
ef52044c5f Remove a test per @kennethreitz in IRC
The test suite is moving from the httpbin pattern (which hits the
network) to depending on the request.prepare method (which doesn't).
Here's a start ...
2013-01-23 20:32:03 -05:00
Steve Klabnik
ccea28db07 introduce a test for link headers. 2013-01-23 20:31:05 -05:00
Kenneth Reitz
e21ef547f7 Merge pull request #1134 from steveklabnik/remove_silly_assert
remove silly assert
2013-01-23 17:06:40 -08:00
Steve Klabnik
0af8ff9405 remove silly assert 2013-01-23 20:03:43 -05:00
Kenneth Reitz
6e0ca583fe Merge pull request #1132 from sburns/jsonkwargs
Pass kwargs to json.loads
2013-01-23 14:02:37 -08:00
Scott Burns
2bcc4a774a Pass kwargs to json.loads
Not all JSON is created equally. This commit
addresses when users want to take control of the
json decode process.
2013-01-23 15:25:48 -06:00
Andrew Jesaitis
34268b16c3 Moves num_401_calls counter to HTTPDigestAuth 2013-01-23 10:32:56 -07:00
Ian Cordasco
1cfe59299b Only call the hook once. 2013-01-23 11:51:37 -05:00
Ian Cordasco
e771aa9386 Comment & replace something that keeps disappearing 2013-01-23 11:28:30 -05:00
sprt
cc7bee19f0 Refactor prepare_hooks() 2013-01-23 15:45:44 +01:00
sprt
088f908d58 Add missing import 2013-01-23 15:45:14 +01:00
sprt
c4ad8afba9 Merge remote-tracking branch 'upstream/master'
Conflicts:
	requests/sessions.py
2013-01-23 14:42:45 +01:00
Kenneth Reitz
0c09d16b29 Merge pull request #1125 from juanriaza/master
HTTPDigestAuth: missing algorithm field
2013-01-22 19:43:50 -08:00
Kenneth Reitz
30573b2d8b Merge pull request #1116 from Lukasa/master
Correct type of Content-Length headers.
2013-01-22 18:51:17 -08:00
Kenneth Reitz
1a87f15e6f Merge pull request #1117 from sigmavirus24/issue1106
Fix #1106
2013-01-22 18:48:31 -08:00
Kenneth Reitz
472a5b4748 Merge pull request #1127 from sigmavirus24/fix1126
A simple fix
2013-01-22 17:53:16 -08:00
Kenneth Reitz
b94510fc42 Merge pull request #1128 from mvid/master
Updating urllib3 to current master
2013-01-22 17:45:00 -08:00
Mantas Vidutis
6e53290acd update urllib3 to current master with ssl bugfixes 2013-01-22 14:40:10 -08:00
Ian Cordasco
ca85b4ec6c Tests pass this time. 2013-01-22 17:22:12 -05:00
Juan Riaza
a6360ca134 missing algorithm field 2013-01-22 16:35:16 +01:00
Kenneth Reitz
47aa968f3c Merge pull request #1121 from Lukasa/typo
Small typo fixes.
2013-01-22 06:40:24 -08:00
Kenneth Reitz
3bf46609c8 Merge pull request #1097 from matthewlmcclure/issues/1096
Resolves the parts of #1096 in requests proper.
2013-01-22 05:11:14 -08:00
Kenneth Reitz
cd0cbada65 Merge pull request #1095 from Jud/patch-1
Fix spacing in setup.py
2013-01-22 05:10:19 -08:00
Kenneth Reitz
ed559bb8b1 Merge pull request #1122 from Lukasa/readline
Decrease default line length for iter_lines
2013-01-22 05:09:11 -08:00
Ian Cordasco
27e814ad76 Fix failing tests. 2013-01-21 20:21:08 -05:00
Andrew Jesaitis
03893d9b7f Fixes repeated 401s when using DigestAuth with a session 2013-01-21 16:34:11 -07:00
Cory Benfield
297aa04bea Decrease default line length for iter_lines 2013-01-21 21:15:04 +00:00
Cory Benfield
c678a7b402 Small typo fixes. 2013-01-21 19:55:08 +00:00
Kenneth Reitz
b7242a1ad1 Merge pull request #1120 from spulec/patch-2
Add py3.3 testing back
2013-01-21 11:27:41 -08:00
Steve Pulec
cf0a5089f5 Add py3.3 testing back 2013-01-21 14:19:29 -05:00
Cory Benfield
de32f1774f Remove test that cannot work on Python3. 2013-01-19 17:14:32 +00:00
Ian Cordasco
e1c4fe21d4 Fix #1106 2013-01-19 11:49:52 -05:00
Cory Benfield
89c8cbbe0c Ensure Content-Length is a string. 2013-01-19 12:07:34 +00:00
Kenneth Reitz
16ba63ccde Merge pull request #1114 from vlaci/master
Keep-alive support for http proxies
2013-01-18 05:40:26 -08:00
László Vaskó
347a52aa5c Fixed proxy requests to pool connections. 2013-01-18 13:41:14 +01:00
Kenneth Reitz
c920ee57b8 Merge pull request #1110 from slingamn/certifi_eol
Remove support for certifi
2013-01-18 04:39:41 -08:00
Shivaram Lingamneni
985a906c6c Remove support for certifi
As per #1105, certifi is being end-of-lifed. Requests will use either
its own vendored bundle, or possibly (when packaged with OS distributions)
an externally packaged bundle, which can be enabled by patching
requests.certs.where().
2013-01-17 20:44:01 -08:00
Kenneth Reitz
f038f68ad8 Merge pull request #1107 from Lukasa/master
Correct broken cookies example
2013-01-17 19:50:55 -08:00
Cory Benfield
d3e6597f73 Update docs with correct cookie behaviour. 2013-01-17 19:28:28 +00:00
Kenneth Reitz
f8d729a17b Merge pull request #1105 from slingamn/turktrust
refresh CA certificates
2013-01-17 02:51:52 -08:00
Shivaram Lingamneni
b66427eab9 remove the TURKTRUST root certificates, as per #1102 2013-01-17 02:02:49 -08:00
David McKeone
35d4a47cb1 Allow passing POST data with a list of 2-tuples. 2013-01-16 11:53:24 -07:00
sprt
a721d590b4 Make hooks work with prepared requests 2013-01-12 21:46:44 +01:00
Matt McClure
628e393b9a Resolves the parts of #1096 in requests proper. 2013-01-11 15:04:47 -05:00
Jud
a950322b07 Fix spacing in setup.py 2013-01-11 11:42:02 -05:00
Kenneth Reitz
34917618d8 +1 2013-01-11 09:34:14 -05:00
Kenneth Reitz
1a7c91f658 Merge remote-tracking branch 'origin/master' 2013-01-10 02:13:06 -05:00
Kenneth Reitz
41687c8fa8 v1.1.0 and docs 2013-01-10 02:13:02 -05:00
Kenneth Reitz
91f6e69cc4 Merge pull request #1092 from jianlius/case-insensitive-update
Make merge_kwargs case-insensitive when looking up keys.
2013-01-09 23:07:45 -08:00
Kenneth Reitz
6ec1a9ca52 Merge pull request #1091 from vinodc/specify_file_content_type
Allow for explicit file content type support
2013-01-09 23:02:47 -08:00
Kenneth Reitz
9d33629b6d don't supply params for directs
Closes #1070
2013-01-10 01:58:46 -05:00
Kenneth Reitz
ef8563ab36 CHUNKED REQUESTS! 2013-01-10 01:58:29 -05:00
Jian Li
a392a87389 Retrieve kwargs.keys() just once. 2013-01-09 22:07:38 -08:00
Kenneth Reitz
4a5b5bc86e test cleanup 2013-01-10 00:40:17 -05:00
Jian Li
68edcd12b1 Make merge_kwargs case-insensitive when looking up keys. 2013-01-09 21:29:24 -08:00
Vinod Chandru
fbc366c1a5 Fixing test to ensure it passes with python 3. 2013-01-09 20:10:54 -08:00
Vinod Chandru
20b10aed1b Allow for third argument in file dict value to support explicit
file content type.
2013-01-09 19:29:28 -08:00
Kenneth Reitz
b4505284f2 Merge pull request #1080 from sigmavirus24/master
Fix #1079
2013-01-07 10:42:30 -08:00
Kenneth Reitz
47fe258b5a Merge pull request #1085 from graingert/patch-1
Update the advanced doc to use the r.json method
2013-01-07 10:42:14 -08:00
Kenneth Reitz
21616a815b Merge pull request #1086 from graingert/patch-2
Update the link headers doc to match the code
2013-01-07 10:41:51 -08:00
Kenneth Reitz
50934e056e Merge pull request #1089 from PaulMcMillan/philosophy
Clarify support for alternative distributions
2013-01-07 10:41:32 -08:00
Paul McMillan
ba1df2cfca Clarify support for alternative distributions 2013-01-07 13:34:51 -05:00
Thomas Grainger
7dc13e5cab Update the link headers doc to match the code 2013-01-04 21:46:14 +00:00
Thomas Grainger
7687746c7c Update the advanced doc to use the r.json method 2013-01-04 21:33:51 +00:00
Ian Cordasco
5264c71d86 Fix #1079
Restore the functionality from here:
https://github.com/kennethreitz/requests/blob/v0.14.2/requests/models.py#L884
2013-01-02 14:25:33 -05:00
Kenneth Reitz
5d87e1aeba Update LICENSE 2012-12-31 17:59:18 -05:00
Kenneth Reitz
c03e893416 Merge pull request #1076 from sigmavirus24/master
Remove (safe|danger)_mode references from docs
2012-12-30 23:49:44 -08:00
Ian Cordasco
fef09a6691 Remove safe_mode/danger_mode from docs 2012-12-30 19:46:04 -05:00
Kenneth Reitz
1d35d439cc Merge pull request #1071 from Lukasa/master
Proxies should have schemes.
2012-12-28 07:41:55 -08:00
Cory Benfield
054e1ab3ee Remove silly print statement. 2012-12-28 15:38:04 +00:00
Cory Benfield
641f4611b2 Make sure proxies have their scheme attached. 2012-12-27 13:37:36 +00:00
Kenneth Reitz
594716abea Merge pull request #1060 from Lukasa/proxy
Provide full URL to proxies.
2012-12-27 03:24:55 -08:00
Cory Benfield
d675abbd3f Remove unneeded import. 2012-12-27 11:22:36 +00:00
Cory Benfield
702f4039bc Use full URL when contacting proxies.
Requests should use the full URL, not the path URL, when forwarding
traffic through proxies.
2012-12-27 11:22:36 +00:00
Cory Benfield
b479a62ec2 PreparedRequests don't carry proxy information.
The proxy info appears to be carried on the session. The only remnants
of proxy handling are this line in __init__().
2012-12-27 11:22:36 +00:00
Kenneth Reitz
81c18c70f5 Merge pull request #1065 from slingamn/certs.release
clean up code support for OS certificate bundles
2012-12-23 02:49:02 -08:00
Shivaram Lingamneni
bbea679ab2 clean up code support for OS certificate bundles 2012-12-23 02:40:18 -08:00
Kenneth Reitz
f1d51aa7eb timeout 2012-12-23 02:45:41 -05:00
Kenneth Reitz
5b5ff69201 update docs 2012-12-23 02:42:14 -05:00
Kenneth Reitz
0665129008 reorder out there 2012-12-23 02:34:53 -05:00
Kenneth Reitz
afd6482219 python for ios 2012-12-23 02:33:03 -05:00
Kenneth Reitz
a23f22e5dc support 2012-12-23 02:29:24 -05:00
Kenneth Reitz
e00e50d01a philsophy 2012-12-23 02:27:28 -05:00
Kenneth Reitz
1960c3c29d Benevolent 2012-12-23 02:25:59 -05:00
Kenneth Reitz
08f66a99b1 semantic versioning 2012-12-23 02:13:31 -05:00
Kenneth Reitz
ab64052642 developer values 2012-12-23 02:07:01 -05:00
Kenneth Reitz
25a9c9fb22 - Python 2.6—3.3 2012-12-23 01:56:46 -05:00
Kenneth Reitz
f0beed8686 + 2012-12-23 01:53:22 -05:00
Kenneth Reitz
5a7d461230 philosophy 2012-12-23 01:51:23 -05:00
Kenneth Reitz
d45eb53bc7 contributor 2012-12-23 01:50:52 -05:00
Kenneth Reitz
911e8aec8d developer interface 2012-12-23 01:50:23 -05:00
Kenneth Reitz
9f89ebe0de request sessions 2012-12-23 01:49:44 -05:00
Kenneth Reitz
526a0befc7 cleaup api docs 2012-12-23 01:47:35 -05:00
Kenneth Reitz
f0fe551dc9 thanks, @sigmavirus24 2012-12-23 01:45:49 -05:00
Kenneth Reitz
f8e2d0e732 api docs 2012-12-23 01:44:54 -05:00
Kenneth Reitz
fcccc82282 Merge remote-tracking branch 'origin/master' 2012-12-23 01:40:12 -05:00
Kenneth Reitz
3ddcc99131 docs update 2012-12-23 01:40:07 -05:00
Kenneth Reitz
70faca2a14 docs, cleanup for preparedrequest 2012-12-23 01:25:01 -05:00
Kenneth Reitz
5f9fecd3aa prepared request docs 2012-12-23 01:21:02 -05:00
Kenneth Reitz
6e780fad6d remove unused timeout 2012-12-23 01:16:00 -05:00
Kenneth Reitz
28b706da83 v1.0.4 2012-12-23 01:15:14 -05:00
Kenneth Reitz
aa3347b2a3 request 2012-12-23 01:14:27 -05:00
Kenneth Reitz
f8a59c3e6f request docstrings 2012-12-23 01:14:22 -05:00
Kenneth Reitz
484916066a Merge pull request #1064 from rascalking/master
fix POST redirects
2012-12-22 22:06:12 -08:00
Kenneth Reitz
b3fbc810d2 no async anymore 2012-12-23 01:02:32 -05:00
Kenneth Reitz
1539d17366 update test docs 2012-12-23 01:02:26 -05:00
Kenneth Reitz
a158abfa15 remove stuidly annoying email notifications 2012-12-23 00:52:18 -05:00
David Bonner
7d085b188c fix POST redirects
the redirect handling logic compares then method to upper-case strings,
so make sure the method gets upper-cased as well.

add a test to POST to /status/302 on httpbin, which fails against httpbin.org
right now.  i'm submitting a pull request over there to fix that right after
this one.  once that's accepted, the new test verifies that the fix works.
2012-12-23 00:51:26 -05:00
Kenneth Reitz
aaa3bf16a9 Merge pull request #1061 from philfreo/patch-1
Fixes url to AUTHORS.rst
2012-12-22 14:10:30 -08:00
Kenneth Reitz
22a1f0917b Merge pull request #1062 from philfreo/patch-2
Using 'master' branch for development
2012-12-22 14:10:17 -08:00
Phil Freo
78f48aef8f Using 'master' branch for development
I'm assuming you're no longer using a 'develop' branch, as I don't see one on GitHub.
2012-12-22 16:46:05 -05:00
Phil Freo
7b1b9df423 Fixes url to AUTHORS.rst
and also mention using 'master' as development branch now.
2012-12-22 16:44:43 -05:00
Kenneth Reitz
e23343ab17 Merge pull request #1059 from Lukasa/sess_docs
Update session documentation.
2012-12-22 03:08:47 -08:00
Kenneth Reitz
b8affd9520 Merge pull request #1049 from Lukasa/master
Correctly identify certificate filenames.
2012-12-22 03:05:50 -08:00
Cory Benfield
1790b1df17 Update session documentation. 2012-12-22 11:03:29 +00:00
Kenneth Reitz
60b4843678 Merge pull request #1055 from michaelwheeler/patch-1
Fixed typo in comment.
2012-12-20 12:55:51 -08:00
michaelwheeler
0b42772663 Fixed typo in comment. 2012-12-20 15:38:04 -05:00
Kenneth Reitz
a49db50b4a Merge pull request #1052 from hozn/apidocs-tweak
Small tweak to API docs to indicate that file-like object supported for data param.
2012-12-20 09:09:24 -08:00
Hans Lellelid
bfef8d99c8 Updated API docs for 'data' param in sessions module to indicate that file-like object is also supported. 2012-12-20 09:47:29 -05:00
Hans Lellelid
dd271782e8 Tweaked the 'data' param docs to indicate that a file-like object is also supported. 2012-12-20 09:45:06 -05:00
Cory Benfield
09da1921ff Import basestring. 2012-12-19 21:37:47 +00:00
Kenneth Reitz
0769ee3b6d Merge pull request #1048 from ib-lundgren/master
Swap prepare_auth and body
2012-12-19 13:12:03 -08:00
Ib Lundgren
05de270d7a Why auth must be prepared last 2012-12-19 21:30:11 +01:00
Cory Benfield
f1ba27faa2 Fix stupid, stupid logic error. 2012-12-19 20:26:41 +00:00
Cory Benfield
8cb904b49d Correctly identify cert files.
Resolves issue #1046.
2012-12-19 20:11:17 +00:00
Ib Lundgren
7e594eb121 Swap prepare_auth and prepare_body 2012-12-19 21:04:50 +01:00
Kenneth Reitz
4966d9c714 Merge pull request #1037 from sigmavirus24/master
Fix #1036
2012-12-18 10:09:33 -08:00
Ian Cordasco
14da5cf180 A possible fix for #1036
I can only assume that the only possible thing to close on a session are the
adapters. As such, I wrote the close method for a session object which closes
all possible adapters.
2012-12-18 09:43:55 -05:00
Kenneth Reitz
6acce57271 no kwargs 2012-12-18 05:01:32 -05:00
64 changed files with 2814 additions and 977 deletions

3
.gitignore vendored
View File

@@ -5,12 +5,15 @@ nosetests.xml
junit-report.xml
pylint.txt
toy.py
tox.ini
violations.pyflakes.txt
cover/
build/
docs/_build
requests.egg-info/
*.pyc
*.swp
*.egg
env/
.workon

View File

@@ -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

View File

@@ -117,3 +117,19 @@ 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>
- Bryce Boe <bbzbryce@gmail.com> @bboe
- Colin Dunklau <colin.dunklau@gmail.com> @cdunklau
- Bob Carroll <bob.carroll@alum.rit.edu> @rcarz
- Hugo Osvaldo Barrera <hugo@osvaldobarrera.com.ar> @hobarrera
- Łukasz Langa <lukasz@langa.pl> @llanga
- Dave Shawley <daveshawley@gmail.com>
- James Clarke (jam)
- Kevin Burke <kev@inburke.com>
- Flavio Curella
- David Pursehouse <david.pursehouse@gmail.com> @dpursehouse

View File

@@ -3,6 +3,64 @@
History
-------
1.2.3 (2013-05-25)
++++++++++++++++++
- Simple packaging fix
1.2.2 (2013-05-23)
++++++++++++++++++
- Simple packaging fix
1.2.1 (2013-05-20)
++++++++++++++++++
- Python 3.3.2 compatibility
- Always percent-encode location headers
- Fix connection adapter matching to be most-specific first
- new argument to the default connection adapter for passing a block argument
- prevent a KeyError when there's no link headers
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
multiprocessing 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)
++++++++++++++++++
@@ -31,7 +89,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)
@@ -566,10 +624,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)

View File

@@ -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.

View File

@@ -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

2
NOTICE
View File

@@ -6,7 +6,7 @@ Urllib3 License
This is the MIT license: http://www.opensource.org/licenses/mit-license.php
Copyright 2008-2011 Andrey Petrov and contributors (see CONTRIBUTORS.txt),
Modifications copyright 2022 Kenneth Reitz.
Modifications copyright 2012 Kenneth Reitz.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software

View File

@@ -1,10 +1,15 @@
Requests: HTTP for Humans
=========================
.. image:: https://badge.fury.io/py/requests.png
:target: http://badge.fury.io/py/requests
.. image:: https://travis-ci.org/kennethreitz/requests.png?branch=master
:target: https://travis-ci.org/kennethreitz/requests
.. image:: https://pypip.in/d/requests/badge.png
:target: https://crate.io/packages/requests/
Requests is an Apache2 Licensed HTTP library, written in Python, for human
beings.
@@ -54,7 +59,7 @@ Features
Installation
------------
To install requests, simply:
To install Requests, simply:
.. code-block:: bash
@@ -69,12 +74,17 @@ Or, if you absolutely must:
But, you really shouldn't do that.
Documentation
-------------
Documentation is available at http://docs.python-requests.org/.
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_.

View File

@@ -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>
@@ -11,8 +11,7 @@
<p>
Requests is an elegant and simple HTTP library for Python, built for
human beings. You are currently looking at the documentation of the
development release.
human beings.
</p>
@@ -34,13 +33,18 @@
<a href="http://gum.co/RRZc" class="gumroad-button">Requests Pro</a><script type="text/javascript" src="https://gumroad.com/js/gumroad-button.js"></script>
</p>
<h3>Feedback</h3>
<p>
Feedback is greatly appreciated. If you have any questions, comments,
random praise, or anonymous threats, <a href="mailto:me@kennethreitz.com">
shoot me an email</a>.
</p>
<h3>Translations</h3>
<ul>
<li><a href="http://docs.python-requests.org/">English</a></li>
<li><a href="http://fr.python-requests.org/">French</a></li>
<li><a href="http://de.python-requests.org/">German</a></li>
<li><a href="http://jp.python-requests.org/">Japanese</a></li>
<li><a href="http://cn.python-requests.org/">Chinese</a></li>
<li><a href="http://pt.python-requests.org/">Portuguese</a></li>
</ul>
<h3>Useful Links</h3>
<ul>

View File

@@ -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>
@@ -32,3 +32,4 @@
<p>
<a href="http://gum.co/RRZc" class="gumroad-button">Requests Pro</a><script type="text/javascript" src="https://gumroad.com/js/gumroad-button.js"></script>
</p>

View File

@@ -1,7 +1,7 @@
.. _api:
API
===
Developer Interface
===================
.. module:: requests
@@ -13,19 +13,11 @@ important right here and provide links to the canonical documentation.
Main Interface
--------------
All of Request's functionality can be accessed by these 7 methods.
All of Requests' functionality can be accessed by these 7 methods.
They all return an instance of the :class:`Response <Response>` object.
.. autofunction:: request
---------------------
.. autoclass:: Response
:inherited-members:
---------------------
.. autofunction:: head
.. autofunction:: get
.. autofunction:: post
@@ -34,61 +26,34 @@ 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:
.. autoclass:: requests.adapters.HTTPAdapter
:inherited-members:
Exceptions
~~~~~~~~~~
.. module:: requests
.. autoexception:: requests.exceptions.RequestException
.. autoexception:: requests.exceptions.ConnectionError
.. autoexception:: requests.exceptions.HTTPError
.. autoexception:: requests.exceptions.URLRequired
.. autoexception:: requests.exceptions.TooManyRedirects
.. autoexception:: RequestException
.. autoexception:: ConnectionError
.. autoexception:: HTTPError
.. autoexception:: URLRequired
.. 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
~~~~~~~~~~~~~~~~~~
@@ -109,26 +74,17 @@ Status Code Lookup
Cookies
~~~~~~~
.. autofunction:: dict_from_cookiejar
.. autofunction:: cookiejar_from_dict
.. autofunction:: add_dict_to_cookiejar
.. autofunction:: requests.utils.dict_from_cookiejar
.. autofunction:: requests.utils.cookiejar_from_dict
.. autofunction:: requests.utils.add_dict_to_cookiejar
Encodings
~~~~~~~~~
.. autofunction:: get_encodings_from_content
.. autofunction:: get_encoding_from_headers
.. autofunction:: get_unicode_from_response
.. 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.
.. autofunction:: requests.utils.get_encodings_from_content
.. autofunction:: requests.utils.get_encoding_from_headers
.. autofunction:: requests.utils.get_unicode_from_response
Classes
@@ -140,8 +96,99 @@ Classes
.. autoclass:: requests.Request
:inherited-members:
.. autoclass:: requests.PreparedRequest
:inherited-members:
.. _sessionapi:
.. autoclass:: requests.Session
:inherited-members:
.. autoclass:: requests.adapters.HTTPAdapter
: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.
::
import requests
import logging
# these two lines enable debugging at httplib level (requests->urllib3->httplib)
# you will see the REQUEST, including HEADERS and DATA, and RESPONSE with HEADERS but without DATA.
# the only thing missing will be the response.body which is not logged.
import httplib
httplib.HTTPConnection.debuglevel = 1
logging.basicConfig() # you need to initialize logging, otherwise you will not see anything from requests
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
requests.get('http://httpbin.org/headers')
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

View File

@@ -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

View File

@@ -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/>`_

View File

@@ -3,7 +3,7 @@
Support
=======
If you have a questions or issues about Requests, there are several options:
If you have questions or issues about Requests, there are several options:
Send a Tweet
------------

View File

@@ -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
View 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.

View File

@@ -3,41 +3,58 @@ 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 and start 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 compatibility will be accepted, as long as they don't complicate the codebase.
Are you crazy?
--------------
- SPDY support would be awesome. No C extensions.
Downstream Repackaging
----------------------
If you are repackaging Requests, please note that you must also redistribute the ``cacerts.pem`` file in order to get correct SSL functionality.

View File

@@ -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 3,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.
@@ -119,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.
@@ -128,6 +129,7 @@ you.
.. toctree::
:maxdepth: 1
dev/philosophy
dev/internals
dev/todo
dev/authors

View File

@@ -13,11 +13,11 @@ The Session object allows you to persist certain parameters across
requests. It also persists cookies across all requests made from the
Session instance.
A session object has all the methods of the main Requests API.
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.
@@ -49,18 +49,18 @@ Request and Response Objects
----------------------------
Whenever a call is made to requests.*() you are doing two major things. First,
you are constructing a ``Request`` object which will be sent of to a server
you are constructing a ``Request`` object which will be sent off to a server
to request or query some resource. Second, a ``Response`` object is generated
once ``requests`` gets a response back from the server. The response object
once ``requests`` gets a response back from the server. The Response object
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,10 +73,44 @@ 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'}
'Accept': '*/*', 'User-Agent': 'python-requests/1.2.0'}
Prepared Requests
-----------------
Whenever you receive a :class:`Response <requests.models.Response>` object
from an API call or a Session call, the ``request`` attribute is actually the
``PreparedRequest`` that was used. In some cases you may wish to do some extra
work to the body or headers (or anything else really) before sending a
request. The simple recipe for this is the following::
from requests import Request, Session
s = Session()
prepped = Request('GET', # or any other method, 'POST', 'PUT', etc.
url,
data=data
headers=headers
# ...
).prepare()
# do something with prepped.body
# do something with prepped.headers
resp = s.send(prepped,
stream=stream,
verify=verify,
proxies=proxies,
cert=cert,
timeout=timeout,
# etc.
)
print(resp.status_code)
Since you are not doing anything special with the ``Request`` object, you
prepare it immediately and modified the ``PreparedRequest`` object. You then
send that with the other parameters you would have sent to ``requests.*`` or
``Sesssion.*``.
SSL Cert Verification
---------------------
@@ -86,14 +120,14 @@ Requests can verify SSL certificates for HTTPS requests, just like a web browser
>>> requests.get('https://kennethreitz.com', verify=True)
requests.exceptions.SSLError: hostname 'kennethreitz.com' doesn't match either of '*.herokuapp.com', 'herokuapp.com'
I don't have SSL setup on this domain, so it fails. Excellent. Github does though::
I don't have SSL setup on this domain, so it fails. Excellent. GitHub does though::
>>> requests.get('https://github.com', verify=True)
<Response [200]>
You can also pass ``verify`` the path to a CA_BUNDLE file for private certs. You can also set the ``REQUESTS_CA_BUNDLE`` environment variable.
Requests can also ignore verifying the SSL certficate if you set ``verify`` to False.
Requests can also ignore verifying the SSL certificate if you set ``verify`` to False.
::
@@ -102,7 +136,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 +163,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 +172,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 +251,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):
@@ -217,6 +268,8 @@ Then, we can make a request using our Pizza Auth::
>>> requests.get('http://pizzabin.org/admin', auth=PizzaAuth('kenneth'))
<Response [200]>
.. _streaming-requests
Streaming Requests
------------------
@@ -225,14 +278,15 @@ APIs such as the `Twitter Streaming API <https://dev.twitter.com/docs/streaming-
To use the Twitter Streaming API to track the keyword "requests"::
import requests
import json
import requests
r = requests.post('https://stream.twitter.com/1/statuses/filter.json',
data={'track': 'requests'}, auth=('username', 'password'), stream=True)
r = requests.get('http://httpbin.org/stream/20', stream=True)
for line in r.iter_lines():
if line: # filter out keep-alive new lines
# filter out keep-alive new lines
if line:
print json.loads(line)
@@ -245,8 +299,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 +309,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 +372,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 +434,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 +471,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 +534,61 @@ 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'}
Transport Adapters
------------------
As of v1.0.0, Requests has moved to a modular internal design. Part of the
reason this was done was to implement Transport Adapters, originally
`described here`_. Transport Adapters provide a mechanism to define interaction
methods for an HTTP service. In particular, they allow you to apply per-service
configuration.
Requests ships with a single Transport Adapter, the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`. This adapter provides the
default Requests interaction with HTTP and HTTPS using the powerful `urllib3`_
library. Whenever a Requests :class:`Session <Session>` is initialized, one of
these is attached to the :class:`Session <Session>` object for HTTP, and one
for HTTPS.
Requests enables users to create and use their own Transport Adapters that
provide specific functionality. Once created, a Transport Adapter can be
mounted to a Session object, along with an indication of which web services
it should apply to.
::
>>> s = requests.Session()
>>> s.mount('http://www.github.com', MyAdapter())
The mount call registers a specific instance of a Transport Adapter to a
prefix. Once mounted, any HTTP request made using that session whose URL starts
with the given prefix will use the given Transport Adapter.
Implementing a Transport Adapter is beyond the scope of this documentation, but
a good start would be to subclass the ``requests.adapters.BaseAdapter`` class.
.. _`described here`: http://kennethreitz.org/exposures/the-future-of-python-http
.. _`urllib3`: https://github.com/shazow/urllib3
Blocking Or Non-Blocking?
-------------------------
With the default Transport Adapter in place, Requests does not provide any kind
of non-blocking IO. The ``Response.content`` property will block until the
entire response has been downloaded. If you require more granularity, the
streaming features of the library (see :ref:`streaming-requests`) allow you to
retrieve smaller quantities of the response at a time. However, these calls
will still block.
If you are concerned about the use of blocking IO, there are lots of projects
out there that combine Requests with one of Python's asynchronicity frameworks.
Two excellent examples are `grequests`_ and `requests-futures`_.
.. _`grequests`: https://github.com/kennethreitz/grequests
.. _`requests-futures`: https://github.com/ross/requests-futures

View File

@@ -32,6 +32,17 @@ Providing the credentials in a tuple like this is exactly the same as the
``HTTPBasicAuth`` example above.
netrc Authentication
~~~~~~~~~~~~~~~~~~~~
If no authentication method is given with the ``auth`` argument, Requests will
attempt to get the authentication credentials for the URL's hostname from the
user's netrc file.
If credentials for the hostname are found, the request is sent with HTTP Basic
Auth.
Digest Authentication
---------------------
@@ -44,6 +55,27 @@ and Requests supports this out of the box as well::
<Response [200]>
OAuth 1 Authentication
----------------------
A common form of authentication for several web APIs is OAuth. The ``requests-oauthlib``
library allows Requests users to easily make OAuth authenticated requests::
>>> import requests
>>> from requests_oauthlib import OAuth1
>>> url = 'https://api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth1('YOUR_APP_KEY', 'YOUR_APP_SECRET',
'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET')
>>> requests.get(url, auth=auth)
<Response [200]>
For more information on how to OAuth flow works, please see the official `OAuth`_ website.
For examples and documentation on requests-oauthlib, please see the `requests_oauthlib`_
repository on GitHub
Other Authentication
--------------------
@@ -53,12 +85,11 @@ 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.
GitHub page and follow the instructions.
New Forms of Authentication
@@ -69,15 +100,28 @@ 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,
``__call__()`` method::
>>> import requests
>>> class MyAuth(requests.auth.AuthBase):
... def __call__(self, r):
... # Implement my authentication
... return r
...
>>> url = 'http://httpbin.org/get'
>>> requests.get(url, auth=MyAuth())
<Response [200]>
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
Further examples can be found under the `Requests organization`_ and in the
``auth.py`` file.
.. _OAuth: https://github.com/requests/requests-oauthlib
.. _OAuth: http://oauth.net/
.. _requests_oauthlib: 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

View File

@@ -10,7 +10,7 @@ The first step to using any software package is getting it properly installed.
Distribute & Pip
----------------
Installing requests is simple with `pip <http://www.pip-installer.org/>`_::
Installing Requests is simple with `pip <http://www.pip-installer.org/>`_::
$ pip install requests
@@ -22,11 +22,11 @@ But, you really `shouldn't do that <http://www.pip-installer.org/en/latest/other
Cheeseshop Mirror
-----------------
Cheeseshop (PyPI) Mirror
------------------------
If the Cheeseshop is down, you can also install Requests from one of the
mirrors. `Crate.io <http://crate.io>`_ is one of them::
If the Cheeseshop (a.k.a. PyPI) is down, you can also install Requests from one
of the mirrors. `Crate.io <http://crate.io>`_ is one of them::
$ pip install -i http://simple.crate.io/ requests

View File

@@ -20,7 +20,7 @@ All contributions to Requests should keep these important rules in mind.
.. _`apache2`:
Apache2 License
-----------
---------------
A large number of open source projects you find today are `GPL Licensed`_.
While the GPL has its time and place, it should most certainly not be your

View File

@@ -19,7 +19,7 @@ Let's get started with some simple examples.
Make a Request
------------------
--------------
Making a request with Requests is very simple.
@@ -69,7 +69,10 @@ following code::
You can see that the URL has been correctly encoded by printing the URL::
>>> print r.url
u'http://httpbin.org/get?key2=value2&key1=value1'
http://httpbin.org/get?key2=value2&key1=value1
Note that any dictionary key whose value is ``None`` will not be added to the
URL's query string.
Response Content
@@ -81,7 +84,7 @@ again::
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.text
'[{"repository":{"open_issues":0,"url":"https://github.com/...
u'[{"repository":{"open_issues":0,"url":"https://github.com/...
Requests will automatically decode content from the server. Most unicode
charsets are seamlessly decoded.
@@ -114,7 +117,7 @@ You can also access the response body as bytes, for non-text requests::
The ``gzip`` and ``deflate`` transfer-encodings are automatically decoded for you.
For example, to create an image from binary data returned by a request, you can
use the following code:
use the following code::
>>> from PIL import Image
>>> from StringIO import StringIO
@@ -131,7 +134,9 @@ There's also a builtin JSON decoder, in case you're dealing with JSON data::
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
In case the JSON decoding fails, ``r.json`` raises an exception.
In case the JSON decoding fails, ``r.json`` raises an exception. For example, if
the response gets a 401 (Unauthorized), attempting ``r.json`` raises ``ValueError:
No JSON object could be decoded``
Raw Response Content
@@ -141,7 +146,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)
@@ -168,19 +173,19 @@ More complicated POST requests
------------------------------
Typically, you want to send some form-encoded data — much like an HTML form.
To do this, simply pass a dictionary to the `data` argument. Your
To do this, simply pass a dictionary to the ``data`` argument. Your
dictionary of data will automatically be form-encoded when the request is made::
>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> 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 +210,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 +225,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 +240,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... //
...
}
@@ -258,7 +263,7 @@ reference::
>>> r.status_code == requests.codes.ok
True
If we made a bad request (non-200 response), we can raise it with
If we made a bad request (a 4XX client error or 5XX server error response), we can raise it with
:class:`Response.raise_for_status()`::
>>> bad_r = requests.get('http://httpbin.org/status/404')
@@ -287,14 +292,13 @@ 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',
'server': 'nginx/1.0.4',
'x-runtime': '148ms',
'etag': '"e1ca502697e5c9317743dc078f67693f"',
'content-type': 'application/json; charset=utf-8'
'content-type': 'application/json'
}
The dictionary is special, though: it's made just for HTTP headers. According to
@@ -304,15 +308,10 @@ Headers are case-insensitive.
So, we can access the headers using any capitalization we want::
>>> r.headers['Content-Type']
'application/json; charset=utf-8'
'application/json'
>>> r.headers.get('content-type')
'application/json; charset=utf-8'
If a header doesn't exist in the Response, its value defaults to ``None``::
>>> r.headers['X-Random']
None
'application/json'
Cookies
@@ -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::
@@ -344,7 +343,7 @@ Requests will automatically perform location redirection while using the GET
and OPTIONS verbs.
GitHub redirects all HTTP requests to HTTPS. We can use the ``history`` method
of the Response object to track redirection. Let's see what Github does::
of the Response object to track redirection. Let's see what GitHub does::
>>> r = requests.get('http://github.com')
>>> r.url
@@ -354,8 +353,9 @@ of the Response object to track redirection. Let's see what Github does::
>>> r.history
[<Response [301]>]
The :class:`Response.history` list contains a list of the
:class:`Request` objects that were created in order to complete the request. The list is sorted from the oldest to the most recent request.
The :class:`Response.history` list contains the :class:`Request` objects that
were created in order to complete the request. The list is sorted from the
oldest to the most recent request.
If you're using GET or OPTIONS, you can disable redirection handling with the
``allow_redirects`` parameter::
@@ -379,17 +379,18 @@ redirection as well::
Timeouts
--------
You can tell requests to stop waiting for a response after a given number of
You can tell Requests to stop waiting for a response after a given number of
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:
``timeout`` only effects the connection process itself, not the
``timeout`` only affects the connection process itself, not the
downloading of the response body.
@@ -397,25 +398,20 @@ Errors and Exceptions
---------------------
In the event of a network problem (e.g. DNS failure, refused connection, etc),
Requests will raise a :class:`ConnectionError` exception.
Requests will raise a :class:`~requests.exceptions.ConnectionError` exception.
In the event of the rare invalid HTTP response, Requests will raise
an :class:`HTTPError` exception.
In the event of the rare invalid HTTP response, Requests will raise an
:class:`~requests.exceptions.HTTPError` exception.
If a request times out, a :class:`Timeout` exception is raised.
If a request times out, a :class:`~requests.exceptions.Timeout` exception is
raised.
If a request exceeds the configured number of maximum redirections, a
:class:`TooManyRedirects` exception is raised.
:class:`~requests.exceptions.TooManyRedirects` exception is raised.
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.

View File

@@ -36,18 +36,24 @@ 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.3'
__build__ = 0x01003
__version__ = '1.2.3'
__build__ = 0x010203
__author__ = 'Kenneth Reitz'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2012 Kenneth Reitz'
__copyright__ = 'Copyright 2013 Kenneth Reitz'
# Attempt to enable urllib3's SNI support, if possible
try:
from requests.packages.urllib3.contrib import pyopenssl
pyopenssl.inject_into_urllib3()
except ImportError:
pass
from . import utils
from .models import Request, Response, PreparedRequest

View File

@@ -11,10 +11,11 @@ and maintain connections.
import socket
from .models import Response
from .packages.urllib3.poolmanager import PoolManager, proxy_from_url
from .hooks import dispatch_hook
from .compat import urlparse
from .utils import DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers
from .packages.urllib3.poolmanager import PoolManager, ProxyManager
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
@@ -22,7 +23,9 @@ 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_POOLBLOCK = False
DEFAULT_POOLSIZE = 10
DEFAULT_RETRIES = 0
@@ -41,20 +44,81 @@ class BaseAdapter(object):
class HTTPAdapter(BaseAdapter):
"""Built-In HTTP Adapter for Urllib3."""
def __init__(self, pool_connections=DEFAULT_POOLSIZE, pool_maxsize=DEFAULT_POOLSIZE):
self.max_retries = DEFAULT_RETRIES
"""The built-in HTTP Adapter for urllib3.
Provides a general-case interface for Requests sessions to contact HTTP and
HTTPS urls by implementing the Transport Adapter interface. This class will
usually be created by the :class:`Session <Session>` class under the
covers.
:param pool_connections: The number of urllib3 connection pools to cache.
:param pool_maxsize: The maximum number of connections to save in the pool.
:param max_retries: The maximum number of retries each connection should attempt.
:param pool_block: Whether the connection pool should block for connections.
Usage::
>>> import requests
>>> s = requests.Session()
>>> a = requests.adapters.HTTPAdapter()
>>> s.mount('http://', a)
"""
__attrs__ = ['max_retries', 'config', '_pool_connections', '_pool_maxsize',
'_pool_block']
def __init__(self, pool_connections=DEFAULT_POOLSIZE,
pool_maxsize=DEFAULT_POOLSIZE, max_retries=DEFAULT_RETRIES,
pool_block=DEFAULT_POOLBLOCK):
self.max_retries = max_retries
self.config = {}
super(HTTPAdapter, self).__init__()
self.init_poolmanager(pool_connections, pool_maxsize)
self._pool_connections = pool_connections
self._pool_maxsize = pool_maxsize
self._pool_block = pool_block
def init_poolmanager(self, connections, maxsize):
self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize)
self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block)
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,
block=self._pool_block)
def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK):
"""Initializes a urllib3 PoolManager. This method should not be called
from user code, and is only exposed for use when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
:param connections: The number of urllib3 connection pools to cache.
:param maxsize: The maximum number of connections to save in the pool.
:param block: Block when no free connections are available.
"""
# save these values for pickling
self._pool_connections = connections
self._pool_maxsize = maxsize
self._pool_block = block
self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize,
block=block)
def cert_verify(self, conn, url, verify, cert):
if url.startswith('https') and verify:
"""Verify a SSL certificate. This method should not be called from user
code, and is only exposed for use when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
:param conn: The urllib3 connection object associated with the cert.
:param url: The requested URL.
:param verify: Whether we should actually verify the certificate.
:param cert: The SSL certificate to verify.
"""
if url.lower().startswith('https') and verify:
cert_loc = None
@@ -75,13 +139,21 @@ 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):
"""Builds a :class:`Response <requests.Response>` object from a urllib3
response. This should not be called from user code, and is only exposed
for use when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`
:param req: The :class:`PreparedRequest <PreparedRequest>` used to generate the response.
:param resp: The urllib3 response object.
"""
response = Response()
# Fallback to None if there's no status_code, for whatever reason.
@@ -93,6 +165,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')
@@ -106,51 +179,146 @@ 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):
"""Returns a connection for the given URL."""
"""Returns a urllib3 connection for the given URL. This should not be
called from user code, and is only exposed for use when subclassing the
:class:`HTTPAdapter <reqeusts.adapters.HTTPAdapter>`.
:param url: The URL to connect to.
:param proxies: (optional) A Requests-style dictionary of proxies used on this request.
"""
proxies = proxies or {}
proxy = proxies.get(urlparse(url).scheme)
proxy = proxies.get(urlparse(url.lower()).scheme)
if proxy:
conn = proxy_from_url(proxy)
proxy = prepend_scheme_if_needed(proxy, urlparse(url.lower()).scheme)
conn = ProxyManager(self.poolmanager.connection_from_url(proxy))
else:
conn = self.poolmanager.connection_from_url(url)
conn = self.poolmanager.connection_from_url(url.lower())
return conn
def close(self):
"""Dispose of any internal state.
"""Disposes of any internal state.
Currently, this just closes the PoolManager, which closes pooled
connections.
"""
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.
This should not be called from user code, and is only exposed for use
when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
:param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
:param proxies: A dictionary of schemes to proxy URLs.
"""
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.
This should not be called from user code, and is only exposed for use
when subclassing the
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
:param request: The :class:`PreparedRequest <PreparedRequest>` to add headers to.
:param kwargs: The keyword arguments from the call to send().
"""
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."""
"""Sends PreparedRequest object. Returns Response object.
:param request: The :class:`PreparedRequest <PreparedRequest>` being sent.
:param stream: (optional) Whether to stream the request content.
:param timeout: (optional) The timeout on the request.
:param verify: (optional) Whether to verify SSL certificates.
:param vert: (optional) Any user-provided SSL certificate to be trusted.
:param proxies: (optional) The proxies dictionary to apply to the request.
"""
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)
@@ -164,7 +332,7 @@ class HTTPAdapter(BaseAdapter):
elif isinstance(e, TimeoutError):
raise Timeout(e)
else:
raise Timeout('Request timed out.')
raise
r = self.build_response(request, resp)

View File

@@ -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.
"""

View File

@@ -8,6 +8,7 @@ This module contains the authentication handlers for Requests.
"""
import os
import re
import time
import hashlib
import logging
@@ -36,6 +37,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):
@@ -48,7 +50,7 @@ class HTTPBasicAuth(AuthBase):
class HTTPProxyAuth(HTTPBasicAuth):
"""Attaches HTTP Proxy Authenetication to a given Request object."""
"""Attaches HTTP Proxy Authentication to a given Request object."""
def __call__(self, r):
r.headers['Proxy-Authorization'] = _basic_auth_str(self.username, self.password)
return r
@@ -68,18 +70,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,26 +131,29 @@ 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:
self.chal = parse_dict_header(s_auth.replace('Digest ', ''))
setattr(self, 'num_401_calls', num_401_calls + 1)
pat = re.compile(r'digest ', flags=re.IGNORECASE)
self.chal = parse_dict_header(pat.sub('', s_auth, count=1))
# Consume content and release the original connection
# to allow our new request to reuse the same one.
@@ -153,11 +161,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):

View File

@@ -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-----

View File

@@ -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())

View File

@@ -83,13 +83,14 @@ except ImportError:
# ---------
if is_py2:
from urllib import quote, unquote, quote_plus, unquote_plus, urlencode
from urllib import quote, unquote, quote_plus, unquote_plus, urlencode, getproxies, proxy_bypass
from urlparse import urlparse, urlunparse, urljoin, urlsplit, urldefrag
from urllib2 import parse_http_list
import cookielib
from Cookie import Morsel
from StringIO import StringIO
from .packages.urllib3.packages.ordered_dict import OrderedDict
from httplib import IncompleteRead
builtin_str = str
bytes = str
@@ -98,14 +99,14 @@ 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
from urllib.request import parse_http_list, getproxies, proxy_bypass
from http import cookiejar as cookielib
from http.cookies import Morsel
from io import StringIO
from collections import OrderedDict
from http.client import IncompleteRead
builtin_str = str
str = str

View File

@@ -6,6 +6,7 @@ Compatibility code to be able to use `cookielib.CookieJar` with requests.
requests.utils imports from here, so be careful with imports.
"""
import time
import collections
from .compat import cookielib, urlparse, Morsel
@@ -69,6 +70,14 @@ class MockRequest(object):
def unverifiable(self):
return self.is_unverifiable()
@property
def origin_req_host(self):
return self.get_origin_req_host()
@property
def host(self):
return self.get_host()
class MockResponse(object):
"""Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`.
@@ -240,18 +249,33 @@ 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 set_cookie(self, cookie, *args, **kwargs):
if cookie.value.startswith('"') and cookie.value.endswith('"'):
cookie.value = cookie.value.replace('\\"', '')
return super(RequestsCookieJar, self).set_cookie(cookie, *args, **kwargs)
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 +321,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):
@@ -338,19 +364,23 @@ def create_cookie(name, value, **kwargs):
def morsel_to_cookie(morsel):
"""Convert a Morsel object into a Cookie containing the one k/v pair."""
expires = None
if morsel["max-age"]:
expires = time.time() + morsel["max-age"]
elif morsel['expires']:
expires = morsel['expires']
if type(expires) == type(""):
time_template = "%a, %d-%b-%Y %H:%M:%S GMT"
expires = time.mktime(time.strptime(expires, time_template))
c = create_cookie(
name=morsel.key,
value=morsel.value,
version=morsel['version'] or 0,
port=None,
port_specified=False,
domain=morsel['domain'],
domain_specified=bool(morsel['domain']),
domain_initial_dot=morsel['domain'].startswith('.'),
path=morsel['path'],
path_specified=bool(morsel['path']),
secure=bool(morsel['secure']),
expires=morsel['max-age'] or morsel['expires'],
expires=expires,
discard=False,
comment=morsel['comment'],
comment_url=bool(morsel['comment']),

View File

@@ -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):
@@ -49,3 +53,7 @@ class InvalidSchema(RequestException, ValueError):
class InvalidURL(RequestException, ValueError):
""" The URL provided was somehow invalid. """
class ChunkedEncodingError(RequestException):
"""The server declared chunked encoding but sent an invalid chunk."""

View File

@@ -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

View File

@@ -9,33 +9,34 @@ This module contains the primary objects that power Requests.
import collections
import logging
import datetime
from io import BytesIO
from .hooks import default_hooks
from .structures import CaseInsensitiveDict
from .status_codes import codes
from .auth import HTTPBasicAuth
from .cookies import cookiejar_from_dict, get_cookie_header
from .packages.urllib3.filepost import encode_multipart_formdata
from .exceptions import HTTPError, RequestException, MissingSchema, InvalidURL
from .packages.urllib3.util import parse_url
from .exceptions import (
HTTPError, RequestException, MissingSchema, InvalidURL,
ChunkedEncodingError)
from .utils import (
stream_untransfer, guess_filename, requote_uri,
guess_filename, get_auth_from_url, requote_uri,
stream_decode_response_unicode, to_key_val_list, parse_header_links,
iter_slices, guess_json_utf)
iter_slices, guess_json_utf, super_len)
from .compat import (
cookielib, urlparse, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
is_py2, chardet, json, builtin_str, basestring)
cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,
is_py2, chardet, json, builtin_str, basestring, IncompleteRead)
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."""
@@ -62,7 +63,7 @@ class RequestEncodingMixin(object):
"""Encode parameters in a piece of data.
Will successfully encode parameters when passed as a dict or a list of
2-tuples. Order is retained if data is a list of 2-tuples but abritrary
2-tuples. Order is retained if data is a list of 2-tuples but arbitrary
if parameters are supplied as a dict.
"""
@@ -101,16 +102,22 @@ class RequestEncodingMixin(object):
files = to_key_val_list(files or {})
for field, val in fields:
if isinstance(val, list):
for v in val:
new_fields.append((field, builtin_str(v)))
else:
new_fields.append((field, builtin_str(val)))
if isinstance(val, basestring) or not hasattr(val, '__iter__'):
val = [val]
for v in val:
if v is not None:
new_fields.append(
(field.decode('utf-8') if isinstance(field, bytes) else field,
v.encode('utf-8') if isinstance(v, str) else v))
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
@@ -118,7 +125,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)
@@ -147,7 +159,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,
@@ -157,7 +190,6 @@ class Request(RequestHooksMixin):
params=dict(),
auth=None,
cookies=None,
timeout=None,
hooks=None):
# Default empty dicts for dict params.
@@ -179,44 +211,86 @@ 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(
method=self.method,
url=self.url,
headers=self.headers,
files=self.files,
data=self.data,
params=self.params,
auth=self.auth,
cookies=self.cookies,
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 prepare(self, method=None, url=None, headers=None, files=None,
data=None, params=None, auth=None, cookies=None, hooks=None):
"""Prepares the the entire request with the given parameters."""
self.prepare_method(method)
self.prepare_url(url, params)
self.prepare_headers(headers)
self.prepare_cookies(cookies)
self.prepare_body(data, files)
self.prepare_auth(auth, 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
self.prepare_hooks(hooks)
def __repr__(self):
return '<PreparedRequest [%s]>' % (self.method)
def copy(self):
p = PreparedRequest()
p.method = self.method
p.url = self.url
p.headers = self.headers
p.body = self.body
p.hooks = self.hooks
return p
def prepare_method(self, method):
"""Prepares the given HTTP method."""
self.method = method
@@ -235,16 +309,28 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
pass
# Support for unicode domain names and paths.
scheme, netloc, path, _params, query, fragment = urlparse(url)
scheme, auth, host, port, path, query, fragment = parse_url(url)
if not scheme:
raise MissingSchema("Invalid URL %r: No schema supplied" % url)
if not host:
raise InvalidURL("Invalid URL %r: No host supplied" % url)
# Only want to apply IDNA to the hostname
try:
netloc = netloc.encode('idna').decode('utf-8')
host = host.encode('idna').decode('utf-8')
except UnicodeError:
raise InvalidURL('URL has an invalid label.')
# Carefully reconstruct the network location
netloc = auth or ''
if netloc:
netloc += '@'
netloc += host
if port:
netloc += ':' + str(port)
# Bare domains aren't valid URLs.
if not path:
path = '/'
@@ -256,8 +342,6 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
netloc = netloc.encode('utf-8')
if isinstance(path, str):
path = path.encode('utf-8')
if isinstance(_params, str):
_params = _params.encode('utf-8')
if isinstance(query, str):
query = query.encode('utf-8')
if isinstance(fragment, str):
@@ -270,13 +354,14 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
else:
query = enc_params
url = requote_uri(urlunparse([scheme, netloc, path, _params, query, fragment]))
url = requote_uri(urlunparse([scheme, netloc, path, None, query, fragment]))
self.url = url
def prepare_headers(self, headers):
"""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()
@@ -284,42 +369,79 @@ 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 = 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 = None
if is_stream:
body = data
if files:
raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
if length is not None:
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
@@ -331,6 +453,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."""
@@ -344,12 +469,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):
@@ -387,6 +515,10 @@ class Response(object):
#: 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)
@@ -398,6 +530,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:
@@ -408,28 +544,41 @@ 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
return iter_slices(self._content, chunk_size)
def generate():
while 1:
chunk = self.raw.read(chunk_size)
if not chunk:
break
yield chunk
try:
# Special case for urllib3.
try:
for chunk in self.raw.stream(chunk_size,
decode_content=True):
yield chunk
except IncompleteRead as e:
raise ChunkedEncodingError(e)
except AttributeError:
# Standard file-like object.
while 1:
chunk = self.raw.read(chunk_size)
if not chunk:
break
yield chunk
self._content_consumed = True
gen = stream_untransfer(generate(), self)
gen = generate()
if decode_unicode:
gen = stream_decode_response_unicode(gen, self)
@@ -437,16 +586,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
@@ -474,7 +622,7 @@ class Response(object):
raise RuntimeError(
'The content for this response was already consumed')
if self.status_code is 0:
if self.status_code == 0:
self._content = None
else:
self._content = bytes().join(self.iter_content(CONTENT_CHUNK_SIZE)) or bytes()
@@ -520,8 +668,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
@@ -530,14 +681,14 @@ 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):
"""Returns the parsed header links of the response, if any."""
header = self.headers['link']
header = self.headers.get('link')
# l = MultiDict()
l = {}
@@ -552,7 +703,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 = ''
@@ -563,9 +714,12 @@ 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):
"""Closes the underlying file descriptor and releases the connection
back to the pool.
*Note: Should not normally need to be called explicitly.*
"""
return self.raw.release_conn()

View File

@@ -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()

View File

@@ -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:

View File

@@ -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

View 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"

View File

@@ -25,8 +25,6 @@
# 02110-1301 USA
######################### END LICENSE BLOCK #########################
from . import constants
# KOI8-R language model
# Character Mapping Table:
KOI8R_CharToOrderMap = (

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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()
]

View File

@@ -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 = (

View File

@@ -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}

View File

@@ -1,5 +1,5 @@
# urllib3/__init__.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php

View File

@@ -1,11 +1,11 @@
# urllib3/_collections.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
from collections import MutableMapping
from threading import Lock
from threading import RLock
try: # Python 2.7+
from collections import OrderedDict
@@ -40,18 +40,18 @@ class RecentlyUsedContainer(MutableMapping):
self.dispose_func = dispose_func
self._container = self.ContainerCls()
self._lock = Lock()
self.lock = RLock()
def __getitem__(self, key):
# Re-insert the item, moving it to the end of the eviction line.
with self._lock:
with self.lock:
item = self._container.pop(key)
self._container[key] = item
return item
def __setitem__(self, key, value):
evicted_value = _Null
with self._lock:
with self.lock:
# Possibly evict the existing value of 'key'
evicted_value = self._container.get(key, _Null)
self._container[key] = value
@@ -65,21 +65,21 @@ class RecentlyUsedContainer(MutableMapping):
self.dispose_func(evicted_value)
def __delitem__(self, key):
with self._lock:
with self.lock:
value = self._container.pop(key)
if self.dispose_func:
self.dispose_func(value)
def __len__(self):
with self._lock:
with self.lock:
return len(self._container)
def __iter__(self):
raise NotImplementedError('Iteration over this class is unlikely to be threadsafe.')
def clear(self):
with self._lock:
with self.lock:
# Copy pointers to all values, then wipe the mapping
# under Python 2, this copies the list of values twice :-|
values = list(self._container.values())
@@ -90,5 +90,5 @@ class RecentlyUsedContainer(MutableMapping):
self.dispose_func(value)
def keys(self):
with self._lock:
with self.lock:
return self._container.keys()

View File

@@ -1,5 +1,5 @@
# urllib3/connectionpool.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -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, assert_fingerprint
try: # Python 3
from http.client import HTTPConnection, HTTPException
@@ -25,7 +26,10 @@ except ImportError:
try: # Compiled with SSL?
HTTPSConnection = object
BaseSSLError = None
class BaseSSLError(BaseException):
pass
ssl = None
try: # Python 3
@@ -80,33 +84,38 @@ 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,
assert_hostname=None, assert_fingerprint=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
self.assert_hostname = assert_hostname
self.assert_fingerprint = assert_fingerprint
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)
if self.ca_certs:
match_hostname(self.sock.getpeercert(), self.host)
ssl_version=resolved_ssl_version)
if resolved_cert_reqs != ssl.CERT_NONE:
if self.assert_fingerprint:
assert_fingerprint(self.sock.getpeercert(binary_form=True),
self.assert_fingerprint)
elif self.assert_hostname is not False:
match_hostname(self.sock.getpeercert(),
self.assert_hostname or self.host)
## Pool objects
@@ -146,8 +155,8 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
:class:`httplib.HTTPConnection`.
:param timeout:
Socket timeout for each individual connection, can be a float. None
disables timeout.
Socket timeout in seconds for each individual connection, can be
a float. None disables timeout.
:param maxsize:
Number of connections to save that can be reused. More than 1 is useful
@@ -370,6 +379,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
:param timeout:
If specified, overrides the default timeout for this one request.
It may be a float (in seconds).
:param pool_timeout:
If set and the pool is set to block=True, then this method will
@@ -404,10 +414,6 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
# Check host
if assert_same_host and not self.is_same_host(url):
host = "%s://%s" % (self.scheme, self.host)
if self.port:
host = "%s:%d" % (host, self.port)
raise HostChangedError(self, url, retries - 1)
conn = None
@@ -440,12 +446,14 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods):
except Empty as e:
# Timed out by queue
raise TimeoutError(self, "Request timed out. (pool_timeout=%s)" %
raise TimeoutError(self, url,
"Request timed out. (pool_timeout=%s)" %
pool_timeout)
except SocketTimeout as e:
# Timed out by socket
raise TimeoutError(self, "Request timed out. (timeout=%s)" %
raise TimeoutError(self, url,
"Request timed out. (timeout=%s)" %
timeout)
except BaseSSLError as e:
@@ -503,9 +511,14 @@ class HTTPSConnectionPool(HTTPConnectionPool):
:class:`.VerifiedHTTPSConnection` is used, which *can* verify certificates,
instead of :class:`httplib.HTTPSConnection`.
The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs``, and ``ssl_version``
are only used if :mod:`ssl` is available and are fed into
:meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket into an SSL socket.
:class:`.VerifiedHTTPSConnection` uses one of ``assert_fingerprint``,
``assert_hostname`` and ``host`` in this order to verify connections.
If ``assert_hostname`` is False, no verification is done.
The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs`` and
``ssl_version`` are only used if :mod:`ssl` is available and are fed into
:meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket
into an SSL socket.
"""
scheme = 'https'
@@ -513,8 +526,9 @@ class HTTPSConnectionPool(HTTPConnectionPool):
def __init__(self, host, port=None,
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):
key_file=None, cert_file=None, cert_reqs=None,
ca_certs=None, ssl_version=None,
assert_hostname=None, assert_fingerprint=None):
HTTPConnectionPool.__init__(self, host, port,
strict, timeout, maxsize,
@@ -524,6 +538,8 @@ class HTTPSConnectionPool(HTTPConnectionPool):
self.cert_reqs = cert_reqs
self.ca_certs = ca_certs
self.ssl_version = ssl_version
self.assert_hostname = assert_hostname
self.assert_fingerprint = assert_fingerprint
def _new_conn(self):
"""
@@ -533,7 +549,7 @@ class HTTPSConnectionPool(HTTPConnectionPool):
log.info("Starting new HTTPS connection (%d): %s"
% (self.num_connections, self.host))
if not ssl: # Platform-specific: Python compiled without +ssl
if not ssl: # Platform-specific: Python compiled without +ssl
if not HTTPSConnection or HTTPSConnection is object:
raise SSLError("Can't connect to HTTPS URL because the SSL "
"module is not available.")
@@ -546,12 +562,11 @@ class HTTPSConnectionPool(HTTPConnectionPool):
port=self.port,
strict=self.strict)
connection.set_cert(key_file=self.key_file, cert_file=self.cert_file,
cert_reqs=self.cert_reqs, ca_certs=self.ca_certs)
cert_reqs=self.cert_reqs, ca_certs=self.ca_certs,
assert_hostname=self.assert_hostname,
assert_fingerprint=self.assert_fingerprint)
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

View File

@@ -1,5 +1,5 @@
# urllib3/contrib/ntlmpool.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -33,7 +33,7 @@ class NTLMConnectionPool(HTTPSConnectionPool):
def __init__(self, user, pw, authurl, *args, **kwargs):
"""
authurl is a random URL on the server that is protected by NTLM.
user is the Windows user, probably in the DOMAIN\username format.
user is the Windows user, probably in the DOMAIN\\username format.
pw is the password for the user.
"""
super(NTLMConnectionPool, self).__init__(*args, **kwargs)

View File

@@ -0,0 +1,173 @@
'''SSL with SNI-support for Python 2.
This needs the following packages installed:
* pyOpenSSL (tested with 0.13)
* ndg-httpsclient (tested with 0.3.2)
* pyasn1 (tested with 0.1.6)
To activate it call :func:`~urllib3.contrib.pyopenssl.inject_into_urllib3`.
This can be done in a ``sitecustomize`` module, or at any other time before
your application begins using ``urllib3``, like this::
try:
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
except ImportError:
pass
Now you can use :mod:`urllib3` as you normally would, and it will support SNI
when the required modules are installed.
'''
from ndg.httpsclient.ssl_peer_verification import (ServerSSLCertVerification,
SUBJ_ALT_NAME_SUPPORT)
from ndg.httpsclient.subj_alt_name import SubjectAltName
import OpenSSL.SSL
from pyasn1.codec.der import decoder as der_decoder
from socket import _fileobject
import ssl
from .. import connectionpool
from .. import util
__all__ = ['inject_into_urllib3', 'extract_from_urllib3']
# SNI only *really* works if we can read the subjectAltName of certificates.
HAS_SNI = SUBJ_ALT_NAME_SUPPORT
# Map from urllib3 to PyOpenSSL compatible parameter-values.
_openssl_versions = {
ssl.PROTOCOL_SSLv23: OpenSSL.SSL.SSLv23_METHOD,
ssl.PROTOCOL_SSLv3: OpenSSL.SSL.SSLv3_METHOD,
ssl.PROTOCOL_TLSv1: OpenSSL.SSL.TLSv1_METHOD,
}
_openssl_verify = {
ssl.CERT_NONE: OpenSSL.SSL.VERIFY_NONE,
ssl.CERT_OPTIONAL: OpenSSL.SSL.VERIFY_PEER,
ssl.CERT_REQUIRED: OpenSSL.SSL.VERIFY_PEER
+ OpenSSL.SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
}
orig_util_HAS_SNI = util.HAS_SNI
orig_connectionpool_ssl_wrap_socket = connectionpool.ssl_wrap_socket
def inject_into_urllib3():
'Monkey-patch urllib3 with PyOpenSSL-backed SSL-support.'
connectionpool.ssl_wrap_socket = ssl_wrap_socket
util.HAS_SNI = HAS_SNI
def extract_from_urllib3():
'Undo monkey-patching by :func:`inject_into_urllib3`.'
connectionpool.ssl_wrap_socket = orig_connectionpool_ssl_wrap_socket
util.HAS_SNI = orig_util_HAS_SNI
### Note: This is a slightly bug-fixed version of same from ndg-httpsclient.
def get_subj_alt_name(peer_cert):
# Search through extensions
dns_name = []
if not SUBJ_ALT_NAME_SUPPORT:
return dns_name
general_names = SubjectAltName()
for i in range(peer_cert.get_extension_count()):
ext = peer_cert.get_extension(i)
ext_name = ext.get_short_name()
if ext_name != 'subjectAltName':
continue
# PyOpenSSL returns extension data in ASN.1 encoded form
ext_dat = ext.get_data()
decoded_dat = der_decoder.decode(ext_dat,
asn1Spec=general_names)
for name in decoded_dat:
if not isinstance(name, SubjectAltName):
continue
for entry in range(len(name)):
component = name.getComponentByPosition(entry)
if component.getName() != 'dNSName':
continue
dns_name.append(str(component.getComponent()))
return dns_name
class WrappedSocket(object):
'''API-compatibility wrapper for Python OpenSSL's Connection-class.'''
def __init__(self, connection, socket):
self.connection = connection
self.socket = socket
def fileno(self):
return self.socket.fileno()
def makefile(self, mode, bufsize=-1):
return _fileobject(self.connection, mode, bufsize)
def settimeout(self, timeout):
return self.socket.settimeout(timeout)
def sendall(self, data):
return self.connection.sendall(data)
def close(self):
return self.connection.shutdown()
def getpeercert(self, binary_form=False):
x509 = self.connection.get_peer_certificate()
if not x509:
raise ssl.SSLError('')
if binary_form:
return OpenSSL.crypto.dump_certificate(
OpenSSL.crypto.FILETYPE_ASN1,
x509)
return {
'subject': (
(('commonName', x509.get_subject().CN),),
),
'subjectAltName': [
('DNS', value)
for value in get_subj_alt_name(x509)
]
}
def _verify_callback(cnx, x509, err_no, err_depth, return_code):
return err_no == 0
def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,
ca_certs=None, server_hostname=None,
ssl_version=None):
ctx = OpenSSL.SSL.Context(_openssl_versions[ssl_version])
if certfile:
ctx.use_certificate_file(certfile)
if keyfile:
ctx.use_privatekey_file(keyfile)
if cert_reqs != ssl.CERT_NONE:
ctx.set_verify(_openssl_verify[cert_reqs], _verify_callback)
if ca_certs:
try:
ctx.load_verify_locations(ca_certs, None)
except OpenSSL.SSL.Error as e:
raise ssl.SSLError('bad ca_certs: %r' % ca_certs, e)
cnx = OpenSSL.SSL.Connection(ctx, sock)
cnx.set_tlsext_host_name(server_hostname)
cnx.set_connect_state()
try:
cnx.do_handshake()
except OpenSSL.SSL.Error as e:
raise ssl.SSLError('bad handshake', e)
return WrappedSocket(cnx, sock)

View File

@@ -1,5 +1,5 @@
# urllib3/exceptions.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -20,7 +20,18 @@ class PoolError(HTTPError):
def __reduce__(self):
# For pickling purposes.
return self.__class__, (None, self.url)
return self.__class__, (None, None)
class RequestError(PoolError):
"Base exception for PoolErrors that have associated URLs."
def __init__(self, pool, url, message):
self.url = url
PoolError.__init__(self, pool, message)
def __reduce__(self):
# For pickling purposes.
return self.__class__, (None, self.url, None)
class SSLError(HTTPError):
@@ -35,7 +46,7 @@ class DecodeError(HTTPError):
## Leaf Exceptions
class MaxRetryError(PoolError):
class MaxRetryError(RequestError):
"Raised when the maximum number of retries is exceeded."
def __init__(self, pool, url, reason=None):
@@ -47,22 +58,19 @@ class MaxRetryError(PoolError):
else:
message += " (Caused by redirect)"
PoolError.__init__(self, pool, message)
self.url = url
RequestError.__init__(self, pool, url, message)
class HostChangedError(PoolError):
class HostChangedError(RequestError):
"Raised when an existing pool gets a request for a foreign host."
def __init__(self, pool, url, retries=3):
message = "Tried to open a foreign host with url: %s" % url
PoolError.__init__(self, pool, message)
self.url = url
RequestError.__init__(self, pool, url, message)
self.retries = retries
class TimeoutError(PoolError):
class TimeoutError(RequestError):
"Raised when a socket timeout occurs."
pass

View File

@@ -1,5 +1,5 @@
# urllib3/filepost.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -93,6 +93,6 @@ def encode_multipart_formdata(fields, boundary=None):
body.write(b('--%s--\r\n' % (boundary)))
content_type = b('multipart/form-data; boundary=%s' % boundary)
content_type = str('multipart/form-data; boundary=%s' % boundary)
return body.getvalue(), content_type

View File

@@ -1,11 +1,16 @@
# urllib3/poolmanager.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import logging
try: # Python 3
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin
from ._collections import RecentlyUsedContainer
from .connectionpool import HTTPConnectionPool, HTTPSConnectionPool
from .connectionpool import connection_from_url, port_by_scheme
@@ -23,6 +28,9 @@ pool_classes_by_scheme = {
log = logging.getLogger(__name__)
SSL_KEYWORDS = ('key_file', 'cert_file', 'cert_reqs', 'ca_certs',
'ssl_version')
class PoolManager(RequestMethods):
"""
@@ -58,6 +66,23 @@ 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]
kwargs = self.connection_pool_kw
if scheme == 'http':
kwargs = self.connection_pool_kw.copy()
for kw in SSL_KEYWORDS:
kwargs.pop(kw, None)
return pool_cls(host, port, **kwargs)
def clear(self):
"""
Empty our store of pools and direct them all to close.
@@ -74,22 +99,21 @@ 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)
# If the scheme, host, or port doesn't match existing open connections,
# open a new ConnectionPool.
pool = self.pools.get(pool_key)
if pool:
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)
self.pools[pool_key] = pool
with self.pools.lock:
# If the scheme, host, or port doesn't match existing open connections,
# open a new ConnectionPool.
pool = self.pools.get(pool_key)
if pool:
return pool
# Make a fresh ConnectionPool of the desired type
pool = self._new_pool(scheme, host, port)
self.pools[pool_key] = pool
return pool
def connection_from_url(self, url):
@@ -127,25 +151,40 @@ class PoolManager(RequestMethods):
if not redirect_location:
return response
# Support relative URLs for redirecting.
redirect_location = urljoin(url, redirect_location)
# RFC 2616, Section 10.3.4
if response.status == 303:
method = 'GET'
log.info("Redirecting %s -> %s" % (url, redirect_location))
kw['retries'] = kw.get('retries', 3) - 1 # Persist retries countdown
kw['redirect'] = redirect
return self.urlopen(method, redirect_location, **kw)
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': '*/*'}
netloc = parse_url(url).netloc
if netloc:
headers_['Host'] = netloc
if headers:
headers_.update(headers)
@@ -154,7 +193,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)

View File

@@ -1,5 +1,5 @@
# urllib3/request.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -30,7 +30,7 @@ class RequestMethods(object):
in the URL (such as GET, HEAD, DELETE).
:meth:`.request_encode_body` is for sending requests whose fields are
encoded in the *body* of the request using multipart or www-orm-urlencoded
encoded in the *body* of the request using multipart or www-form-urlencoded
(such as for POST, PUT, PATCH).
:meth:`.request` is for making any kind of request, it will look up the

View File

@@ -1,35 +1,56 @@
# urllib3/response.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import gzip
import logging
import zlib
from io import BytesIO
import io
from .exceptions import DecodeError
from .packages.six import string_types as basestring
from .packages.six import string_types as basestring, binary_type
from .util import is_fp_closed
log = logging.getLogger(__name__)
def decode_gzip(data):
gzipper = gzip.GzipFile(fileobj=BytesIO(data))
return gzipper.read()
class DeflateDecoder(object):
def __init__(self):
self._first_try = True
self._data = binary_type()
self._obj = zlib.decompressobj()
def __getattr__(self, name):
return getattr(self._obj, name)
def decompress(self, data):
if not self._first_try:
return self._obj.decompress(data)
self._data += data
try:
return self._obj.decompress(data)
except zlib.error:
self._first_try = False
self._obj = zlib.decompressobj(-zlib.MAX_WBITS)
try:
return self.decompress(self._data)
finally:
self._data = None
def decode_deflate(data):
try:
return zlib.decompress(data)
except zlib.error:
return zlib.decompress(data, -zlib.MAX_WBITS)
def _get_decoder(mode):
if mode == 'gzip':
return zlib.decompressobj(16 + zlib.MAX_WBITS)
return DeflateDecoder()
class HTTPResponse(object):
class HTTPResponse(io.IOBase):
"""
HTTP Response container.
@@ -52,10 +73,7 @@ class HTTPResponse(object):
otherwise unused.
"""
CONTENT_DECODERS = {
'gzip': decode_gzip,
'deflate': decode_deflate,
}
CONTENT_DECODERS = ['gzip', 'deflate']
def __init__(self, body='', headers=None, status=0, version=0, reason=None,
strict=0, preload_content=True, decode_content=True,
@@ -65,8 +83,9 @@ class HTTPResponse(object):
self.version = version
self.reason = reason
self.strict = strict
self.decode_content = decode_content
self._decode_content = decode_content
self._decoder = None
self._body = body if body and isinstance(body, basestring) else None
self._fp = None
self._original_response = original_response
@@ -115,13 +134,13 @@ class HTTPResponse(object):
parameters: ``decode_content`` and ``cache_content``.
:param amt:
How much of the content to read. If specified, decoding and caching
is skipped because we can't decode partial content nor does it make
sense to cache partial content as the full response.
How much of the content to read. If specified, caching is skipped
because it doesn't make sense to cache partial content as the full
response.
:param decode_content:
If True, will attempt to decode the body based on the
'content-encoding' header. (Overridden if ``amt`` is set.)
'content-encoding' header.
:param cache_content:
If True, will save the returned data such that the same result is
@@ -133,26 +152,48 @@ class HTTPResponse(object):
# Note: content-encoding value should be case-insensitive, per RFC 2616
# Section 3.5
content_encoding = self.headers.get('content-encoding', '').lower()
decoder = self.CONTENT_DECODERS.get(content_encoding)
if self._decoder is None:
if content_encoding in self.CONTENT_DECODERS:
self._decoder = _get_decoder(content_encoding)
if decode_content is None:
decode_content = self._decode_content
decode_content = self.decode_content
if self._fp is None:
return
flush_decoder = False
try:
if amt is None:
# cStringIO doesn't like amt=None
data = self._fp.read()
flush_decoder = True
else:
return self._fp.read(amt)
cache_content = False
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()
flush_decoder = True
try:
if decode_content and decoder:
data = decoder(data)
except (IOError, zlib.error):
raise DecodeError("Received response with content-encoding: %s, but "
"failed to decode it." % content_encoding)
if decode_content and self._decoder:
data = self._decoder.decompress(data)
except (IOError, zlib.error) as e:
raise DecodeError(
"Received response with content-encoding: %s, but "
"failed to decode it." % content_encoding,
e)
if flush_decoder and self._decoder:
buf = self._decoder.decompress(binary_type())
data += buf + self._decoder.flush()
if cache_content:
self._body = data
@@ -163,6 +204,29 @@ class HTTPResponse(object):
if self._original_response and self._original_response.isclosed():
self.release_conn()
def stream(self, amt=2**16, decode_content=None):
"""
A generator wrapper for the read() method. A call will block until
``amt`` bytes have been read from the connection or until the
connection is closed.
:param amt:
How much of the content to read. The generator will return up to
much data per iteration, but may return less. This is particularly
likely when using compressed data. However, the empty string will
never be returned.
:param decode_content:
If True, will attempt to decode the body based on the
'content-encoding' header.
"""
while not is_fp_closed(self._fp):
data = self.read(amt=amt, decode_content=decode_content)
if data:
yield data
@classmethod
def from_httplib(ResponseCls, r, **response_kw):
"""
@@ -202,3 +266,35 @@ class HTTPResponse(object):
def getheader(self, name, default=None):
return self.headers.get(name, default)
# Overrides from io.IOBase
def close(self):
if not self.closed:
self._fp.close()
@property
def closed(self):
if self._fp is None:
return True
elif hasattr(self._fp, 'closed'):
return self._fp.closed
elif hasattr(self._fp, 'isclosed'): # Python 2
return self._fp.isclosed()
else:
return True
def fileno(self):
if self._fp is None:
raise IOError("HTTPResponse has no file to get a fileno from")
elif hasattr(self._fp, "fileno"):
return self._fp.fileno()
else:
raise IOError("The file-like object this HTTPResponse is wrapped "
"around has no file descriptor")
def flush(self):
if self._fp is not None and hasattr(self._fp, 'flush'):
return self._fp.flush()
def readable(self):
return True

View File

@@ -1,5 +1,5 @@
# urllib3/util.py
# Copyright 2008-2012 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
# Copyright 2008-2013 Andrey Petrov and contributors (see CONTRIBUTORS.txt)
#
# This module is part of urllib3 and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
@@ -8,6 +8,8 @@
from base64 import b64encode
from collections import namedtuple
from socket import error as SocketError
from hashlib import md5, sha1
from binascii import hexlify, unhexlify
try:
from select import poll, POLLIN
@@ -22,15 +24,15 @@ try: # Test for SSL features
SSLContext = None
HAS_SNI = False
from ssl import wrap_socket, CERT_NONE, SSLError, PROTOCOL_SSLv23
import ssl
from ssl import wrap_socket, CERT_NONE, PROTOCOL_SSLv23
from ssl import SSLContext # Modern SSL?
from ssl import HAS_SNI # Has SNI?
except ImportError:
pass
from .packages import six
from .exceptions import LocationParseError
from .exceptions import LocationParseError, SSLError
class Url(namedtuple('Url', ['scheme', 'auth', 'host', 'port', 'path', 'query', 'fragment'])):
@@ -58,6 +60,13 @@ class Url(namedtuple('Url', ['scheme', 'auth', 'host', 'port', 'path', 'query',
return uri
@property
def netloc(self):
"""Network location including host and port"""
if self.port:
return '%s:%d' % (self.host, self.port)
return self.host
def split_first(s, delims):
"""
@@ -111,7 +120,7 @@ def parse_url(url):
# While this code has overlap with stdlib's urlparse, it is much
# simplified for our needs and less annoying.
# Additionally, this imeplementations does silly things to be optimal
# Additionally, this implementations does silly things to be optimal
# on CPython.
scheme = None
@@ -140,7 +149,8 @@ def parse_url(url):
# IPv6
if url and url[0] == '[':
host, url = url[1:].split(']', 1)
host, url = url.split(']', 1)
host += ']'
# Port
if ':' in url:
@@ -231,7 +241,7 @@ def make_headers(keep_alive=None, accept_encoding=None, user_agent=None,
return headers
def is_connection_dropped(conn):
def is_connection_dropped(conn): # Platform-specific
"""
Returns True if the connection is dropped and should be closed.
@@ -245,7 +255,7 @@ def is_connection_dropped(conn):
if not sock: # Platform-specific: AppEngine
return False
if not poll: # Platform-specific
if not poll:
if not select: # Platform-specific: AppEngine
return False
@@ -263,10 +273,100 @@ 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
def assert_fingerprint(cert, fingerprint):
"""
Checks if given fingerprint matches the supplied certificate.
:param cert:
Certificate as bytes object.
:param fingerprint:
Fingerprint as string of hexdigits, can be interspersed by colons.
"""
# Maps the length of a digest to a possible hash function producing
# this digest.
hashfunc_map = {
16: md5,
20: sha1
}
fingerprint = fingerprint.replace(':', '').lower()
digest_length, rest = divmod(len(fingerprint), 2)
if rest or digest_length not in hashfunc_map:
raise SSLError('Fingerprint is of invalid length.')
# We need encode() here for py32; works on py2 and p33.
fingerprint_bytes = unhexlify(fingerprint.encode())
hashfunc = hashfunc_map[digest_length]
cert_digest = hashfunc(cert).digest()
if not cert_digest == fingerprint_bytes:
raise SSLError('Fingerprints did not match. Expected "{0}", got "{1}".'
.format(hexlify(fingerprint_bytes),
hexlify(cert_digest)))
def is_fp_closed(obj):
"""
Checks whether a given file-like object is closed.
:param obj:
The file-like object to check.
"""
if hasattr(obj, 'fp'):
# Object is a container for another file-like object that gets released
# on exhaustion (e.g. HTTPResponse)
return obj.fp is None
return obj.closed
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 +379,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 +391,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)

View File

@@ -9,67 +9,75 @@ requests (cookies, auth, proxies).
"""
import os
from collections import Mapping
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 .utils import from_key_val_list, default_headers
from .compat import cookielib, OrderedDict, urljoin, urlparse
from .cookies import cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar
from .models import Request, PreparedRequest
from .hooks import default_hooks, dispatch_hook
from .utils import to_key_val_list, default_headers
from .exceptions import TooManyRedirects, InvalidSchema
from .structures import CaseInsensitiveDict
from .compat import urlparse, urljoin
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
def merge_kwargs(local_kwarg, default_kwarg):
"""Merges kwarg dictionaries.
If a local key in the dictionary is set to None, it will be removed.
def merge_setting(request_setting, session_setting, dict_class=OrderedDict):
"""
Determines appropriate setting for a given request, taking into account the
explicit setting on that request, and the setting in the session. If a
setting is a dictionary, they will be merged together using `dict_class`
"""
if default_kwarg is None:
return local_kwarg
if session_setting is None:
return request_setting
if isinstance(local_kwarg, str):
return local_kwarg
if request_setting is None:
return session_setting
if local_kwarg is None:
return default_kwarg
# Bypass if not a dictionary (e.g. verify)
if not (
isinstance(session_setting, Mapping) and
isinstance(request_setting, Mapping)
):
return request_setting
# Bypass if not a dictionary (e.g. timeout)
if not hasattr(default_kwarg, 'items'):
return local_kwarg
default_kwarg = from_key_val_list(default_kwarg)
local_kwarg = from_key_val_list(local_kwarg)
# Update new values.
kwargs = default_kwarg.copy()
kwargs.update(local_kwarg)
merged_setting = dict_class(to_key_val_list(session_setting))
merged_setting.update(to_key_val_list(request_setting))
# Remove keys that are set to None.
for (k, v) in local_kwarg.items():
for (k, v) in request_setting.items():
if v is None:
del kwargs[k]
del merged_setting[k]
return kwargs
return merged_setting
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
# ((resp.status_code is codes.see_other))
while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):
prepared_request = PreparedRequest()
prepared_request.body = req.body
prepared_request.headers = req.headers.copy()
prepared_request.hooks = req.hooks
resp.content # Consume socket so it can be released
@@ -87,51 +95,81 @@ class SessionRedirectMixin(object):
parsed_rurl = urlparse(resp.url)
url = '%s:%s' % (parsed_rurl.scheme, url)
# The scheme should be lower case...
if '://' in url:
scheme, uri = url.split('://', 1)
url = '%s://%s' % (scheme.lower(), uri)
# Facilitate non-RFC2616-compliant 'location' headers
# (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')
# Compliant with RFC3986, we percent encode the url.
if not urlparse(url).netloc:
# Compliant with RFC3986, we percent encode the url.
url = urljoin(resp.url, requote_uri(url))
else:
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
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
method not in ('GET', 'HEAD')):
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(self.cookies)
resp = self.send(
prepared_request,
stream=stream,
timeout=timeout,
verify=verify,
cert=cert,
proxies=proxies,
allow_redirects=False,
)
extract_cookies_to_jar(self.cookies, prepared_request, resp.raw)
i += 1
yield resp
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):
@@ -140,7 +178,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
@@ -157,28 +195,32 @@ 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.
#: Maximum number of redirects allowed. If the request exceeds this
#: limit, a :class:`TooManyRedirects` exception is raised.
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
#: A CookieJar containing all currently outstanding cookies set on this
#: session. By default it is a
#: :class:`RequestsCookieJar <requests.cookies.RequestsCookieJar>`, but
#: may be any other ``cookielib.CookieJar`` compatible object.
self.cookies = cookiejar_from_dict({})
# Default connection adapters.
self.adapters = {}
self.mount('http://', HTTPAdapter())
self.adapters = OrderedDict()
self.mount('https://', HTTPAdapter())
self.mount('http://', HTTPAdapter())
def __enter__(self):
return self
@@ -186,6 +228,46 @@ class Session(SessionRedirectMixin):
def __exit__(self, *args):
self.close()
def prepare_request(self, request):
"""Constructs a :class:`PreparedRequest <PreparedRequest>` for
transmission and returns it. The :class:`PreparedRequest` has settings
merged from the :class:`Request <Request>` instance and those of the
:class:`Session`.
:param request: :class:`Request` instance to prepare with this
session's settings.
"""
cookies = request.cookies or {}
# Bootstrap CookieJar.
if not isinstance(cookies, cookielib.CookieJar):
cookies = cookiejar_from_dict(cookies)
# Merge with session cookies
merged_cookies = RequestsCookieJar()
merged_cookies.update(self.cookies)
merged_cookies.update(cookies)
# Set environment's basic authentication if not explicitly set.
auth = request.auth
if self.trust_env and not auth and not self.auth:
auth = get_netrc_auth(request.url)
p = PreparedRequest()
p.prepare(
method=request.method.upper(),
url=request.url,
files=request.files,
data=request.data,
headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict),
params=merge_setting(request.params, self.params),
auth=merge_setting(auth, self.auth),
cookies=merged_cookies,
hooks=merge_setting(request.hooks, self.hooks),
)
return p
def request(self, method, url,
params=None,
data=None,
@@ -200,18 +282,51 @@ 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 stream: (optional) whether to immediately download the response
content. Defaults to ``False``.
: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.
"""
# Create the Request.
req = Request(
method = method.upper(),
url = url,
headers = headers,
files = files,
data = data or {},
params = params or {},
auth = auth,
cookies = cookies,
hooks = hooks,
)
prep = self.prepare_request(req)
cookies = cookies or {}
proxies = proxies or {}
# Bootstrap CookieJar.
if not isinstance(cookies, cookielib.CookieJar):
cookies = cookiejar_from_dict(cookies)
# Bubble down session cookies.
for cookie in self.cookies:
cookies.set_cookie(cookie)
# Gather clues from the surrounding environment.
if self.trust_env:
# Set environment's proxies.
@@ -219,10 +334,6 @@ class Session(SessionRedirectMixin):
for (k, v) in env_proxies.items():
proxies.setdefault(k, v)
# Set environment's basic authentication.
if not auth:
auth = get_netrc_auth(url)
# Look for configuration.
if not verify and verify is not False:
verify = os.environ.get('REQUESTS_CA_BUNDLE')
@@ -231,54 +342,22 @@ 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)
auth = merge_kwargs(auth, self.auth)
proxies = merge_kwargs(proxies, self.proxies)
hooks = merge_kwargs(hooks, self.hooks)
stream = merge_kwargs(stream, self.stream)
verify = merge_kwargs(verify, self.verify)
cert = merge_kwargs(cert, self.cert)
# Create the Request.
req = Request()
req.method = method
req.url = url
req.headers = headers
req.files = files
req.data = data
req.params = params
req.auth = auth
req.cookies = cookies
req.hooks = hooks
# Prepare the Request.
prep = req.prepare()
proxies = merge_setting(proxies, self.proxies)
stream = merge_setting(stream, self.stream)
verify = merge_setting(verify, self.verify)
cert = merge_setting(cert, self.cert)
# Send the request.
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
# Persist cookies.
for cookie in resp.cookies:
self.cookies.set_cookie(cookie)
# 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)
send_kwargs = {
'stream': stream,
'timeout': timeout,
'verify': verify,
'cert': cert,
'proxies': proxies,
'allow_redirects': allow_redirects,
}
resp = self.send(prep, **send_kwargs)
return resp
@@ -316,7 +395,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 +405,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 +415,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,23 +432,85 @@ 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 not isinstance(request, PreparedRequest):
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)
# Persist cookies
extract_cookies_to_jar(self.cookies, request, r.raw)
# 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):
"""Returns the appropriate connnection adapter for the given URL."""
for (prefix, adapter) in self.adapters.items():
if url.startswith(prefix):
if url.lower().startswith(prefix):
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."""
"""Registers a connection adapter to a prefix.
Adapters are sorted in descending order by key length."""
self.adapters[prefix] = adapter
keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
for key in keys_to_move:
self.adapters[key] = self.adapters.pop(key)
def __getstate__(self):
return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)
@@ -379,7 +520,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()

View File

@@ -18,7 +18,8 @@ _codes = {
205: ('reset_content', 'reset'),
206: ('partial_content', 'partial'),
207: ('multi_status', 'multiple_status', 'multi_stati', 'multiple_stati'),
208: ('im_used',),
208: ('already_reported',),
226: ('im_used',),
# Redirection.
300: ('multiple_choices',),
@@ -62,6 +63,7 @@ _codes = {
444: ('no_response', 'none'),
449: ('retry_with', 'retry'),
450: ('blocked_by_windows_parental_controls', 'parental_controls'),
451: ('unavailable_for_legal_reasons', 'legal_reasons'),
499: ('client_closed_request',),
# Server Error.

View File

@@ -8,44 +8,105 @@ Data structures that power Requests.
"""
import os
import collections
from itertools import islice
class CaseInsensitiveDict(dict):
"""Case-insensitive Dictionary
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(collections.MutableMapping):
"""
A case-insensitive ``dict``-like object.
Implements all methods and operations of
``collections.MutableMapping`` as well as dict's ``copy``. Also
provides ``lower_items``.
All keys are expected to be strings. The structure remembers the
case of the last key to be set, and ``iter(instance)``,
``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()``
will contain case-sensitive keys. However, querying and contains
testing is case insensitive:
cid = CaseInsensitiveDict()
cid['Accept'] = 'application/json'
cid['aCCEPT'] == 'application/json' # True
list(cid) == ['Accept'] # True
For example, ``headers['content-encoding']`` will return the
value of a ``'Content-Encoding'`` response header."""
value of a ``'Content-Encoding'`` response header, regardless
of how the header name was originally stored.
@property
def lower_keys(self):
if not hasattr(self, '_lower_keys') or not self._lower_keys:
self._lower_keys = dict((k.lower(), k) for k in list(self.keys()))
return self._lower_keys
If the constructor, ``.update``, or equality comparison
operations are given keys that have equal ``.lower()``s, the
behavior is undefined.
def _clear_lower_keys(self):
if hasattr(self, '_lower_keys'):
self._lower_keys.clear()
"""
def __init__(self, data=None, **kwargs):
self._store = dict()
if data is None:
data = {}
self.update(data, **kwargs)
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
self._clear_lower_keys()
def __delitem__(self, key):
dict.__delitem__(self, self.lower_keys.get(key.lower(), key))
self._lower_keys.clear()
def __contains__(self, key):
return key.lower() in self.lower_keys
# Use the lowercased key for lookups, but store the actual
# key alongside the value.
self._store[key.lower()] = (key, value)
def __getitem__(self, key):
# We allow fall-through here, so values default to None
if key in self:
return dict.__getitem__(self, self.lower_keys[key.lower()])
return self._store[key.lower()][1]
def get(self, key, default=None):
if key in self:
return self[key]
def __delitem__(self, key):
del self._store[key.lower()]
def __iter__(self):
return (casedkey for casedkey, mappedvalue in self._store.values())
def __len__(self):
return len(self._store)
def lower_items(self):
"""Like iteritems(), but with all lowercase keys."""
return (
(lowerkey, keyval[1])
for (lowerkey, keyval)
in self._store.items()
)
def __eq__(self, other):
if isinstance(other, collections.Mapping):
other = CaseInsensitiveDict(other)
else:
return default
return NotImplemented
# Compare insensitively
return dict(self.lower_items()) == dict(other.lower_items())
# Copy is required
def copy(self):
return CaseInsensitiveDict(self._store.values())
def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, dict(self.items()))
class LookupDict(dict):

View File

@@ -11,57 +11,26 @@ that are also useful for external consumption.
import cgi
import codecs
import collections
import os
import platform
import re
import sys
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, urlparse, bytes, str, OrderedDict
from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse
from .compat import getproxies, proxy_bypass
from .cookies import RequestsCookieJar, cookiejar_from_dict
from .structures import CaseInsensitiveDict
_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 +42,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 +91,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):
@@ -158,7 +136,7 @@ def to_key_val_list(value):
if isinstance(value, (str, bytes, bool, int)):
raise ValueError('cannot encode objects that are not 2-tuples')
if isinstance(value, dict):
if isinstance(value, collections.Mapping):
value = value.items()
return list(value)
@@ -276,8 +254,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
@@ -325,7 +302,7 @@ def stream_decode_response_unicode(iterator, r):
rv = decoder.decode(chunk)
if rv:
yield rv
rv = decoder.decode('', final=True)
rv = decoder.decode(b'', final=True)
if rv:
yield rv
@@ -371,48 +348,6 @@ def get_unicode_from_response(r):
return r.content
def stream_decompress(iterator, mode='gzip'):
"""Stream decodes an iterator over compressed data
:param iterator: An iterator over compressed data
:param mode: 'gzip' or 'deflate'
:return: An iterator over decompressed data
"""
if mode not in ['gzip', 'deflate']:
raise ValueError('stream_decompress mode must be gzip or deflate')
zlib_mode = 16 + zlib.MAX_WBITS if mode == 'gzip' else -zlib.MAX_WBITS
dec = zlib.decompressobj(zlib_mode)
try:
for chunk in iterator:
rv = dec.decompress(chunk)
if rv:
yield rv
except zlib.error:
# If there was an error decompressing, just return the raw chunk
yield chunk
# Continue to return the rest of the raw data
for chunk in iterator:
yield chunk
else:
# Make sure everything has been returned from the decompression object
buf = dec.decompress(bytes())
rv = buf + dec.flush()
if rv:
yield rv
def stream_untransfer(gen, resp):
ce = resp.headers.get('content-encoding', '').lower()
if 'gzip' in ce:
gen = stream_decompress(gen, mode='gzip')
elif 'deflate' in ce:
gen = stream_decompress(gen, mode='deflate')
return gen
# The unreserved URI characters (RFC 3986)
UNRESERVED_SET = frozenset(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
@@ -452,37 +387,34 @@ def requote_uri(uri):
def get_environ_proxies(url):
"""Return a dict of environment proxies."""
proxy_keys = [
'all',
'http',
'https',
'ftp',
'socks'
]
get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper())
# First check whether no_proxy is defined. If it is, check that the URL
# we're getting isn't in the no_proxy list.
no_proxy = get_proxy('no_proxy')
netloc = urlparse(url).netloc
if no_proxy:
# We need to check whether we match here. We need to see if we match
# the end of the netloc, both with and without the port.
no_proxy = no_proxy.split(',')
netloc = urlparse(url).netloc
for host in no_proxy:
if netloc.endswith(host) or netloc.split(':')[0].endswith(host):
# The URL does match something in no_proxy, so we don't want
# to apply the proxies on this URL.
return {}
# If the system proxy settings indicate that this URL should be bypassed,
# don't proxy.
if proxy_bypass(netloc):
return {}
# If we get here, we either didn't have no_proxy set or we're not going
# anywhere that no_proxy applies to.
proxies = [(key, get_proxy(key + '_proxy')) for key in proxy_keys]
return dict([(key, val) for (key, val) in proxies if val])
# anywhere that no_proxy applies to, and the system settings don't require
# bypassing the proxy for the current URL.
return getproxies()
def default_user_agent():
"""Return a string representing the default user agent."""
@@ -491,11 +423,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':
@@ -512,18 +442,17 @@ 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 {
return CaseInsensitiveDict({
'User-Agent': default_user_agent(),
'Accept-Encoding': ', '.join(('gzip', 'deflate', 'compress')),
'Accept': '*/*'
}
})
def parse_header_links(value):
@@ -549,7 +478,7 @@ def parse_header_links(value):
for param in params.split(";"):
try:
key,value = param.split("=")
key, value = param.split("=")
except ValueError:
break
@@ -593,3 +522,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 ('', '')

View File

@@ -1,2 +1,3 @@
pytest
sphinx
py==1.4.12
pytest==2.3.4
invoke==0.2.0

View File

@@ -17,9 +17,10 @@ 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.contrib',
'requests.packages.urllib3.packages.ssl_match_hostname'
]
@@ -50,9 +51,7 @@ setup(
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
# 'Programming Language :: Python :: 3.0',
'Programming Language :: Python :: 3.1',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
),
)

32
tasks.py Normal file
View 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)

558
test_requests.py Normal file → Executable file
View File

@@ -3,18 +3,34 @@
"""Tests for Requests."""
from __future__ import division
import json
import os
import unittest
import pickle
import requests
from requests.auth import HTTPDigestAuth
from requests.adapters import HTTPAdapter
from requests.compat import str, cookielib, getproxies, urljoin, urlparse
from requests.cookies import cookiejar_from_dict
from requests.exceptions import InvalidURL, MissingSchema
from requests.structures import CaseInsensitiveDict
try:
import StringIO
except ImportError:
import io as StringIO
HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')
# Issue #1483: Make sure the URL always has a trailing slash
HTTPBIN = HTTPBIN.rstrip('/') + '/'
def httpbin(*suffix):
"""Returns url for HTTPBIN resource."""
return HTTPBIN + '/'.join(suffix)
return urljoin(HTTPBIN, '/'.join(suffix))
class RequestsTestCase(unittest.TestCase):
@@ -28,9 +44,6 @@ class RequestsTestCase(unittest.TestCase):
"""Teardown."""
pass
def test_assertion(self):
assert 1
def test_entry_points(self):
requests.session
@@ -43,7 +56,8 @@ class RequestsTestCase(unittest.TestCase):
requests.post
def test_invalid_url(self):
self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')
self.assertRaises(MissingSchema, requests.get, 'hiwpefhipowhefopw')
self.assertRaises(InvalidURL, requests.get, 'http://')
def test_basic_building(self):
req = requests.Request()
@@ -54,6 +68,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()
@@ -70,13 +89,23 @@ 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_mixed_case_scheme_acceptable(self):
s = requests.Session()
s.proxies = getproxies()
parts = urlparse(httpbin('get'))
schemes = ['http://', 'HTTP://', 'hTTp://', 'HttP://',
'https://', 'HTTPS://', 'hTTps://', 'HttPs://']
for scheme in schemes:
url = scheme + parts.netloc + parts.path
r = requests.Request('GET', url)
r = s.send(r.prepare())
self.assertEqual(r.status_code, 200,
"failed for scheme %s" % scheme)
def test_HTTP_200_OK_GET_ALTERNATIVE(self):
r = requests.Request('GET', httpbin('get'))
s = requests.Session()
s.proxies = getproxies()
r = s.send(r.prepare())
@@ -86,6 +115,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'}
@@ -100,19 +133,72 @@ 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_cookie_removed_on_expire(self):
s = requests.session()
s.get(httpbin('cookies/set?foo=bar'))
self.assertTrue(s.cookies['foo'] == 'bar')
s.get(
httpbin('response-headers'),
params={
'Set-Cookie':
'foo=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT'
}
)
assert 'foo' not in s.cookies
def test_cookie_quote_wrapped(self):
s = requests.session()
s.get(httpbin('cookies/set?foo="bar:baz"'))
self.assertTrue(s.cookies['foo'] == '"bar:baz"')
def test_request_cookie_overrides_session_cookie(self):
s = requests.session()
s.cookies['foo'] = 'bar'
r = s.get(httpbin('cookies'), cookies={'foo': 'baz'})
assert r.json()['cookies']['foo'] == 'baz'
# Session cookie should not be modified
assert s.cookies['foo'] == 'bar'
def test_generic_cookiejar_works(self):
cj = cookielib.CookieJar()
cookiejar_from_dict({'foo': 'bar'}, cj)
s = requests.session()
s.cookies = cj
r = s.get(httpbin('cookies'))
# Make sure the cookie was sent
assert r.json()['cookies']['foo'] == 'bar'
# Make sure the session cj is still the custom one
assert s.cookies is cj
def test_requests_in_history_are_not_overridden(self):
resp = requests.get(httpbin('redirect/3'))
urls = [r.url for r in resp.history]
req_urls = [r.request.url for r in resp.history]
self.assertEquals(urls, req_urls)
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)
@@ -127,8 +213,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')
@@ -143,6 +227,34 @@ class RequestsTestCase(unittest.TestCase):
r = s.get(url)
self.assertEqual(r.status_code, 200)
def test_basicauth_with_netrc(self):
auth = ('user', 'pass')
wrong_auth = ('wronguser', 'wrongpass')
url = httpbin('basic-auth', 'user', 'pass')
def get_netrc_auth_mock(url):
return auth
requests.sessions.get_netrc_auth = get_netrc_auth_mock
# Should use netrc and work.
r = requests.get(url)
self.assertEqual(r.status_code, 200)
# Given auth should override and fail.
r = requests.get(url, auth=wrong_auth)
self.assertEqual(r.status_code, 401)
s = requests.session()
# Should use netrc and work.
r = s.get(url)
self.assertEqual(r.status_code, 200)
# Given auth should override and fail.
s.auth = wrong_auth
r = s.get(url)
self.assertEqual(r.status_code, 401)
def test_DIGEST_HTTP_200_OK_GET(self):
auth = HTTPDigestAuth('user', 'pass')
@@ -159,6 +271,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')
@@ -238,6 +362,9 @@ 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']))
@@ -251,6 +378,409 @@ class RequestsTestCase(unittest.TestCase):
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
def test_unicode_multipart_post(self):
r = requests.post(httpbin('post'),
data={'stuff': u'ëlïxr'},
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
r = requests.post(httpbin('post'),
data={'stuff': u'ëlïxr'.encode('utf-8')},
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
r = requests.post(httpbin('post'),
data={'stuff': 'elixr'},
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
r = requests.post(httpbin('post'),
data={'stuff': 'elixr'.encode('utf-8')},
files={'file': ('test_requests.py', open(__file__, 'rb'))})
self.assertEqual(r.status_code, 200)
def test_unicode_multipart_post_fieldnames(self):
filename = os.path.splitext(__file__)[0] + '.py'
r = requests.Request(method='POST',
url=httpbin('post'),
data={'stuff'.encode('utf-8'): 'elixr'},
files={'file': ('test_requests.py',
open(filename, 'rb'))})
prep = r.prepare()
self.assertTrue(b'name="stuff"' in prep.body)
self.assertFalse(b'name="b\'stuff\'"' in prep.body)
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()
s.proxies = getproxies()
resp = s.send(prep)
self.assertTrue(hasattr(resp, 'hook_working'))
def test_prepared_from_session(self):
class DummyAuth(requests.auth.AuthBase):
def __call__(self, r):
r.headers['Dummy-Auth-Test'] = 'dummy-auth-test-ok'
return r
req = requests.Request('GET', httpbin('headers'))
self.assertEqual(req.auth, None)
s = requests.Session()
s.auth = DummyAuth()
prep = s.prepare_request(req)
resp = s.send(prep)
self.assertTrue(resp.json()['headers']['Dummy-Auth-Test'], 'dummy-auth-test-ok')
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')
read_ = io.read
def read_mock(amt, decode_content=None):
return read_(amt)
setattr(io, 'read', read_mock)
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))
s.proxies = getproxies()
r = s.send(r.prepare())
self.assertEqual(r.status_code, 200)
def test_fixes_1329(self):
"""
Ensure that header updates are done case-insensitively.
"""
s = requests.Session()
s.headers.update({'ACCEPT': 'BOGUS'})
s.headers.update({'accept': 'application/json'})
r = s.get(httpbin('get'))
headers = r.request.headers
# ASCII encode because of key comparison changes in py3
self.assertEqual(
headers['accept'.encode('ascii')],
'application/json'
)
self.assertEqual(
headers['Accept'.encode('ascii')],
'application/json'
)
self.assertEqual(
headers['ACCEPT'.encode('ascii')],
'application/json'
)
def test_uppercase_scheme_redirect(self):
parts = urlparse(httpbin('html'))
url = "HTTP://" + parts.netloc + parts.path
r = requests.get(httpbin('redirect-to'), params={'url': url})
self.assertEqual(r.status_code, 200)
self.assertEqual(r.url.lower(), url.lower())
def test_transport_adapter_ordering(self):
s = requests.Session()
order = ['https://', 'http://']
self.assertEqual(order, list(s.adapters))
s.mount('http://git', HTTPAdapter())
s.mount('http://github', HTTPAdapter())
s.mount('http://github.com', HTTPAdapter())
s.mount('http://github.com/about/', HTTPAdapter())
order = [
'http://github.com/about/',
'http://github.com',
'http://github',
'http://git',
'https://',
'http://',
]
self.assertEqual(order, list(s.adapters))
s.mount('http://gittip', HTTPAdapter())
s.mount('http://gittip.com', HTTPAdapter())
s.mount('http://gittip.com/about/', HTTPAdapter())
order = [
'http://github.com/about/',
'http://gittip.com/about/',
'http://github.com',
'http://gittip.com',
'http://github',
'http://gittip',
'http://git',
'https://',
'http://',
]
self.assertEqual(order, list(s.adapters))
s2 = requests.Session()
s2.adapters = {'http://': HTTPAdapter()}
s2.mount('https://', HTTPAdapter())
self.assertTrue('http://' in s2.adapters)
self.assertTrue('https://' in s2.adapters)
def test_header_remove_is_case_insensitive(self):
# From issue #1321
s = requests.Session()
s.headers['foo'] = 'bar'
r = s.get(httpbin('get'), headers={'FOO': None})
assert 'foo' not in r.request.headers
def test_params_are_merged_case_sensitive(self):
s = requests.Session()
s.params['foo'] = 'bar'
r = s.get(httpbin('get'), params={'FOO': 'bar'})
assert r.json()['args'] == {'foo': 'bar', 'FOO': 'bar'}
def test_long_authinfo_in_url(self):
url = 'http://{0}:{1}@{2}:9000/path?query#frag'.format(
'E8A3BE87-9E3F-4620-8858-95478E385B5B',
'EA770032-DA4D-4D84-8CE9-29C6D910BF1E',
'exactly-------------sixty-----------three------------characters',
)
r = requests.Request('GET', url).prepare()
self.assertEqual(r.url, url)
class TestCaseInsensitiveDict(unittest.TestCase):
def test_mapping_init(self):
cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
self.assertEqual(len(cid), 2)
self.assertTrue('foo' in cid)
self.assertTrue('bar' in cid)
def test_iterable_init(self):
cid = CaseInsensitiveDict([('Foo', 'foo'), ('BAr', 'bar')])
self.assertEqual(len(cid), 2)
self.assertTrue('foo' in cid)
self.assertTrue('bar' in cid)
def test_kwargs_init(self):
cid = CaseInsensitiveDict(FOO='foo', BAr='bar')
self.assertEqual(len(cid), 2)
self.assertTrue('foo' in cid)
self.assertTrue('bar' in cid)
def test_docstring_example(self):
cid = CaseInsensitiveDict()
cid['Accept'] = 'application/json'
self.assertEqual(cid['aCCEPT'], 'application/json')
self.assertEqual(list(cid), ['Accept'])
def test_len(self):
cid = CaseInsensitiveDict({'a': 'a', 'b': 'b'})
cid['A'] = 'a'
self.assertEqual(len(cid), 2)
def test_getitem(self):
cid = CaseInsensitiveDict({'Spam': 'blueval'})
self.assertEqual(cid['spam'], 'blueval')
self.assertEqual(cid['SPAM'], 'blueval')
def test_fixes_649(self):
"""__setitem__ should behave case-insensitively."""
cid = CaseInsensitiveDict()
cid['spam'] = 'oneval'
cid['Spam'] = 'twoval'
cid['sPAM'] = 'redval'
cid['SPAM'] = 'blueval'
self.assertEqual(cid['spam'], 'blueval')
self.assertEqual(cid['SPAM'], 'blueval')
self.assertEqual(list(cid.keys()), ['SPAM'])
def test_delitem(self):
cid = CaseInsensitiveDict()
cid['Spam'] = 'someval'
del cid['sPam']
self.assertFalse('spam' in cid)
self.assertEqual(len(cid), 0)
def test_contains(self):
cid = CaseInsensitiveDict()
cid['Spam'] = 'someval'
self.assertTrue('Spam' in cid)
self.assertTrue('spam' in cid)
self.assertTrue('SPAM' in cid)
self.assertTrue('sPam' in cid)
self.assertFalse('notspam' in cid)
def test_get(self):
cid = CaseInsensitiveDict()
cid['spam'] = 'oneval'
cid['SPAM'] = 'blueval'
self.assertEqual(cid.get('spam'), 'blueval')
self.assertEqual(cid.get('SPAM'), 'blueval')
self.assertEqual(cid.get('sPam'), 'blueval')
self.assertEqual(cid.get('notspam', 'default'), 'default')
def test_update(self):
cid = CaseInsensitiveDict()
cid['spam'] = 'blueval'
cid.update({'sPam': 'notblueval'})
self.assertEqual(cid['spam'], 'notblueval')
cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
cid.update({'fOO': 'anotherfoo', 'bAR': 'anotherbar'})
self.assertEqual(len(cid), 2)
self.assertEqual(cid['foo'], 'anotherfoo')
self.assertEqual(cid['bar'], 'anotherbar')
def test_update_retains_unchanged(self):
cid = CaseInsensitiveDict({'foo': 'foo', 'bar': 'bar'})
cid.update({'foo': 'newfoo'})
self.assertEquals(cid['bar'], 'bar')
def test_iter(self):
cid = CaseInsensitiveDict({'Spam': 'spam', 'Eggs': 'eggs'})
keys = frozenset(['Spam', 'Eggs'])
self.assertEqual(frozenset(iter(cid)), keys)
def test_equality(self):
cid = CaseInsensitiveDict({'SPAM': 'blueval', 'Eggs': 'redval'})
othercid = CaseInsensitiveDict({'spam': 'blueval', 'eggs': 'redval'})
self.assertEqual(cid, othercid)
del othercid['spam']
self.assertNotEqual(cid, othercid)
self.assertEqual(cid, {'spam': 'blueval', 'eggs': 'redval'})
def test_setdefault(self):
cid = CaseInsensitiveDict({'Spam': 'blueval'})
self.assertEqual(
cid.setdefault('spam', 'notblueval'),
'blueval'
)
self.assertEqual(
cid.setdefault('notspam', 'notblueval'),
'notblueval'
)
def test_lower_items(self):
cid = CaseInsensitiveDict({
'Accept': 'application/json',
'user-Agent': 'requests',
})
keyset = frozenset(lowerkey for lowerkey, v in cid.lower_items())
lowerkeyset = frozenset(['accept', 'user-agent'])
self.assertEqual(keyset, lowerkeyset)
def test_preserve_key_case(self):
cid = CaseInsensitiveDict({
'Accept': 'application/json',
'user-Agent': 'requests',
})
keyset = frozenset(['Accept', 'user-Agent'])
self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
self.assertEqual(frozenset(cid.keys()), keyset)
self.assertEqual(frozenset(cid), keyset)
def test_preserve_last_key_case(self):
cid = CaseInsensitiveDict({
'Accept': 'application/json',
'user-Agent': 'requests',
})
cid.update({'ACCEPT': 'application/json'})
cid['USER-AGENT'] = 'requests'
keyset = frozenset(['ACCEPT', 'USER-AGENT'])
self.assertEqual(frozenset(i[0] for i in cid.items()), keyset)
self.assertEqual(frozenset(cid.keys()), keyset)
self.assertEqual(frozenset(cid), keyset)
if __name__ == '__main__':