Skip to content
Myr

Merge branch 'develop'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kenneth Reitz · committed by Kenneth Reitz · parents 4f6fc98, b37b977 · Browse files · 9 files+186 added−85 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+186 added−85 removed

  • other:+186 added−85 removed

9 files+186 added−85 removed

added:docs/community/out-there.rst+63 added
1Modules2=======34- `requests-oauth-hook <https://github.com/maraujop/requests-oauth-hook>`_, adds OAuth support to Requests.5- `FacePy <https://github.com/jgorset/facepy>`_, a Python wrapper to the Facebook API.6- `robotframework-requests <https://github.com/bulkan/robotframework-requests>`_, a Robot Framework API wrapper.7- `fullerene <https://github.com/bitprophet/fullerene>`_, a Graphite Dashboard.8- `urbanairship-python <https://github.com/benjaminws/urbanairship-python>`_, a fork of the Urban Airship API wrapper.91011Articles & Talks12================13- `Python for the Web <http://gun.io/blog/python-for-the-web/>`_ teaches how to use Python to interact with the web, using Requests.14- `Daniel Greenfield's Review of Requests <http://pydanny.blogspot.com/2011/05/python-http-requests-for-humans.html>`_15- `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>`_ )16- `Issac Kelly's 'Consuming Web APIs' talk <http://issackelly.github.com/Consuming-Web-APIs-with-Python-Talk/slides/slides.html>`_17- `Blog post about Requests via Yum <http://arunsag.wordpress.com/2011/08/17/new-package-python-requests-http-for-humans/>`_18- `Russian blog post introducing Requests <http://habrahabr.ru/blogs/python/126262/>`_192021Integrations22============2324ScraperWiki25------------2627`ScraperWiki <https://scraperwiki.com/>`_ is an excellent service that allows28you to run Python, Ruby, and PHP scraper scripts on the web. Now, Requests29v0.6.1 is available to use in your scrapers!3031To give it a try, simply::3233 import requests343536Managed Packages37================3839Requests is available in a number of popular package formats. Of course,40the ideal way to install Requests is via The Cheeseshop.414243Ubuntu & Debian44---------------4546Requests is available installed as a Debian package! Debian Etch Ubuntu, since Oneiric::4748 $ apt-get install python-requests4950Unfortunately, the most recent version available is v0.5.0. If you're on the51Debian Python Package team, I'd love an update of that :)525354Fedora and RedHat55-----------------5657You can easily install Requests v0.6.1 with yum on rpm-based systems::5859 $ yum install python-requests60616263
modified:docs/index.rst+1 added
85 unmodified lines
86.. toctree::87 :maxdepth: 28889 community/faq90 community/out-there.rst91 community/support92 community/updates9394API Documentation
23 unmodified lines
modified:requests/core.py+2 added-2 removed
10 unmodified lines
1112"""1314__title__ = 'requests'15__version__ = '0.6.4'16__build__ = 0x00060415__version__ = '0.6.5'16__build__ = 0x00060517__author__ = 'Kenneth Reitz'18__license__ = 'ISC'19__copyright__ = 'Copyright 2011 Kenneth Reitz'20
9 unmodified lines
modified:requests/sessions.py+65 added-16 removed
14 unmodified lines
15from .utils import add_dict_to_cookiejar16171819def merge_kwargs(local_kwarg, default_kwarg):20 """Merges kwarg dictionaries.2122 If a local key in the dictionary is set to None, it will be removed.23 """2425 if default_kwarg is None:26 return local_kwarg2728 if local_kwarg is None:29 return default_kwarg3031 # Bypass if not a dictionary (e.g. timeout)32 if not hasattr(default_kwarg, 'items'):33 return local_kwarg34353637 # Update new values.38 kwargs = default_kwarg.copy()39 kwargs.update(local_kwarg)4041 # Remove keys that are set to None.42 for (k,v) in local_kwarg.items():43 if v is None:44 del kwargs[k]4546 return kwargs474849class Session(object):50 """A Requests session."""5152 __attrs__ = ['headers', 'cookies', 'auth', 'timeout', 'proxies', 'hooks']535425 def __init__(self, **kwargs):55 def __init__(self,56 headers=None,57 cookies=None,58 auth=None,59 timeout=None,60 proxies=None,61 hooks=None):6263 self.headers = headers or {}64 self.cookies = cookies or {}65 self.auth = auth66 self.timeout = timeout67 self.proxies = proxies or {}68 self.hooks = hooks or {}6970 # Set up a CookieJar to be used by default71 self.cookies = cookielib.FileCookieJar()7230 # Map args from kwargs to instance-local variables31 map(lambda k, v: (k in self.__attrs__) and setattr(self, k, v),32 kwargs.iterkeys(), kwargs.itervalues())3373 # Map and wrap requests.api methods74 self._map_api_methods()7576
3 unmodified lines
80 def __enter__(self):81 return self8283 def __exit__(self, *args):45 # print args84 pass854886 def _map_api_methods(self):87 """Reads each available method from requests.api and decorates88 them with a wrapper, which inserts any instance-local attributes89 (from __attrs__) that have been set, combining them with **kwargs.90 """9192 def pass_args(func):93 def wrapper_func(*args, **kwargs):57 inst_attrs = dict((k, v) for k, v in self.__dict__.iteritems()58 if k in self.__attrs__)59 # Combine instance-local values with kwargs values, with60 # priority to values in kwargs61 kwargs = dict(inst_attrs.items() + kwargs.items())9495 # Argument collector.96 _kwargs = {}9798 # If a session request has a cookie_dict, inject the99 # values into the existing CookieJar instead.100 if isinstance(kwargs.get('cookies', None), dict):101 kwargs['cookies'] = add_dict_to_cookiejar(67 inst_attrs['cookies'], kwargs['cookies']102 self.cookies, kwargs['cookies']103 )10470 if kwargs.get('headers', None) and inst_attrs.get('headers', None):71 kwargs['headers'].update(inst_attrs['headers'])105 for attr in self.__attrs__:106 # for attr in ['headers',]:107 s_val = self.__dict__.get(attr)108 r_val = kwargs.get(attr)109110 new_attr = merge_kwargs(r_val, s_val)111112 # Skip attributes that were set to None.113 if new_attr is not None:114 _kwargs[attr] = new_attr115116 # Make sure we didn't miss anything.117 for (k, v) in kwargs.items():118 if k not in _kwargs:119 _kwargs[k] = v12073 return func(*args, **kwargs)121 return func(*args, **_kwargs)122123 return wrapper_func124125 # Map and decorate each function available in requests.api126 map(lambda fn: setattr(self, fn, pass_args(getattr(api, fn))),
7 unmodified lines
modified:.gitignore+1 added
5 unmodified lines
6*.pyc7docs/_build8toy.py9.gitignore10junit-report.xml
modified:HISTORY.rst+7 added
1History2-------340.6.5 (2011-10-19)5++++++++++++++++++67* Offline (fast) test suite.8* Session dictionary argument merging.910110.6.4 (2011-10-13)12++++++++++++++++++1314* Automatic decoding of unicode, based on HTTP Headers.
174 unmodified lines
modified:Makefile+4 added-2 removed
1init:2 pip install -r reqs.txt34test:5 nosetests test_requests.py --processes=305 nosetests test_requests.py --with-color67ci: init8 nosetests --search-test --processes=30 --with-nosexunit test_requests.py8 nosetests test_requests.py --with-xunit --xunit-file=junit-report.xml910stats:11 pyflakes requests | awk -F\: '{printf "%s:%s: [E]%s\n", $1, $2, $3}' > violations.pyflakes.txt1213site:14 cd docs; make dirhtml
2 unmodified lines
added:reqs.txt+7 added
1envoy==0.0.22httpbin==0.0.43gunicorn4nose5pyflakes6omnijson7rudolf
modified:test_requests.py+36 added-65 removed
1 unmodified line
2# -*- coding: utf-8 -*-34from __future__ import with_statement56import unittest6import time7import cookielib8import os9import unittest1011import envoy1213try:14 import omnijson as json15except ImportError:16 import json
1 unmodified line
18import requests1920from requests.sessions import Session2122PORT = os.environ.get('HTTPBIN_PORT', '7045')2319HTTPBIN_URL = 'http://httpbin.ep.io/'20HTTPSBIN_URL = 'https://httpbin.ep.io/'24HTTPBIN_URL = 'http://0.0.0.0:%s/' % (PORT)25# HTTPBIN_URL = 'http://127.0.0.1:8000/'2622# HTTPBIN_URL = 'http://staging.httpbin.org/'23# HTTPSBIN_URL = 'https://httpbin-staging.ep.io/'272528def httpbin(*suffix):29 """Returns url for HTTPBIN resource."""3031 return HTTPBIN_URL + '/'.join(suffix)323332def httpsbin(*suffix):33 """Returns url for HTTPSBIN resource."""34SERVICES = (httpbin, )3535 return HTTPSBIN_URL + '/'.join(suffix)36_httpbin = False373738SERVICES = (httpbin, httpsbin)39404138class RequestsTestSuite(unittest.TestCase):39 """Requests test cases."""4041 # It goes to eleven.42 _multiprocess_can_split_ = True4344 def setUp(self):49 pass4546 global _httpbin4748 if not _httpbin:4950 self.httpbin = envoy.connect('gunicorn httpbin:app --bind=0.0.0.0:%s' % (PORT))5152 _httpbin = True53 time.sleep(1)54555657 def tearDown(self):58 """Teardown."""54 pass59 # self.httpbin.kill()606162 def test_invalid_url(self):63 self.assertRaises(ValueError, requests.get, 'hiwpefhipowhefopw')646566 def test_HTTP_200_OK_GET(self):62 r = requests.get(httpbin('/'))67 r = requests.get(httpbin('/get'))68 self.assertEqual(r.status_code, 200)6970 def test_HTTP_302_ALLOW_REDIRECT_GET(self):71 r = requests.get(httpbin('redirect', '1'))
2 unmodified lines
74 def test_HTTP_302_GET(self):75 r = requests.get(httpbin('redirect', '1'), allow_redirects=False)76 self.assertEqual(r.status_code, 302)7773 def test_HTTPS_200_OK_GET(self):74 r = requests.get(httpsbin('/'))75 self.assertEqual(r.status_code, 200)787779 def test_HTTP_200_OK_GET_WITH_PARAMS(self):80 heads = {'User-agent': 'Mozilla/5.0'}8182 r = requests.get(httpbin('user-agent'), headers=heads)
29 unmodified lines
112 self.assertTrue(heads['user-agent'] in r.content)113114115 def test_HTTP_200_OK_HEAD(self):115 r = requests.head(httpbin('/'))116 r = requests.head(httpbin('/get'))117 self.assertEqual(r.status_code, 200)118119119 def test_HTTPS_200_OK_HEAD(self):120 r = requests.head(httpsbin('/'))121 self.assertEqual(r.status_code, 200)122123120 def test_HTTP_200_OK_PUT(self):121 r = requests.put(httpbin('put'))122 self.assertEqual(r.status_code, 200)123124129 def test_HTTPS_200_OK_PUT(self):130 r = requests.put(httpsbin('put'))131 self.assertEqual(r.status_code, 200)132133125 def test_HTTP_200_OK_PATCH(self):126 r = requests.patch(httpbin('patch'))127 self.assertEqual(r.status_code, 200)128129139 def test_HTTPS_200_OK_PATCH(self):140 r = requests.patch(httpsbin('patch'))141 self.assertEqual(r.status_code, 200)142143130 def test_AUTH_HTTP_200_OK_GET(self):131132 for service in SERVICES:133
57 unmodified lines
191192 r = requests.get(service('status', '500'))193 self.assertEqual(bool(r), False)194209 r = requests.get(service('/'))195 r = requests.get(service('/get'))196 self.assertEqual(bool(r), True)197198199 def test_request_ok_set(self):
42 unmodified lines
242 def test_unicode_get(self):243244 for service in SERVICES:245260 url = service('/')246 url = service('/get')247248 requests.get(url, params={'foo': u'føø'})249 requests.get(url, params={u'føø': u'føø'})250 requests.get(url, params={'føø': 'føø'})
9 unmodified lines
260 r = requests.get(service('basic-auth', 'user', 'pass'), auth=http_auth)261 self.assertEquals(r.status_code, 401)262263278 def test_settings(self):279280 def test():281 r = requests.get(httpbin(''))282 r.raise_for_status()283284 with requests.settings(timeout=0.0000001):285 self.assertRaises(requests.Timeout, test)286287 with requests.settings(timeout=100):288 requests.get(httpbin(''))289290264 def test_urlencoded_post_data(self):265266 for service in SERVICES:267
90 unmodified lines
358 self.assertEquals(rbody.get('form'), None)359 self.assertEquals(rbody.get('data'), 'foobar')360361389 def test_idna(self):390 r = requests.get(u'http://➡.ws/httpbin')391 assert 'httpbin' in r.url362 # def test_idna(self):363 # r = requests.get(u'http://➡.ws/httpbin')364 # assert 'httpbin' in r.url365366367 def test_urlencoded_get_query_multivalued_param(self):368
73 unmodified lines
442443 def test_session_HTTP_200_OK_GET(self):444445 s = Session()473 r = s.get(httpbin('/'))446 r = s.get(httpbin('/get'))447 self.assertEqual(r.status_code, 200)448449477 def test_session_HTTPS_200_OK_GET(self):478479 s = Session()480 r = s.get(httpsbin('/'))481 self.assertEqual(r.status_code, 200)482483450 def test_session_persistent_headers(self):451452 heads = {'User-agent': 'Mozilla/5.0'}453454 s = Session()455 s.headers = heads456457 # Make 2 requests from Session object, should send header both times458 r1 = s.get(httpbin('user-agent'))492459 assert heads['User-agent'] in r1.content494 r2 = s.get(httpbin('user-agent'))460461 r2 = s.get(httpbin('user-agent'))462 assert heads['User-agent'] in r2.content463464 new_heads = {'User-agent': 'blah'}465 r3 = s.get(httpbin('user-agent'), headers=new_heads)466 assert new_heads['User-agent'] in r3.content467468 self.assertEqual(r2.status_code, 200)469470471if __name__ == '__main__':
1 unmodified line