working!!!
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 · parent 88d4143 · Browse files · 2 files·+124 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+124 added−47 removed
- other:+124 added−47 removed
Provenance
- Commit
5ce4b69- Tree
fb7424b- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
88d4143
2 files changed+124 added−47 removed
modified:requests/adapters.py+90 added-13 removed
6 unmodified lines7 This module contains the transport adapters that Requests uses to define8 and maintain connections.9 """10 import os11+from .models import Response12 from .packages.urllib3.poolmanager import PoolManager13 from .utils import DEFAULT_CA_BUNDLE_PATH14 15+import socket16+from .structures import CaseInsensitiveDict17+from .packages.urllib3.exceptions import MaxRetryError, LocationParseError18+from .packages.urllib3.exceptions import TimeoutError19+from .packages.urllib3.exceptions import SSLError as _SSLError20+from .packages.urllib3.exceptions import HTTPError as _HTTPError21+from .packages.urllib3 import connectionpool, poolmanager22+from .packages.urllib3.filepost import encode_multipart_formdata23+from .exceptions import (24+ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,25+URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)26 27 DEFAULT_POOLSIZE = 1028 DEFAULT_RETRIES = 029 3 unmodified lines33 34 def __init__(self):35 super(BaseAdapter, self).__init__()36 25- def send(self):26- raise NotImplementedError37+ # def send(self):38+ # raise NotImplementedError39 40 def close(self):41 raise NotImplementedError42 12 unmodified lines55 56 def init_poolmanager(self, connections, maxsize):57 self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize)58 47- def cert_verify(self, conn, verify, cert):48- if url.startswith('https') and self.verify:59+ def cert_verify(self, conn, url, verify, cert):60+ if url.startswith('https') and verify:61 62 cert_loc = None63 64 # Allow self-specified cert location.53- if self.verify is not True:65+ if verify is not True:66 cert_loc = self.verify67 68 # Look for configuration.69 if not cert_loc and self.config.get('trust_env'):14 unmodified lines84 else:85 conn.cert_reqs = 'CERT_NONE'86 conn.ca_certs = None87 76- if self.cert:77- if len(self.cert) == 2:78- conn.cert_file = self.cert[0]79- conn.key_file = self.cert[1]88+ if cert:89+ if len(cert) == 2:90+ conn.cert_file = cert[0]91+ conn.key_file = cert[1]92 else:81- conn.cert_file = self.cert93+ conn.cert_file = cert94 95+ @staticmethod96+ def build_response(req, resp):97+ response = Response()98+99+ # Fallback to None if there's no status_code, for whatever reason.100+ response.status_code = getattr(resp, 'status', None)101+102+ # Make headers case-insensitive.103+ response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))104+105+ # Set encoding.106+ # response.encoding = get_encoding_from_headers(response.headers)107+ response.raw = resp108+109+ if isinstance(req.url, bytes):110+ response.url = req.url.decode('utf-8')111+ else:112+ response.url = req.url113+114+ return response115+ # Add new cookies from the server.116+ # extract_cookies_to_jar(self.cookies, self, resp)117+118+119+120+121+122 def close(self):123 """Dispose of any internal state.124 125 Currently, this just closes the PoolManager, which closes pooled126 connections.127 """128 self.poolmanager.clear()129 91- def send(self, request, timeout, verify, cert):130+ def send(self, request, prefetch=True, timeout=None, verify=True, cert=None):131 """Sends PreparedRequest object. Returns Response object."""132 94- conn = self._poolmanager.connection_from_url(request.url)95- self.cert_verify(conn, verify, cert)133+ conn = self.poolmanager.connection_from_url(request.url)134+ self.cert_verify(conn, request.url, verify, cert)135 136+ try:137+ # Send the request.138+ resp = conn.urlopen(139+ method=request.method,140+ url=request.path_url,141+ body=request.body,142+ headers=request.headers,143+ redirect=False,144+ assert_same_host=False,145+ preload_content=False,146+ decode_content=False,147+ retries=self.max_retries,148+ timeout=timeout,149+ )150 151+ except socket.error as sockerr:152+ raise ConnectionError(sockerr)153 154+ except MaxRetryError as e:155+ raise ConnectionError(e)156 157+ except (_SSLError, _HTTPError) as e:158+ if isinstance(e, _SSLError):159+ raise SSLError(e)160+ elif isinstance(e, TimeoutError):161+ raise Timeout(e)162+ else:163+ raise Timeout('Request timed out.')164+165+ r = self.build_response(request, resp)166+167+ if prefetch:168+ r.content169+170+ return r171+172+173+174+175+176+modified:requests/models.py+34 added-34 removed
511 unmodified lines512 513 if resp:514 515 # Fallback to None if there's no status_code, for whatever reason.516- response.status_code = getattr(resp, 'status', None)516+ # response.status_code = getattr(resp, 'status', None)517 518 # Make headers case-insensitive.519- response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))519+ # response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))520 521 # Set encoding.522- response.encoding = get_encoding_from_headers(response.headers)522+ # response.encoding = get_encoding_from_headers(response.headers)523 524 # Add new cookies from the server.525 extract_cookies_to_jar(self.cookies, self, resp)526 18 unmodified lines545 546 history = []547 548 r = build(resp)549-549+ # TODO: session level shit550 if r.status_code in REDIRECT_STATI and not self.redirect:551 552 while (('location' in r.headers and r.status_code in REDIRECT_STATI) and553 ((r.status_code is codes.see_other) or (self.allow_redirects))):220 unmodified lines774 775 if not self.sent or anyway:776 777 # Skip if 'cookie' header is explicitly set.778- if 'cookie' not in self.headers:779- cookie_header = get_cookie_header(self.cookies, self)780- if cookie_header is not None:781- self.headers['Cookie'] = cookie_header778+ # if 'cookie' not in self.headers:779+ # cookie_header = get_cookie_header(self.cookies, self)780+ # if cookie_header is not None:781+ # self.headers['Cookie'] = cookie_header782 783 # Pre-send hook.784 r = dispatch_hook('pre_send', self.hooks, self)785 self.__dict__.update(r.__dict__)786 787 # catch urllib3 exceptions and throw Requests exceptions788- try:789- # Send the request.790- r = conn.urlopen(791- method=self.method,792- url=self.path_url,793- body=body,794- headers=self.headers,795- redirect=False,796- assert_same_host=False,797- preload_content=False,798- decode_content=False,799- retries=self.config.get('max_retries', 0),800- timeout=self.timeout,801- )802- self.sent = True788+ # try:789+ # # Send the request.790+ # r = conn.urlopen(791+ # method=self.method,792+ # url=self.path_url,793+ # body=body,794+ # headers=self.headers,795+ # redirect=False,796+ # assert_same_host=False,797+ # preload_content=False,798+ # decode_content=False,799+ # retries=self.config.get('max_retries', 0),800+ # timeout=self.timeout,801+ # )802+ # self.sent = True803 804- except socket.error as sockerr:805- raise ConnectionError(sockerr)804+ # except socket.error as sockerr:805+ # raise ConnectionError(sockerr)806 807- except MaxRetryError as e:808- raise ConnectionError(e)807+ # except MaxRetryError as e:808+ # raise ConnectionError(e)809 810- except (_SSLError, _HTTPError) as e:811- if isinstance(e, _SSLError):812- raise SSLError(e)813- elif isinstance(e, TimeoutError):814- raise Timeout(e)815- else:816- raise Timeout('Request timed out.')810+ # except (_SSLError, _HTTPError) as e:811+ # if isinstance(e, _SSLError):812+ # raise SSLError(e)813+ # elif isinstance(e, TimeoutError):814+ # raise Timeout(e)815+ # else:816+ # raise Timeout('Request timed out.')817 818 # build_response can throw TooManyRedirects819 self._build_response(r)820 272 unmodified lines