wirepulse 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.
- wirepulse/__init__.py +124 -0
- wirepulse/__init__.pyi +80 -0
- wirepulse-0.1.0.dist-info/METADATA +108 -0
- wirepulse-0.1.0.dist-info/RECORD +6 -0
- wirepulse-0.1.0.dist-info/WHEEL +5 -0
- wirepulse-0.1.0.dist-info/top_level.txt +1 -0
wirepulse/__init__.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from socketserver import StreamRequestHandler as _brh, TCPServer as _tcpserv, ThreadingTCPServer as _thtcpserv
|
|
2
|
+
from urllib.parse import urlsplit, parse_qs
|
|
3
|
+
|
|
4
|
+
class BaseHTTPRequestHandler(_brh):
|
|
5
|
+
Protocol: str = 'HTTP/1.0'
|
|
6
|
+
|
|
7
|
+
def _read_request(self) -> tuple[str, str, dict[str, list[str]], str, dict[str, str], bytes] | None:
|
|
8
|
+
try:
|
|
9
|
+
first_line = self.rfile.readline()
|
|
10
|
+
if not first_line:
|
|
11
|
+
return None
|
|
12
|
+
method, raw_path, protocol = first_line.decode('utf-8').strip().split(' ', 2)
|
|
13
|
+
|
|
14
|
+
parsed_url = urlsplit(raw_path)
|
|
15
|
+
path = parsed_url.path
|
|
16
|
+
query = parse_qs(parsed_url.query)
|
|
17
|
+
|
|
18
|
+
headers: dict[str, str] = {}
|
|
19
|
+
while True:
|
|
20
|
+
line = self.rfile.readline()
|
|
21
|
+
if line in (b'\r\n', b'\n', b''):
|
|
22
|
+
break
|
|
23
|
+
k, v = line.decode('utf-8').split(':', 1)
|
|
24
|
+
headers[k.strip()] = v.strip()
|
|
25
|
+
|
|
26
|
+
content_length = int(headers.get('Content-Length', 0))
|
|
27
|
+
body = self.rfile.read(content_length) if content_length > 0 else b''
|
|
28
|
+
|
|
29
|
+
return method, path, query, protocol, headers, body
|
|
30
|
+
except Exception:
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
def response(
|
|
34
|
+
self,
|
|
35
|
+
code: int = 200,
|
|
36
|
+
hint: str = 'OK',
|
|
37
|
+
body: str | bytes = b'',
|
|
38
|
+
headers: dict[str, object] | None = None
|
|
39
|
+
) -> bytes:
|
|
40
|
+
res_headers = dict(headers) if headers else {}
|
|
41
|
+
|
|
42
|
+
if not isinstance(body, bytes):
|
|
43
|
+
if isinstance(body, str):
|
|
44
|
+
body = body.encode('utf-8')
|
|
45
|
+
else:
|
|
46
|
+
try:
|
|
47
|
+
body = bytes(body)
|
|
48
|
+
except Exception as e:
|
|
49
|
+
raise ValueError("body must be convertable to bytes.") from e
|
|
50
|
+
|
|
51
|
+
res_headers.setdefault('Content-Length', len(body))
|
|
52
|
+
|
|
53
|
+
header_lines = "".join(f"{k}: {v}\r\n" for k, v in res_headers.items())
|
|
54
|
+
head = f"{self.Protocol} {code} {hint}\r\n{header_lines}\r\n".encode('utf-8')
|
|
55
|
+
return head + body
|
|
56
|
+
|
|
57
|
+
def allowedMethods(self) -> list[str]:
|
|
58
|
+
return [m for m in ('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS') if hasattr(self, m)]
|
|
59
|
+
|
|
60
|
+
def handle(self) -> None:
|
|
61
|
+
res = self._read_request()
|
|
62
|
+
if res is None:
|
|
63
|
+
self.wfile.write(self.response(400, 'Bad Request', 'Bad Request', {'Connection': 'close'}))
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
method, path, query, protocol, headers, body = res
|
|
67
|
+
if not hasattr(self, method):
|
|
68
|
+
allowed = ", ".join(self.allowedMethods())
|
|
69
|
+
self.wfile.write(
|
|
70
|
+
self.response(
|
|
71
|
+
405,
|
|
72
|
+
'Method Not Allowed',
|
|
73
|
+
f'Method not allowed. Available: {allowed or "none"}',
|
|
74
|
+
{'Allow': allowed} if allowed else None
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
return
|
|
78
|
+
|
|
79
|
+
func = getattr(self, method)
|
|
80
|
+
self.wfile.write(func(path, query, protocol, headers, body))
|
|
81
|
+
|
|
82
|
+
class BaseHTTPServer:
|
|
83
|
+
__slots__ = ('server',)
|
|
84
|
+
def __init__(
|
|
85
|
+
self,
|
|
86
|
+
url: str,
|
|
87
|
+
port: int,
|
|
88
|
+
handler: type,
|
|
89
|
+
serverFunc
|
|
90
|
+
) -> None:
|
|
91
|
+
self.server = serverFunc(
|
|
92
|
+
(url, max(0, min(65535, port))),
|
|
93
|
+
handler
|
|
94
|
+
)
|
|
95
|
+
try: self.server.allow_reuse_address = True
|
|
96
|
+
except Exception: pass
|
|
97
|
+
try:
|
|
98
|
+
self.server.serve_forever()
|
|
99
|
+
finally:
|
|
100
|
+
self.server.shutdown()
|
|
101
|
+
self.server.server_close()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class HTTPServer(BaseHTTPServer):
|
|
105
|
+
__slots__ = ()
|
|
106
|
+
def __init__(
|
|
107
|
+
self,
|
|
108
|
+
url: str,
|
|
109
|
+
port: int,
|
|
110
|
+
handler: type
|
|
111
|
+
) -> None:
|
|
112
|
+
super().__init__(url, port, handler, _tcpserv)
|
|
113
|
+
|
|
114
|
+
class ThreadingHTTPServer(BaseHTTPServer):
|
|
115
|
+
__slots__ = ()
|
|
116
|
+
def __init__(
|
|
117
|
+
self,
|
|
118
|
+
url: str,
|
|
119
|
+
port: int,
|
|
120
|
+
handler: type
|
|
121
|
+
) -> None:
|
|
122
|
+
super().__init__(url, port, handler, _thtcpserv)
|
|
123
|
+
|
|
124
|
+
__all__ = ('HTTPServer','ThreadingHTTPServer', 'BaseHTTPRequestHandler')
|
wirepulse/__init__.pyi
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Ryn HTTP module for beginners (but it not value what a advanced users cannot use the htryn.)
|
|
3
|
+
|
|
4
|
+
BaseHTTPRequestHandler: Use it for inheriting the your own classes.
|
|
5
|
+
HTTPServer/ThreadingHTTPServer: Use it for running the your own Request Handler.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from socketserver import StreamRequestHandler as _brh, TCPServer as _tcpserv, ThreadingTCPServer as _thtcpserv
|
|
9
|
+
from typing import SupportsInt, SupportsBytes, NewType, TypeVar, Generic, overload, Literal
|
|
10
|
+
|
|
11
|
+
SupportsStr = NewType('_T', str)
|
|
12
|
+
UrlLike = NewType('_T1', str | tuple[int, int, int, int])
|
|
13
|
+
_T2 = TypeVar('_T2')
|
|
14
|
+
_T3 = TypeVar('_T3')
|
|
15
|
+
AnotherRequestHandler = NewType('_T4', type)
|
|
16
|
+
|
|
17
|
+
class IntegerInRange(int, Generic[_T2, _T3]):
|
|
18
|
+
@overload
|
|
19
|
+
def __gt__(self, other: Literal[0]) -> Literal[True]:
|
|
20
|
+
"Return `True` if it larger than 0."
|
|
21
|
+
@overload
|
|
22
|
+
def __lt__(self, other: Literal[65535]) -> Literal[True]:
|
|
23
|
+
"Return `True` if it smaller than 0."
|
|
24
|
+
|
|
25
|
+
class BaseHTTPRequestHandler(_brh):
|
|
26
|
+
"""
|
|
27
|
+
Base request handler. Create the subclass of this type and write your own methods.
|
|
28
|
+
Example: create a `def GET(self, url: str, query: dict[str, list[str]], protocol: str, headers: dict[str, str], body: bytes) -> bytes`.
|
|
29
|
+
And use internal method `def response(...)` for return HTTP valid response.
|
|
30
|
+
"""
|
|
31
|
+
Protocol: str = "HTTP/1.0"
|
|
32
|
+
"Protocol in response. `HTTP/1.0` by default."
|
|
33
|
+
def allowedMethods(self) -> list[str]:
|
|
34
|
+
"Method for getting allow since 405 error code. Like `Allow: GET, POST, ...`"
|
|
35
|
+
def response(
|
|
36
|
+
self,
|
|
37
|
+
code: SupportsInt = 200,
|
|
38
|
+
hint: SupportsBytes | str = 'OK',
|
|
39
|
+
body: SupportsBytes | str = b'',
|
|
40
|
+
headers: dict[SupportsStr, SupportsStr] = {}
|
|
41
|
+
) -> bytes:
|
|
42
|
+
"Creates a ready HTTP response. Also passing 'Content-Length' (`{'Content-Length': len(body)}`) by default"
|
|
43
|
+
|
|
44
|
+
class HTTPServer:
|
|
45
|
+
"""
|
|
46
|
+
Upping the server, and calling your Request Handler.
|
|
47
|
+
"""
|
|
48
|
+
server: _tcpserv
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
url: UrlLike,
|
|
52
|
+
port: IntegerInRange[0, 65535],
|
|
53
|
+
handler: BaseHTTPRequestHandler | AnotherRequestHandler
|
|
54
|
+
) -> None:
|
|
55
|
+
"""
|
|
56
|
+
Initializes the HTTP server which supports a threading.
|
|
57
|
+
|
|
58
|
+
Url: string like `"(0-255).(0-255).(0-255).(0-255)"` or tuple `(0-255,0-255,0-255,0-255)`
|
|
59
|
+
Port: integer in range 0-65535
|
|
60
|
+
Handler: your Request Handler
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
class ThreadingHTTPServer:
|
|
64
|
+
"""
|
|
65
|
+
Upping the threading server, and calling your Request Handler.
|
|
66
|
+
"""
|
|
67
|
+
server: _tcpserv
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
url: UrlLike,
|
|
71
|
+
port: IntegerInRange[0, 65535],
|
|
72
|
+
handler: BaseHTTPRequestHandler | AnotherRequestHandler
|
|
73
|
+
) -> None:
|
|
74
|
+
"""
|
|
75
|
+
Initializes the HTTP server which supports a threading.
|
|
76
|
+
|
|
77
|
+
Url: string like `"(0-255).(0-255).(0-255).(0-255)"` or tuple `(0-255,0-255,0-255,0-255)`
|
|
78
|
+
Port: integer in range 0-65535
|
|
79
|
+
Handler: your Request Handler
|
|
80
|
+
"""
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wirepulse
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Lightweight HTTP server module
|
|
5
|
+
Author: rozikovskiy
|
|
6
|
+
License: MIT
|
|
7
|
+
Keywords: http,server,socket
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
<div align='center'>
|
|
20
|
+
|
|
21
|
+
# WirePulse
|
|
22
|
+
|
|
23
|
+
Lightweight HTTP server and handler framework built on top of Python's `socketserver` and `urllib.parse`.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
|
|
27
|
+
</div>
|
|
28
|
+
|
|
29
|
+
- Minimal `BaseHTTPRequestHandler` with automatic parsing of HTTP method, path, query string (`dict[str, list[str]]`), headers, and body (`bytes`).
|
|
30
|
+
- Method routing via method names on your handler class (`GET`, `POST`, `PUT`, `DELETE`, `PATCH`, `HEAD`, `OPTIONS`) with automatic `400` and `405` responses.
|
|
31
|
+
- Helper `self.response(code, hint, body, headers)` for building raw HTTP responses with automatic `Content-Length`.
|
|
32
|
+
- Both single-threaded (`HTTPServer`) and multi-threaded (`ThreadingHTTPServer`) server classes.
|
|
33
|
+
|
|
34
|
+
<div align='center'>
|
|
35
|
+
|
|
36
|
+
## Quick Example
|
|
37
|
+
|
|
38
|
+
</div>
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from wirepulse import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class Handler(BaseHTTPRequestHandler):
|
|
45
|
+
def GET(self, path: str, query: dict[str, list[str]], protocol: str, headers: dict[str, str], body: bytes) -> bytes:
|
|
46
|
+
name = query.get("name", ["world"])[0]
|
|
47
|
+
return self.response(
|
|
48
|
+
200,
|
|
49
|
+
"OK",
|
|
50
|
+
f"Hello, {name}! Path: {path}",
|
|
51
|
+
{"Content-Type": "text/plain; charset=utf-8"},
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
def POST(self, path: str, query: dict[str, list[str]], protocol: str, headers: dict[str, str], body: bytes) -> bytes:
|
|
55
|
+
return self.response(
|
|
56
|
+
200,
|
|
57
|
+
"OK",
|
|
58
|
+
body,
|
|
59
|
+
{"Content-Type": "application/octet-stream"},
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
if __name__ == "__main__":
|
|
64
|
+
ThreadingHTTPServer("127.0.0.1", 8010, Handler)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
<div align='center'>
|
|
68
|
+
|
|
69
|
+
## Handler API
|
|
70
|
+
|
|
71
|
+
### Methods to implement
|
|
72
|
+
|
|
73
|
+
</div>
|
|
74
|
+
|
|
75
|
+
Define methods named after uppercase HTTP verbs on your subclass of `BaseHTTPRequestHandler`:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
def GET(self, path, query, protocol, headers, body) -> bytes: ...
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
- `path` (`str`): URL path without query string.
|
|
82
|
+
- `query` (`dict[str, list[str]]`): Parsed query parameters from `urllib.parse.parse_qs`.
|
|
83
|
+
- `protocol` (`str`): Request protocol string from the request line (for example, `HTTP/1.1`).
|
|
84
|
+
- `headers` (`dict[str, str]`): Request headers stripped of surrounding whitespace.
|
|
85
|
+
- `body` (`bytes`): Request payload read according to `Content-Length` (or `b""`).
|
|
86
|
+
|
|
87
|
+
Return value must be `bytes` — typically produced by `self.response(...)`.
|
|
88
|
+
|
|
89
|
+
### `self.response(code=200, hint="OK", body=b"", headers=None) -> bytes`
|
|
90
|
+
Formats a complete HTTP response (`self.Protocol` defaults to `"HTTP/1.0"`):
|
|
91
|
+
- Converts `str` bodies using `utf-8` (or attempts `bytes(body)` for other types).
|
|
92
|
+
- Sets `Content-Length` automatically if not already provided in `headers`.
|
|
93
|
+
|
|
94
|
+
## Servers
|
|
95
|
+
|
|
96
|
+
- `HTTPServer(url, port, handler)` — runs `TCPServer` with `serve_forever()` and cleans up with `shutdown()` and `server_close()`.
|
|
97
|
+
- `ThreadingHTTPServer(url, port, handler)` — runs `ThreadingTCPServer` for concurrent requests.
|
|
98
|
+
|
|
99
|
+
`url` accepts either a string (such as `"0.0.0.0"` or `"192.168.11.244"`) or a 4-tuple of integers like `(127, 0, 0, 1)`. `port` is clamped to `0..65535`.
|
|
100
|
+
|
|
101
|
+
<div align='center'>
|
|
102
|
+
|
|
103
|
+
## Notes & Limitations
|
|
104
|
+
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
- Built for local testing, prototyping, and lightweight custom servers rather than high-load production traffic.
|
|
108
|
+
- `BaseHTTPServer.__init__` starts `serve_forever()` immediately upon instantiation.
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
wirepulse/__init__.py,sha256=_EPenyVZShqKfdygR6uUE4cNFGQ0LnNZpC1pJ6e4uQs,4012
|
|
2
|
+
wirepulse/__init__.pyi,sha256=d0APB6GXqAHAGgSCZBanMcycvwoUAwI6mXU4berlVSM,2958
|
|
3
|
+
wirepulse-0.1.0.dist-info/METADATA,sha256=JZaN-Azq3YYWZDTec-LJir8MmXvcnP-endwqXuGUuV0,3874
|
|
4
|
+
wirepulse-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
5
|
+
wirepulse-0.1.0.dist-info/top_level.txt,sha256=EhBNz3PoYpYsYCoDN6T-RYLkQkP5Edjc8pdp-fP_6jg,10
|
|
6
|
+
wirepulse-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
wirepulse
|