Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/requests/__init__.py: 62%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

69 statements  

1# __ 

2# /__) _ _ _ _ _/ _ 

3# / ( (- (/ (/ (- _) / _) 

4# / 

5 

6""" 

7Requests HTTP Library 

8~~~~~~~~~~~~~~~~~~~~~ 

9 

10Requests is an HTTP library, written in Python, for human beings. 

11Basic GET usage: 

12 

13 >>> import requests 

14 >>> r = requests.get('https://www.python.org') 

15 >>> r.status_code 

16 200 

17 >>> b'Python is a programming language' in r.content 

18 True 

19 

20... or POST: 

21 

22 >>> payload = dict(key1='value1', key2='value2') 

23 >>> r = requests.post('https://httpbin.org/post', data=payload) 

24 >>> print(r.text) 

25 { 

26 ... 

27 "form": { 

28 "key1": "value1", 

29 "key2": "value2" 

30 }, 

31 ... 

32 } 

33 

34The other HTTP methods are supported - see `requests.api`. Full documentation 

35is at <https://requests.readthedocs.io>. 

36 

37:copyright: (c) 2017 by Kenneth Reitz. 

38:license: Apache 2.0, see LICENSE for more details. 

39""" 

40 

41import warnings 

42 

43import urllib3 

44 

45from .exceptions import RequestsDependencyWarning 

46 

47try: 

48 from charset_normalizer import __version__ as charset_normalizer_version 

49except ImportError: 

50 charset_normalizer_version = None 

51 

52try: 

53 from chardet import __version__ as chardet_version 

54except ImportError: 

55 chardet_version = None 

56 

57 

58def check_compatibility(urllib3_version, chardet_version, charset_normalizer_version): 

59 urllib3_version = urllib3_version.split(".") 

60 assert urllib3_version != ["dev"] # Verify urllib3 isn't installed from git. 

61 

62 # Sometimes, urllib3 only reports its version as 16.1. 

63 if len(urllib3_version) == 2: 

64 urllib3_version.append("0") 

65 

66 # Check urllib3 for compatibility. 

67 major, minor, patch = urllib3_version # noqa: F811 

68 major, minor, patch = int(major), int(minor), int(patch) 

69 # urllib3 >= 1.21.1 

70 assert major >= 1 

71 if major == 1: 

72 assert minor >= 21 

73 

74 # Check charset_normalizer for compatibility. 

75 if chardet_version: 

76 major, minor, patch = chardet_version.split(".")[:3] 

77 major, minor, patch = int(major), int(minor), int(patch) 

78 # chardet_version >= 3.0.2, < 6.0.0 

79 assert (3, 0, 2) <= (major, minor, patch) < (6, 0, 0) 

80 elif charset_normalizer_version: 

81 major, minor, patch = charset_normalizer_version.split(".")[:3] 

82 major, minor, patch = int(major), int(minor), int(patch) 

83 # charset_normalizer >= 2.0.0 < 4.0.0 

84 assert (2, 0, 0) <= (major, minor, patch) < (4, 0, 0) 

85 else: 

86 warnings.warn( 

87 "Unable to find acceptable character detection dependency " 

88 "(chardet or charset_normalizer).", 

89 RequestsDependencyWarning, 

90 ) 

91 

92 

93def _check_cryptography(cryptography_version): 

94 # cryptography < 1.3.4 

95 try: 

96 cryptography_version = list(map(int, cryptography_version.split("."))) 

97 except ValueError: 

98 return 

99 

100 if cryptography_version < [1, 3, 4]: 

101 warning = ( 

102 f"Old version of cryptography ({cryptography_version}) may cause slowdown." 

103 ) 

104 warnings.warn(warning, RequestsDependencyWarning) 

105 

106 

107# Check imported dependencies for compatibility. 

108try: 

109 check_compatibility( 

110 urllib3.__version__, chardet_version, charset_normalizer_version 

111 ) 

112except (AssertionError, ValueError): 

113 warnings.warn( 

114 f"urllib3 ({urllib3.__version__}) or chardet " 

115 f"({chardet_version})/charset_normalizer ({charset_normalizer_version}) " 

116 "doesn't match a supported version!", 

117 RequestsDependencyWarning, 

118 ) 

119 

120# Attempt to enable urllib3's fallback for SNI support 

121# if the standard library doesn't support SNI or the 

122# 'ssl' library isn't available. 

123try: 

124 try: 

125 import ssl 

126 except ImportError: 

127 ssl = None 

128 

129 if not getattr(ssl, "HAS_SNI", False): 

130 from urllib3.contrib import pyopenssl 

131 

132 pyopenssl.inject_into_urllib3() 

133 

134 # Check cryptography version 

135 from cryptography import __version__ as cryptography_version 

136 

137 _check_cryptography(cryptography_version) 

138except ImportError: 

139 pass 

140 

141# urllib3's DependencyWarnings should be silenced. 

142from urllib3.exceptions import DependencyWarning 

143 

144warnings.simplefilter("ignore", DependencyWarning) 

145 

146# Set default logging handler to avoid "No handler found" warnings. 

147import logging 

148from logging import NullHandler 

149 

150from . import packages, utils 

151from .__version__ import ( 

152 __author__, 

153 __author_email__, 

154 __build__, 

155 __cake__, 

156 __copyright__, 

157 __description__, 

158 __license__, 

159 __title__, 

160 __url__, 

161 __version__, 

162) 

163from .api import delete, get, head, options, patch, post, put, request 

164from .exceptions import ( 

165 ConnectionError, 

166 ConnectTimeout, 

167 FileModeWarning, 

168 HTTPError, 

169 JSONDecodeError, 

170 ReadTimeout, 

171 RequestException, 

172 Timeout, 

173 TooManyRedirects, 

174 URLRequired, 

175) 

176from .models import PreparedRequest, Request, Response 

177from .sessions import Session, session 

178from .status_codes import codes 

179 

180logging.getLogger(__name__).addHandler(NullHandler()) 

181 

182# FileModeWarnings go off per the default. 

183warnings.simplefilter("default", FileModeWarning, append=True)