python-urlopen 0.0.7__tar.gz → 0.0.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/PKG-INFO +1 -1
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/pyproject.toml +1 -1
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/urlopen/__init__.py +16 -9
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/LICENSE +0 -0
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/readme.md +0 -0
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/urlopen/__main__.py +0 -0
- {python_urlopen-0.0.7 → python_urlopen-0.0.7.1}/urlopen/py.typed +0 -0
|
@@ -27,10 +27,8 @@ from urllib.request import build_opener, HTTPCookieProcessor, HTTPSHandler, Open
|
|
|
27
27
|
from zlib import compressobj, DEF_MEM_LEVEL, DEFLATED, MAX_WBITS
|
|
28
28
|
|
|
29
29
|
from argtools import argcount
|
|
30
|
-
from brotli import decompress as decompress_br # type: ignore
|
|
31
30
|
from filewrap import bio_skip_iter, SupportsWrite
|
|
32
31
|
from http_response import get_filename, get_length, is_chunked, is_range_request
|
|
33
|
-
from zstandard import decompress as decompress_zstd
|
|
34
32
|
|
|
35
33
|
|
|
36
34
|
if "__del__" not in HTTPResponse.__dict__:
|
|
@@ -38,10 +36,11 @@ if "__del__" not in HTTPResponse.__dict__:
|
|
|
38
36
|
if "__del__" not in OpenerDirector.__dict__:
|
|
39
37
|
setattr(OpenerDirector, "__del__", OpenerDirector.close)
|
|
40
38
|
|
|
39
|
+
_opener: None | OpenerDirector = None
|
|
41
40
|
CRE_search_charset = re_compile(r"\bcharset=(?P<charset>[^ ;]+)").search
|
|
42
41
|
|
|
43
42
|
|
|
44
|
-
def decompress_deflate(data, compresslevel=9):
|
|
43
|
+
def decompress_deflate(data: bytes, compresslevel: int = 9) -> bytes:
|
|
45
44
|
# Fork from: https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations#answer-1089787
|
|
46
45
|
compress = compressobj(
|
|
47
46
|
compresslevel, # level: 0-9
|
|
@@ -63,7 +62,7 @@ def decompress_deflate(data, compresslevel=9):
|
|
|
63
62
|
return deflated
|
|
64
63
|
|
|
65
64
|
|
|
66
|
-
def ensure_ascii_url(url: str, /):
|
|
65
|
+
def ensure_ascii_url(url: str, /) -> str:
|
|
67
66
|
if url.isascii():
|
|
68
67
|
return url
|
|
69
68
|
return quote(url, safe=punctuation)
|
|
@@ -78,8 +77,10 @@ def decompress_response(resp: HTTPResponse) -> bytes:
|
|
|
78
77
|
case "deflate":
|
|
79
78
|
data = decompress_deflate(data)
|
|
80
79
|
case "br":
|
|
80
|
+
from brotli import decompress as decompress_br # type: ignore
|
|
81
81
|
data = decompress_br(data)
|
|
82
82
|
case "zstd":
|
|
83
|
+
from zstandard import decompress as decompress_zstd
|
|
83
84
|
data = decompress_zstd(data)
|
|
84
85
|
return data
|
|
85
86
|
|
|
@@ -95,9 +96,10 @@ def urlopen(
|
|
|
95
96
|
cookies: None | CookieJar = None,
|
|
96
97
|
proxy: None | tuple[str, str] = None,
|
|
97
98
|
context: None | SSLContext = None,
|
|
98
|
-
opener: OpenerDirector =
|
|
99
|
+
opener: None | OpenerDirector = None,
|
|
99
100
|
origin: None | str = None,
|
|
100
101
|
) -> HTTPResponse:
|
|
102
|
+
global _opener
|
|
101
103
|
if isinstance(url, str) and not urlsplit(url).scheme:
|
|
102
104
|
if origin:
|
|
103
105
|
if not url.startswith("/"):
|
|
@@ -148,12 +150,17 @@ def urlopen(
|
|
|
148
150
|
req = Request(url, data=data, headers=headers, method=method.upper())
|
|
149
151
|
if proxy:
|
|
150
152
|
req.set_proxy(*proxy)
|
|
153
|
+
if opener is None:
|
|
154
|
+
if _opener is None:
|
|
155
|
+
opener = _opener = build_opener(HTTPSHandler(context=_create_unverified_context()))
|
|
156
|
+
else:
|
|
157
|
+
opener = _opener
|
|
151
158
|
if context is not None or cookies is not None:
|
|
152
159
|
opener = copy(opener)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
160
|
+
if context is not None:
|
|
161
|
+
opener.add_handler(HTTPSHandler(context=context))
|
|
162
|
+
if cookies is not None:
|
|
163
|
+
opener.add_handler(HTTPCookieProcessor(cookies))
|
|
157
164
|
req.full_url = ensure_ascii_url(req.full_url)
|
|
158
165
|
if timeout is None:
|
|
159
166
|
return opener.open(req)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|