python-urlopen 0.0.7.1__tar.gz → 0.0.8__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.1 → python_urlopen-0.0.8}/PKG-INFO +1 -1
- {python_urlopen-0.0.7.1 → python_urlopen-0.0.8}/pyproject.toml +1 -1
- {python_urlopen-0.0.7.1 → python_urlopen-0.0.8}/urlopen/__init__.py +19 -11
- {python_urlopen-0.0.7.1 → python_urlopen-0.0.8}/LICENSE +0 -0
- {python_urlopen-0.0.7.1 → python_urlopen-0.0.8}/readme.md +0 -0
- {python_urlopen-0.0.7.1 → python_urlopen-0.0.8}/urlopen/__main__.py +0 -0
- {python_urlopen-0.0.7.1 → python_urlopen-0.0.8}/urlopen/py.typed +0 -0
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
# coding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 0,
|
|
5
|
+
__version__ = (0, 0, 8)
|
|
6
6
|
__all__ = ["urlopen", "request", "download"]
|
|
7
7
|
|
|
8
8
|
import errno
|
|
@@ -18,9 +18,11 @@ from os import fsdecode, fstat, makedirs, PathLike
|
|
|
18
18
|
from os.path import abspath, dirname, isdir, join as joinpath
|
|
19
19
|
from re import compile as re_compile
|
|
20
20
|
from shutil import COPY_BUFSIZE # type: ignore
|
|
21
|
+
from socket import getdefaulttimeout, setdefaulttimeout
|
|
21
22
|
from ssl import SSLContext, _create_unverified_context
|
|
22
23
|
from string import punctuation
|
|
23
|
-
from
|
|
24
|
+
from types import EllipsisType
|
|
25
|
+
from typing import cast, Any, Literal
|
|
24
26
|
from urllib.error import HTTPError
|
|
25
27
|
from urllib.parse import quote, urlencode, urlsplit
|
|
26
28
|
from urllib.request import build_opener, HTTPCookieProcessor, HTTPSHandler, OpenerDirector, Request
|
|
@@ -39,6 +41,9 @@ if "__del__" not in OpenerDirector.__dict__:
|
|
|
39
41
|
_opener: None | OpenerDirector = None
|
|
40
42
|
CRE_search_charset = re_compile(r"\bcharset=(?P<charset>[^ ;]+)").search
|
|
41
43
|
|
|
44
|
+
if getdefaulttimeout() is None:
|
|
45
|
+
setdefaulttimeout(60)
|
|
46
|
+
|
|
42
47
|
|
|
43
48
|
def decompress_deflate(data: bytes, compresslevel: int = 9) -> bytes:
|
|
44
49
|
# Fork from: https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations#answer-1089787
|
|
@@ -62,13 +67,20 @@ def decompress_deflate(data: bytes, compresslevel: int = 9) -> bytes:
|
|
|
62
67
|
return deflated
|
|
63
68
|
|
|
64
69
|
|
|
70
|
+
def get_charset(content_type: str, default="utf-8") -> str:
|
|
71
|
+
match = CRE_search_charset(content_type)
|
|
72
|
+
if match is None:
|
|
73
|
+
return "utf-8"
|
|
74
|
+
return match["charset"]
|
|
75
|
+
|
|
76
|
+
|
|
65
77
|
def ensure_ascii_url(url: str, /) -> str:
|
|
66
78
|
if url.isascii():
|
|
67
79
|
return url
|
|
68
80
|
return quote(url, safe=punctuation)
|
|
69
81
|
|
|
70
82
|
|
|
71
|
-
def decompress_response(resp: HTTPResponse) -> bytes:
|
|
83
|
+
def decompress_response(resp: HTTPResponse, /) -> bytes:
|
|
72
84
|
data = resp.read()
|
|
73
85
|
content_encoding = resp.headers.get("Content-Encoding")
|
|
74
86
|
match content_encoding:
|
|
@@ -168,17 +180,10 @@ def urlopen(
|
|
|
168
180
|
return opener.open(req, timeout=timeout)
|
|
169
181
|
|
|
170
182
|
|
|
171
|
-
def get_charset(content_type: str, default="utf-8") -> str:
|
|
172
|
-
match = CRE_search_charset(content_type)
|
|
173
|
-
if match is None:
|
|
174
|
-
return "utf-8"
|
|
175
|
-
return match["charset"]
|
|
176
|
-
|
|
177
|
-
|
|
178
183
|
def request(
|
|
179
184
|
url: str | Request,
|
|
180
185
|
method: str = "GET",
|
|
181
|
-
parse: None | bool | Callable = None,
|
|
186
|
+
parse: None | EllipsisType | bool | Callable = None,
|
|
182
187
|
raise_for_status: bool = True,
|
|
183
188
|
timeout: None | float = 60,
|
|
184
189
|
**request_kwargs,
|
|
@@ -197,6 +202,9 @@ def request(
|
|
|
197
202
|
resp = getattr(e, "file")
|
|
198
203
|
if parse is None:
|
|
199
204
|
return resp
|
|
205
|
+
elif parse is ...:
|
|
206
|
+
resp.close()
|
|
207
|
+
return resp
|
|
200
208
|
with resp:
|
|
201
209
|
if isinstance(parse, bool):
|
|
202
210
|
data = decompress_response(resp)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|