python-urlopen 0.0.2__tar.gz → 0.0.3__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.2 → python_urlopen-0.0.3}/PKG-INFO +1 -1
- {python_urlopen-0.0.2 → python_urlopen-0.0.3}/pyproject.toml +1 -1
- {python_urlopen-0.0.2 → python_urlopen-0.0.3}/urlopen/__init__.py +12 -6
- {python_urlopen-0.0.2 → python_urlopen-0.0.3}/urlopen/__main__.py +1 -4
- {python_urlopen-0.0.2 → python_urlopen-0.0.3}/LICENSE +0 -0
- {python_urlopen-0.0.2 → python_urlopen-0.0.3}/readme.md +0 -0
- {python_urlopen-0.0.2 → python_urlopen-0.0.3}/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, 3)
|
|
6
6
|
__all__ = ["urlopen", "download"]
|
|
7
7
|
|
|
8
8
|
import errno
|
|
@@ -10,6 +10,7 @@ import errno
|
|
|
10
10
|
from collections.abc import Callable, Generator, Mapping, Sequence
|
|
11
11
|
from copy import copy
|
|
12
12
|
from http.client import HTTPResponse
|
|
13
|
+
from http.cookiejar import CookieJar
|
|
13
14
|
from inspect import isgenerator
|
|
14
15
|
from json import dumps
|
|
15
16
|
from os import fsdecode, fstat, makedirs, PathLike
|
|
@@ -18,7 +19,7 @@ from shutil import COPY_BUFSIZE # type: ignore
|
|
|
18
19
|
from ssl import SSLContext, _create_unverified_context
|
|
19
20
|
from typing import cast, Any, Optional
|
|
20
21
|
from urllib.parse import urlencode, urlsplit
|
|
21
|
-
from urllib.request import build_opener, HTTPSHandler, OpenerDirector, Request
|
|
22
|
+
from urllib.request import build_opener, HTTPCookieProcessor, HTTPSHandler, OpenerDirector, Request
|
|
22
23
|
|
|
23
24
|
from filewrap import bio_skip_iter, SupportsRead, SupportsWrite
|
|
24
25
|
from http_response import get_filename, get_length, is_chunked, is_range_request
|
|
@@ -30,12 +31,13 @@ if "__del__" not in HTTPResponse.__dict__:
|
|
|
30
31
|
|
|
31
32
|
def urlopen(
|
|
32
33
|
url: str | Request,
|
|
34
|
+
method: str = "GET",
|
|
33
35
|
params: Optional[str | Mapping | Sequence[tuple[Any, Any]]] = None,
|
|
34
36
|
data: Optional[bytes | str | Mapping | Sequence[tuple[Any, Any]]] = None,
|
|
35
37
|
json: Any = None,
|
|
36
38
|
headers: dict[str, str] = {"User-agent": ""},
|
|
37
|
-
method: str = "GET",
|
|
38
39
|
timeout: Optional[int | float] = None,
|
|
40
|
+
cookies: Optional[CookieJar] = None,
|
|
39
41
|
proxy: Optional[tuple[str, str]] = None,
|
|
40
42
|
opener: OpenerDirector = build_opener(HTTPSHandler(context=_create_unverified_context())),
|
|
41
43
|
context: Optional[SSLContext] = None,
|
|
@@ -77,11 +79,15 @@ def urlopen(
|
|
|
77
79
|
else:
|
|
78
80
|
if params:
|
|
79
81
|
url += "?&"["?" in url] + params
|
|
80
|
-
req = Request(url, data, headers=headers, method=method.upper())
|
|
82
|
+
req = Request(url, data=data, headers=headers, method=method.upper())
|
|
81
83
|
if proxy:
|
|
82
84
|
req.set_proxy(*proxy)
|
|
83
|
-
if
|
|
84
|
-
opener = build_opener(
|
|
85
|
+
if opener is None:
|
|
86
|
+
opener = build_opener()
|
|
87
|
+
if context is not None:
|
|
88
|
+
opener.add_handler(HTTPSHandler(context=context))
|
|
89
|
+
if cookies is not None:
|
|
90
|
+
opener.add_handler(HTTPCookieProcessor(cookies))
|
|
85
91
|
if timeout is None:
|
|
86
92
|
return opener.open(req)
|
|
87
93
|
else:
|
|
@@ -13,21 +13,18 @@ from . import download, __version__
|
|
|
13
13
|
|
|
14
14
|
def parse_args():
|
|
15
15
|
parser = ArgumentParser(description=__doc__, formatter_class=RawTextHelpFormatter)
|
|
16
|
-
|
|
17
16
|
parser.add_argument("url", nargs="?", help="URL to be downloaded")
|
|
18
17
|
parser.add_argument("-o", "--output-file", help="file path to be downloaded, if omitted, print into stdout")
|
|
19
18
|
parser.add_argument("-r", "--resume", action="store_true", help="skip downloaded data")
|
|
20
19
|
parser.add_argument("-hs", "--headers", help="dictionary of HTTP Headers to send with")
|
|
21
20
|
parser.add_argument("-v", "--version", action="store_true", help="print the current version")
|
|
22
|
-
|
|
23
21
|
args = parser.parse_args()
|
|
24
|
-
|
|
25
22
|
if args.version:
|
|
26
23
|
print(".".join(map(str, __version__)))
|
|
27
24
|
raise SystemExit(0)
|
|
28
|
-
|
|
29
25
|
if not args.url:
|
|
30
26
|
parser.parse_args(["-h"])
|
|
27
|
+
return args
|
|
31
28
|
|
|
32
29
|
|
|
33
30
|
def headers_str_to_dict(headers: str, /) -> dict[str, str]:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|