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 044252e, ed8ff63 · Browse files · 5 files+76 added−22 removed
This commit

State

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

Diff stats

5 files+76 added−22 removed

  • other:+76 added−22 removed

5 files+76 added−22 removed

modified:requests/core.py+34 added-15 removed
22 unmodified lines
23242526__title__ = 'requests'27__version__ = '0.3.0'28__build__ = 0x00030027__version__ = '0.3.1'28__build__ = 0x00030129__author__ = 'Kenneth Reitz'30__license__ = 'ISC'31__copyright__ = 'Copyright 2011 Kenneth Reitz'32
77 unmodified lines
110 """Creates appropriate opener object for urllib2."""111112 _handlers = []113114 if self.cookiejar is not None:115 _handlers.append(urllib2.HTTPCookieProcessor(self.cookiejar))116117 if self.auth:118 if not isinstance(self.auth.handler, (urllib2.AbstractBasicAuthHandler, urllib2.AbstractDigestAuthHandler)):119 auth_manager.add_password(self.auth.realm, self.url, self.auth.username, self.auth.password)120 self.auth.handler = self.auth.handler(auth_manager)121 auth_manager.add_auth(self.url, self.auth)122123 _handlers.append(self.auth.handler)124122 _handlers.extend(get_handlers())123 opener = urllib2.build_opener(*_handlers)124 return opener.open125 else:125 if not _handlers:126 return urllib2.urlopen127128 _handlers.extend(get_handlers())129 opener = urllib2.build_opener(*_handlers)130131 if self.headers:132 # Allow default headers in the opener to be overloaded133 normal_keys = [k.capitalize() for k in self.headers]134 for key, val in opener.addheaders[:]:135 if key not in normal_keys:136 continue137 # Remove it, we have a value to take its place138 opener.addheaders.remove((key, val))139140 return opener.open141142 def _build_response(self, resp):143 """Build internal Response object from given response."""144 145 self.response.status_code = getattr(resp, 'code', None)
13 unmodified lines
159 return '%s?%s' % (url, data)160 else:161 return url162150163 def send(self, anyway=False):164 """Sends the request. Returns True of successful, false if not.165 If there was an HTTPError during transmission,166 self.response.status_code will contain the HTTPError code.
22 unmodified lines
189 else:190 req = _Request(self.url, data=self._enc_data, method=self.method)191192 if self.headers:181 req.headers = self.headers193 req.headers.update(self.headers)194195 if not self.sent or anyway:196 try:197 opener = self._get_opener()186 resp = opener(req)198 resp = opener(req)199200 if self.cookiejar is not None:201 self.cookiejar.extract_cookies(resp, req)202 203 except urllib2.HTTPError, why:204 self._build_response(why)205 self.response.error = why206 else:
3 unmodified lines
210 self.response.cached = False211 else:212 self.response.cached = True213198214 self.sent = self.response.ok215216 return self.sent217218219 def read(self):220 return self.response.read()221 222class Response(object):223 """The :class:`Request` object. All :class:`Request` objects contain a224 :class:`Request.response <response>` attribute, which is an instance of
23 unmodified lines
248 """Raises stored HTTPError if one exists."""249 if self.error:250 raise self.error251252 def read(self):253 return self.content254255256class AuthManager(object):257 """Authentication Manager."""
183 unmodified lines
441 :param cookies: (optional) CookieJar object to send with the :class:`Request`.442 :param auth: (optional) AuthObject to enable Basic HTTP Auth.443 """444 426 return request('GET', url, params=params, headers=headers, cookiejar=cookies, auth=auth)445 return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth)446447448def head(url, params={}, headers={}, cookies=None, auth=None):449 """Sends a HEAD request. Returns :class:`Response` object.
4 unmodified lines
454 :param cookies: (optional) CookieJar object to send with the :class:`Request`.455 :param auth: (optional) AuthObject to enable Basic HTTP Auth.456 """457439 return request('HEAD', url, params=params, headers=headers, cookiejar=cookies, auth=auth)458 return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth)459460461def post(url, data={}, headers={}, files=None, cookies=None, auth=None):462 """Sends a POST request. Returns :class:`Response` object.
5 unmodified lines
468 :param cookies: (optional) CookieJar object to send with the :class:`Request`.469 :param auth: (optional) AuthObject to enable Basic HTTP Auth.470 """471453 return request('POST', url, data=data, headers=headers, files=files, cookiejar=cookies, auth=auth)472 return request('POST', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth)473474475def put(url, data='', headers={}, files={}, cookies=None, auth=None):476 """Sends a PUT request. Returns :class:`Response` object.
5 unmodified lines
482 :param cookies: (optional) CookieJar object to send with the :class:`Request`.483 :param auth: (optional) AuthObject to enable Basic HTTP Auth.484 """485467 return request('PUT', url, data=data, headers=headers, files=files, cookiejar=cookies, auth=auth)486 return request('PUT', url, data=data, headers=headers, files=files, cookies=cookies, auth=auth)487488489def delete(url, params={}, headers={}, cookies=None, auth=None):490 """Sends a DELETE request. Returns :class:`Response` object.
4 unmodified lines
495 :param cookies: (optional) CookieJar object to send with the :class:`Request`.496 :param auth: (optional) AuthObject to enable Basic HTTP Auth.497 """498480 return request('DELETE', url, params=params, headers=headers, cookiejar=cookies, auth=auth)499 return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth)500501502503class RequestException(Exception):
11 unmodified lines
modified:.gitignore+2 added-1 removed
1.idea2MANIFEST3coverage.xml4nosetests.xml5pylint.txt5pylint.txt6*.pyc
modified:AUTHORS+4 added-1 removed
11 unmodified lines
1213- Various Pocoo Members14- Chris Adams15- Flavio Percoco Premoli16- Dj Gilcrease16- Dj Gilcrease17- Justin Murphy18- Rob Madole19- Aram Dulyan
modified:HISTORY.rst+8 added
1History2-------340.3.1 (2011-04-01)5++++++++++++++++++67* Cookie Changes8* Response.read() 9* Poster fix1011120.3.0 (2011-02-25)13++++++++++++++++++1415* Automatic Authentication API Change
53 unmodified lines
modified:test_requests.py+28 added-5 removed
1#!/usr/bin/env python2# -*- coding: utf-8 -*-34import unittest5import cookielib67import requests89
18 unmodified lines
28 r = requests.get('https://google.com')29 self.assertEqual(r.status_code, 200)3031 def test_HTTP_200_OK_GET_WITH_PARAMS(self):3132 heads = {'User-agent': 'Mozilla/5.0'}33 34 r = requests.get('http://www.google.com/search', params={'q': 'test'}, headers=heads)35 self.assertEqual(r.status_code, 200)363737 def test_HTTP_200_OK_GET_WITH_MIXED_PARAMS(self):3938 heads = {'User-agent': 'Mozilla/5.0'}3940 r = requests.get('http://google.com/search?test=true', params={'q': 'test'}, headers=heads)41 self.assertEqual(r.status_code, 200)4243 def test_user_agent_transfers(self):44 """Issue XX"""45 heads = {'User-agent':46 'Mozilla/5.0 (github.com/kennethreitz/requests)'}4748 r = requests.get('http://whatsmyua.com', headers=heads);49 self.assertTrue(heads['User-agent'] in r.content)50 51 def test_HTTP_200_OK_HEAD(self):52 r = requests.head('http://google.com')53 self.assertEqual(r.status_code, 200)
15 unmodified lines
69 # reset auto authentication70 requests.auth_manager.empty()7172 def test_POSTBIN_GET_POST_FILES(self):6773 bin = requests.post('http://www.postbin.org/')74 print bin.url75 self.assertEqual(bin.status_code, 200)76
3 unmodified lines
80 post2 = requests.post(bin.url, files={'some': open('test_requests.py')})81 self.assertEqual(post2.status_code, 201)8283 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):7984 bin = requests.post('http://www.postbin.org/')85 86 self.assertEqual(bin.status_code, 200)8788 post2 = requests.post(bin.url, files={'some': open('test_requests.py')}, data={'some': 'data'})89 self.assertEqual(post2.status_code, 201)909192 def test_POSTBIN_GET_POST_FILES_WITH_HEADERS(self):93 bin = requests.post('http://www.postbin.org/')94 self.assertEqual(bin.status_code, 200)9596 post2 = requests.post(bin.url, files={'some': open('test_requests.py')},97 headers={'User-Agent': 'requests-tests'})9899 self.assertEqual(post2.status_code, 201)100 101 def test_nonzero_evaluation(self):102 r = requests.get('http://google.com/some-404-url')103 self.assertEqual(bool(r), False)104
11 unmodified lines
116 r = requests.get('http://google.com/')117 self.assertFalse(r.error)118 r.raise_for_status()119 120 def test_cookie_jar(self):121 """122 .. todo:: This really doesn't test to make sure the cookie is working123 """124 jar = cookielib.CookieJar()125 self.assertFalse(jar)126127 requests.get('http://google.com', cookies=jar)128 self.assertTrue(jar)129130131132if __name__ == '__main__':133 unittest.main()