pyfex 0.1.0__py3-none-any.whl
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.
- pyfex/__init__.py +6 -0
- pyfex/cli.py +24 -0
- pyfex/core.py +89 -0
- pyfex-0.1.0.dist-info/METADATA +59 -0
- pyfex-0.1.0.dist-info/RECORD +9 -0
- pyfex-0.1.0.dist-info/WHEEL +5 -0
- pyfex-0.1.0.dist-info/entry_points.txt +2 -0
- pyfex-0.1.0.dist-info/licenses/LICENSE +21 -0
- pyfex-0.1.0.dist-info/top_level.txt +1 -0
pyfex/__init__.py
ADDED
pyfex/cli.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import sys
|
|
5
|
+
|
|
6
|
+
from .core import fex
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def main(argv: list[str] | None = None) -> int:
|
|
10
|
+
parser = argparse.ArgumentParser(
|
|
11
|
+
prog="pyfex",
|
|
12
|
+
description="Fetch and execute a Python script from a URL.",
|
|
13
|
+
)
|
|
14
|
+
parser.add_argument("url", help="Target URL (https:// is added automatically if omitted)")
|
|
15
|
+
parser.add_argument("-t", "--timeout", type=int, default=10, help="Request timeout in seconds (default: 10)")
|
|
16
|
+
parser.add_argument("-s", "--silent", action="store_true", help="Suppress error output")
|
|
17
|
+
args = parser.parse_args(argv)
|
|
18
|
+
|
|
19
|
+
ok = fex(args.url, timeout=args.timeout, silent=args.silent)
|
|
20
|
+
return 0 if ok else 1
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
if __name__ == "__main__":
|
|
24
|
+
sys.exit(main())
|
pyfex/core.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import urllib.request
|
|
4
|
+
from urllib.error import HTTPError, URLError
|
|
5
|
+
|
|
6
|
+
DEFAULT_HEADERS = {"User-Agent": "pyfex/0.1.0"}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _normalize_url(url: str) -> str:
|
|
10
|
+
url = url.strip()
|
|
11
|
+
if not url:
|
|
12
|
+
raise ValueError("URL must not be empty.")
|
|
13
|
+
if not url.startswith(("http://", "https://")):
|
|
14
|
+
url = "https://" + url
|
|
15
|
+
return url
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def fetch(
|
|
19
|
+
url: str,
|
|
20
|
+
timeout: int = 10,
|
|
21
|
+
headers: dict[str, str] | None = None,
|
|
22
|
+
) -> tuple[int, str]:
|
|
23
|
+
"""
|
|
24
|
+
Perform an HTTP GET request to `url` (auto-prepending `https://` if missing).
|
|
25
|
+
Returns `(status_code, response_text)`.
|
|
26
|
+
"""
|
|
27
|
+
normalized = _normalize_url(url)
|
|
28
|
+
req_headers = {**DEFAULT_HEADERS, **(headers or {})}
|
|
29
|
+
req = urllib.request.Request(normalized, method="GET", headers=req_headers)
|
|
30
|
+
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
|
31
|
+
charset = resp.headers.get_content_charset() or "utf-8"
|
|
32
|
+
body = resp.read().decode(charset, errors="replace")
|
|
33
|
+
return resp.status, body
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def fex(
|
|
37
|
+
url: str,
|
|
38
|
+
namespace: dict | None = None,
|
|
39
|
+
timeout: int = 10,
|
|
40
|
+
headers: dict[str, str] | None = None,
|
|
41
|
+
silent: bool = False,
|
|
42
|
+
) -> bool:
|
|
43
|
+
"""
|
|
44
|
+
Fetch Python code from `url` via GET and execute it with `exec()`.
|
|
45
|
+
Handles missing scheme, network/HTTP errors, timeouts, and empty responses safely.
|
|
46
|
+
Returns `True` on success, `False` otherwise.
|
|
47
|
+
"""
|
|
48
|
+
def _log(msg: str) -> None:
|
|
49
|
+
if not silent:
|
|
50
|
+
print(msg)
|
|
51
|
+
|
|
52
|
+
try:
|
|
53
|
+
status, code = fetch(url, timeout=timeout, headers=headers)
|
|
54
|
+
except ValueError as e:
|
|
55
|
+
_log(f"[pyfex] {e}")
|
|
56
|
+
return False
|
|
57
|
+
except HTTPError as e:
|
|
58
|
+
_log(f"[pyfex] HTTP error: {e.code} {e.reason}")
|
|
59
|
+
return False
|
|
60
|
+
except URLError as e:
|
|
61
|
+
_log(f"[pyfex] Request failed: {e.reason}")
|
|
62
|
+
return False
|
|
63
|
+
except TimeoutError:
|
|
64
|
+
_log(f"[pyfex] Request timed out after {timeout}s.")
|
|
65
|
+
return False
|
|
66
|
+
except Exception as e:
|
|
67
|
+
_log(f"[pyfex] Unexpected request error: {e}")
|
|
68
|
+
return False
|
|
69
|
+
|
|
70
|
+
if status != 200:
|
|
71
|
+
_log(f"[pyfex] Unexpected HTTP status: {status}")
|
|
72
|
+
return False
|
|
73
|
+
|
|
74
|
+
code = code.strip()
|
|
75
|
+
if not code:
|
|
76
|
+
_log("[pyfex] Empty response body.")
|
|
77
|
+
return False
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
exec_ns = {"__name__": "__main__"}
|
|
81
|
+
if namespace is not None:
|
|
82
|
+
exec_ns.update(namespace)
|
|
83
|
+
exec(code, exec_ns)
|
|
84
|
+
if namespace is not None:
|
|
85
|
+
namespace.update(exec_ns)
|
|
86
|
+
return True
|
|
87
|
+
except Exception as e:
|
|
88
|
+
_log(f"[pyfex] Execution error: {e}")
|
|
89
|
+
return False
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyfex
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lightweight zero-dependency utility to fetch and execute remote Python scripts.
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: fetch,exec,http,remote,script,loader
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# pyfex
|
|
23
|
+
|
|
24
|
+
Lightweight, zero-dependency Python library & CLI to fetch and execute remote Python scripts over HTTP/HTTPS.
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install pyfex
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
### Python API
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from pyfex import fex, fetch
|
|
38
|
+
|
|
39
|
+
# Fetch and execute (https:// is optional)
|
|
40
|
+
fex("example.com/script.py")
|
|
41
|
+
|
|
42
|
+
# Pass variables into the executed script namespace
|
|
43
|
+
ns = {"x": 42}
|
|
44
|
+
if fex("https://example.com/script.py", namespace=ns, timeout=15):
|
|
45
|
+
print("Done!", ns.get("result"))
|
|
46
|
+
|
|
47
|
+
# Only fetch without executing
|
|
48
|
+
status, text = fetch("example.com")
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### CLI
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pyfex example.com/script.py --timeout 10
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Security Notice
|
|
58
|
+
|
|
59
|
+
`fex()` executes arbitrary Python code via `exec()`. Only use it with trusted URLs over HTTPS.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pyfex/__init__.py,sha256=_CzZqqOsCSxmJ5QjHk6GSFGa90oRYL87SWcVFfzg9Ys,130
|
|
2
|
+
pyfex/cli.py,sha256=J7VP5aZvmHqRI2hcODaQnfSoaLdbHoD4qBMbgXRk9oI,750
|
|
3
|
+
pyfex/core.py,sha256=Q1I2Ft5LrceaYTEJRN0GHI9vk_KwAYoaUy6AEhY6wFk,2614
|
|
4
|
+
pyfex-0.1.0.dist-info/licenses/LICENSE,sha256=ESYyLizI0WWtxMeS7rGVcX3ivMezm-HOd5WdeOh-9oU,1056
|
|
5
|
+
pyfex-0.1.0.dist-info/METADATA,sha256=WpbpGwA033Icrh26U3jvSlhpBAKvvenUZFZNiPQj4PQ,1581
|
|
6
|
+
pyfex-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
7
|
+
pyfex-0.1.0.dist-info/entry_points.txt,sha256=tZIsV7ldoATgZ7wMQHSOnrnIkrIrOMCHZW6sDfM1qkQ,41
|
|
8
|
+
pyfex-0.1.0.dist-info/top_level.txt,sha256=ASA9c4HuFotuHxQEpP6zS8PBg1HnFBVXmL9jyLJZS9k,6
|
|
9
|
+
pyfex-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyfex
|