jpayne@7: # __ jpayne@7: # /__) _ _ _ _ _/ _ jpayne@7: # / ( (- (/ (/ (- _) / _) jpayne@7: # / jpayne@7: jpayne@7: """ jpayne@7: Requests HTTP Library jpayne@7: ~~~~~~~~~~~~~~~~~~~~~ jpayne@7: jpayne@7: Requests is an HTTP library, written in Python, for human beings. jpayne@7: Basic GET usage: jpayne@7: jpayne@7: >>> import requests jpayne@7: >>> r = requests.get('https://www.python.org') jpayne@7: >>> r.status_code jpayne@7: 200 jpayne@7: >>> b'Python is a programming language' in r.content jpayne@7: True jpayne@7: jpayne@7: ... or POST: jpayne@7: jpayne@7: >>> payload = dict(key1='value1', key2='value2') jpayne@7: >>> r = requests.post('https://httpbin.org/post', data=payload) jpayne@7: >>> print(r.text) jpayne@7: { jpayne@7: ... jpayne@7: "form": { jpayne@7: "key1": "value1", jpayne@7: "key2": "value2" jpayne@7: }, jpayne@7: ... jpayne@7: } jpayne@7: jpayne@7: The other HTTP methods are supported - see `requests.api`. Full documentation jpayne@7: is at . jpayne@7: jpayne@7: :copyright: (c) 2017 by Kenneth Reitz. jpayne@7: :license: Apache 2.0, see LICENSE for more details. jpayne@7: """ jpayne@7: jpayne@7: import warnings jpayne@7: jpayne@7: import urllib3 jpayne@7: jpayne@7: from .exceptions import RequestsDependencyWarning jpayne@7: jpayne@7: try: jpayne@7: from charset_normalizer import __version__ as charset_normalizer_version jpayne@7: except ImportError: jpayne@7: charset_normalizer_version = None jpayne@7: jpayne@7: try: jpayne@7: from chardet import __version__ as chardet_version jpayne@7: except ImportError: jpayne@7: chardet_version = None jpayne@7: jpayne@7: jpayne@7: def check_compatibility(urllib3_version, chardet_version, charset_normalizer_version): jpayne@7: urllib3_version = urllib3_version.split(".") jpayne@7: assert urllib3_version != ["dev"] # Verify urllib3 isn't installed from git. jpayne@7: jpayne@7: # Sometimes, urllib3 only reports its version as 16.1. jpayne@7: if len(urllib3_version) == 2: jpayne@7: urllib3_version.append("0") jpayne@7: jpayne@7: # Check urllib3 for compatibility. jpayne@7: major, minor, patch = urllib3_version # noqa: F811 jpayne@7: major, minor, patch = int(major), int(minor), int(patch) jpayne@7: # urllib3 >= 1.21.1 jpayne@7: assert major >= 1 jpayne@7: if major == 1: jpayne@7: assert minor >= 21 jpayne@7: jpayne@7: # Check charset_normalizer for compatibility. jpayne@7: if chardet_version: jpayne@7: major, minor, patch = chardet_version.split(".")[:3] jpayne@7: major, minor, patch = int(major), int(minor), int(patch) jpayne@7: # chardet_version >= 3.0.2, < 6.0.0 jpayne@7: assert (3, 0, 2) <= (major, minor, patch) < (6, 0, 0) jpayne@7: elif charset_normalizer_version: jpayne@7: major, minor, patch = charset_normalizer_version.split(".")[:3] jpayne@7: major, minor, patch = int(major), int(minor), int(patch) jpayne@7: # charset_normalizer >= 2.0.0 < 4.0.0 jpayne@7: assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0) jpayne@7: else: jpayne@7: raise Exception("You need either charset_normalizer or chardet installed") jpayne@7: jpayne@7: jpayne@7: def _check_cryptography(cryptography_version): jpayne@7: # cryptography < 1.3.4 jpayne@7: try: jpayne@7: cryptography_version = list(map(int, cryptography_version.split("."))) jpayne@7: except ValueError: jpayne@7: return jpayne@7: jpayne@7: if cryptography_version < [1, 3, 4]: jpayne@7: warning = "Old version of cryptography ({}) may cause slowdown.".format( jpayne@7: cryptography_version jpayne@7: ) jpayne@7: warnings.warn(warning, RequestsDependencyWarning) jpayne@7: jpayne@7: jpayne@7: # Check imported dependencies for compatibility. jpayne@7: try: jpayne@7: check_compatibility( jpayne@7: urllib3.__version__, chardet_version, charset_normalizer_version jpayne@7: ) jpayne@7: except (AssertionError, ValueError): jpayne@7: warnings.warn( jpayne@7: "urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported " jpayne@7: "version!".format( jpayne@7: urllib3.__version__, chardet_version, charset_normalizer_version jpayne@7: ), jpayne@7: RequestsDependencyWarning, jpayne@7: ) jpayne@7: jpayne@7: # Attempt to enable urllib3's fallback for SNI support jpayne@7: # if the standard library doesn't support SNI or the jpayne@7: # 'ssl' library isn't available. jpayne@7: try: jpayne@7: try: jpayne@7: import ssl jpayne@7: except ImportError: jpayne@7: ssl = None jpayne@7: jpayne@7: if not getattr(ssl, "HAS_SNI", False): jpayne@7: from urllib3.contrib import pyopenssl jpayne@7: jpayne@7: pyopenssl.inject_into_urllib3() jpayne@7: jpayne@7: # Check cryptography version jpayne@7: from cryptography import __version__ as cryptography_version jpayne@7: jpayne@7: _check_cryptography(cryptography_version) jpayne@7: except ImportError: jpayne@7: pass jpayne@7: jpayne@7: # urllib3's DependencyWarnings should be silenced. jpayne@7: from urllib3.exceptions import DependencyWarning jpayne@7: jpayne@7: warnings.simplefilter("ignore", DependencyWarning) jpayne@7: jpayne@7: # Set default logging handler to avoid "No handler found" warnings. jpayne@7: import logging jpayne@7: from logging import NullHandler jpayne@7: jpayne@7: from . import packages, utils jpayne@7: from .__version__ import ( jpayne@7: __author__, jpayne@7: __author_email__, jpayne@7: __build__, jpayne@7: __cake__, jpayne@7: __copyright__, jpayne@7: __description__, jpayne@7: __license__, jpayne@7: __title__, jpayne@7: __url__, jpayne@7: __version__, jpayne@7: ) jpayne@7: from .api import delete, get, head, options, patch, post, put, request jpayne@7: from .exceptions import ( jpayne@7: ConnectionError, jpayne@7: ConnectTimeout, jpayne@7: FileModeWarning, jpayne@7: HTTPError, jpayne@7: JSONDecodeError, jpayne@7: ReadTimeout, jpayne@7: RequestException, jpayne@7: Timeout, jpayne@7: TooManyRedirects, jpayne@7: URLRequired, jpayne@7: ) jpayne@7: from .models import PreparedRequest, Request, Response jpayne@7: from .sessions import Session, session jpayne@7: from .status_codes import codes jpayne@7: jpayne@7: logging.getLogger(__name__).addHandler(NullHandler()) jpayne@7: jpayne@7: # FileModeWarnings go off per the default. jpayne@7: warnings.simplefilter("default", FileModeWarning, append=True)