Skip to content
Myr

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

2 files+124 added−47 removed

modified:requests/adapters.py+90 added-13 removed
6 unmodified lines
7This module contains the transport adapters that Requests uses to define8and maintain connections.9"""10import os11from .models import Response12from .packages.urllib3.poolmanager import PoolManager13from .utils import DEFAULT_CA_BUNDLE_PATH1415import socket16from .structures import CaseInsensitiveDict17from .packages.urllib3.exceptions import MaxRetryError, LocationParseError18from .packages.urllib3.exceptions import TimeoutError19from .packages.urllib3.exceptions import SSLError as _SSLError20from .packages.urllib3.exceptions import HTTPError as _HTTPError21from .packages.urllib3 import connectionpool, poolmanager22from .packages.urllib3.filepost import encode_multipart_formdata23from .exceptions import (24ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,25URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)2627DEFAULT_POOLSIZE = 1028DEFAULT_RETRIES = 029
3 unmodified lines
3334 def __init__(self):35 super(BaseAdapter, self).__init__()3625 def send(self):26 raise NotImplementedError37 # def send(self):38 # raise NotImplementedError3940 def close(self):41 raise NotImplementedError42
12 unmodified lines
5556 def init_poolmanager(self, connections, maxsize):57 self.poolmanager = PoolManager(num_pools=connections, maxsize=maxsize)5847 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:6162 cert_loc = None6364 # Allow self-specified cert location.53 if self.verify is not True:65 if verify is not True:66 cert_loc = self.verify6768 # Look for configuration.69 if not cert_loc and self.config.get('trust_env'):
14 unmodified lines
84 else:85 conn.cert_reqs = 'CERT_NONE'86 conn.ca_certs = None8776 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 = cert9495 @staticmethod96 def build_response(req, resp):97 response = Response()9899 # Fallback to None if there's no status_code, for whatever reason.100 response.status_code = getattr(resp, 'status', None)101102 # Make headers case-insensitive.103 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))104105 # Set encoding.106 # response.encoding = get_encoding_from_headers(response.headers)107 response.raw = resp108109 if isinstance(req.url, bytes):110 response.url = req.url.decode('utf-8')111 else:112 response.url = req.url113114 return response115 # Add new cookies from the server.116 # extract_cookies_to_jar(self.cookies, self, resp)117118119120121122 def close(self):123 """Dispose of any internal state.124125 Currently, this just closes the PoolManager, which closes pooled126 connections.127 """128 self.poolmanager.clear()12991 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."""13294 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)135136 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 )150151 except socket.error as sockerr:152 raise ConnectionError(sockerr)153154 except MaxRetryError as e:155 raise ConnectionError(e)156157 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.')164165 r = self.build_response(request, resp)166167 if prefetch:168 r.content169170 return r171172173174175176
modified:requests/models.py+34 added-34 removed
511 unmodified lines
512513 if resp:514515 # 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)517518 # Make headers case-insensitive.519 response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))519 # response.headers = CaseInsensitiveDict(getattr(resp, 'headers', {}))520521 # Set encoding.522 response.encoding = get_encoding_from_headers(response.headers)522 # response.encoding = get_encoding_from_headers(response.headers)523524 # Add new cookies from the server.525 extract_cookies_to_jar(self.cookies, self, resp)526
18 unmodified lines
545546 history = []547548 r = build(resp)549549 # TODO: session level shit550 if r.status_code in REDIRECT_STATI and not self.redirect:551552 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 lines
774775 if not self.sent or anyway:776777 # 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_header782783 # Pre-send hook.784 r = dispatch_hook('pre_send', self.hooks, self)785 self.__dict__.update(r.__dict__)786787 # 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 = True803804 except socket.error as sockerr:805 raise ConnectionError(sockerr)804 # except socket.error as sockerr:805 # raise ConnectionError(sockerr)806807 except MaxRetryError as e:808 raise ConnectionError(e)807 # except MaxRetryError as e:808 # raise ConnectionError(e)809810 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.')817818 # build_response can throw TooManyRedirects819 self._build_response(r)820
272 unmodified lines