wisp-python 0.8.3__py3-none-any.whl → 0.9.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.
wisp/server/cli.py CHANGED
@@ -4,12 +4,24 @@ import pathlib
4
4
  import sys
5
5
  import logging
6
6
  import multiprocessing
7
+ import threading
8
+ import platform
9
+
10
+ try:
11
+ import concurrent.interpreters
12
+ has_subinterpreters = True
13
+ except ImportError:
14
+ has_subinterpreters = False
7
15
 
8
16
  try:
9
17
  import uvloop
10
- use_uvloop = True
18
+ event_loop = "uvloop"
11
19
  except ImportError:
12
- use_uvloop = False
20
+ try:
21
+ import winloop
22
+ event_loop = "winloop"
23
+ except ImportError:
24
+ event_loop = "asyncio"
13
25
 
14
26
  import wisp
15
27
  from wisp.server import http
@@ -17,12 +29,11 @@ from wisp.server import net
17
29
 
18
30
  def run_async(func, *args, **kwargs):
19
31
  try:
20
- if use_uvloop:
32
+ if event_loop == "uvloop":
21
33
  uvloop.run(func(*args, **kwargs))
34
+ elif event_loop == "winloop":
35
+ winloop.run(func(*args, **kwargs))
22
36
  else:
23
- #uvloop doesn't support windows at all so we don't need to print the error
24
- if not sys.platform in ("win32", "cygwin"):
25
- logging.error("Importing uvloop failed. Falling back to asyncio, which is slower.")
26
37
  asyncio.run(func(*args, **kwargs))
27
38
  except KeyboardInterrupt:
28
39
  pass
@@ -30,6 +41,34 @@ def run_async(func, *args, **kwargs):
30
41
  def run_http(args):
31
42
  run_async(http.main, args)
32
43
 
44
+ def run_processes(thread_count, args):
45
+ processes = []
46
+ for i in range(0, int(thread_count)):
47
+ process = multiprocessing.Process(target=run_http, args=(args,), daemon=True)
48
+ processes.append(process)
49
+ process.start()
50
+ try:
51
+ for process in processes:
52
+ process.join()
53
+ except KeyboardInterrupt:
54
+ pass
55
+
56
+ def run_subinterpreters(thread_count, args):
57
+ def thread_func():
58
+ interpreter = concurrent.interpreters.create()
59
+ interpreter.call(run_http, args)
60
+
61
+ threads = []
62
+ for i in range(0, int(thread_count)):
63
+ thread = threading.Thread(target=thread_func, daemon=True)
64
+ threads.append(thread)
65
+ thread.start()
66
+ try:
67
+ for thread in threads:
68
+ thread.join()
69
+ except KeyboardInterrupt:
70
+ pass
71
+
33
72
  def main():
34
73
  parser = argparse.ArgumentParser(
35
74
  prog="wisp-server-python",
@@ -66,24 +105,22 @@ def main():
66
105
  logging.info("enabled rate limits")
67
106
  if args.proxy:
68
107
  logging.info(f"proxy enabled: {args.proxy}")
108
+ logging.info(f"running on {platform.python_implementation()} {platform.python_version()}")
69
109
  logging.info(f"listening on {args.host}:{args.port}")
70
110
 
111
+ if event_loop == "asyncio":
112
+ logging.error("failed to import uvloop or winloop. falling back to asyncio, which is slower.")
113
+
71
114
  threads = int(args.threads)
72
115
  if net.reuse_port_supported():
73
116
  if threads == 0:
74
117
  threads = multiprocessing.cpu_count()
75
118
  logging.info(f"running using {threads} threads")
76
-
77
- processes = []
78
- for i in range(0, int(threads)):
79
- process = multiprocessing.Process(target=run_http, args=(args,), daemon=True)
80
- processes.append(process)
81
- process.start()
82
- try:
83
- for process in processes:
84
- process.join()
85
- except KeyboardInterrupt:
86
- pass
119
+
120
+ if has_subinterpreters:
121
+ run_subinterpreters(threads, args);
122
+ else:
123
+ run_processes(threads, args)
87
124
 
88
125
  else:
89
126
  if threads != 0:
wisp/server/http.py CHANGED
@@ -3,6 +3,7 @@ import pathlib
3
3
  import mimetypes
4
4
  import logging
5
5
  import random
6
+ import textwrap
6
7
 
7
8
  from websockets.server import serve
8
9
 
@@ -11,6 +12,57 @@ from wisp.server import connection
11
12
  from wisp.server import ratelimit
12
13
  from wisp.server import net
13
14
 
15
+ static_path = None
16
+ default_html = f"""
17
+ <!DOCTYPE html>
18
+ <html lang="en">
19
+ <head>
20
+ <meta charset="utf-8">
21
+ <meta name="viewport" content="width=device-width">
22
+ <title>wisp-server-python v{wisp.version}</title>
23
+ <style>
24
+ html {{
25
+ color-scheme: light dark;
26
+ }}
27
+ h1, p {{
28
+ font-family: sans-serif;
29
+ }}
30
+ body {{
31
+ max-width: 600px;
32
+ margin-left: auto;
33
+ margin-right: auto;
34
+ }}
35
+ pre {{
36
+ white-space: pre-wrap
37
+ }}
38
+ </style>
39
+ </head>
40
+ <body>
41
+ <h1>wisp-server-python</h1>
42
+ <p>This is a <a href="https://github.com/MercuryWorkshop/wisp-protocol">Wisp protocol</a> server running
43
+ <a href="https://github.com/MercuryWorkshop/wisp-server-python">wisp-server-python</a> v{wisp.version}.</p>
44
+ <p>This program is licensed under the <a href="https://github.com/MercuryWorkshop/wisp-server-python/blob/main/LICENSE">GNU AGPL v3</a>.</p>
45
+ <pre>
46
+ wisp-server-python: a Wisp server implementation written in Python
47
+ Copyright (C) 2025 Mercury Workshop
48
+
49
+ This program is free software: you can redistribute it and/or modify
50
+ it under the terms of the GNU Affero General Public License as published by
51
+ the Free Software Foundation, either version 3 of the License, or
52
+ (at your option) any later version.
53
+
54
+ This program is distributed in the hope that it will be useful,
55
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
56
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
57
+ GNU Affero General Public License for more details.
58
+
59
+ You should have received a copy of the GNU Affero General Public License
60
+ along with this program. If not, see &lt;https://www.gnu.org/licenses/&gt;.
61
+ </pre>
62
+ </body>
63
+ </html>
64
+ """.strip()
65
+
14
66
  async def connection_handler(websocket, path):
15
67
  client_ip = websocket.remote_address[0]
16
68
  if client_ip == "127.0.0.1" and "X-Real-IP" in websocket.request_headers:
@@ -37,9 +89,14 @@ async def connection_handler(websocket, path):
37
89
  tcp_handler = asyncio.create_task(wsproxy_conn.handle_tcp())
38
90
  await asyncio.gather(ws_handler, tcp_handler)
39
91
 
40
- async def static_handler(path, request_headers):
92
+ async def request_handler(path, request_headers):
41
93
  if "Upgrade" in request_headers:
42
94
  return
95
+ if not static_path:
96
+ if path.endswith("/") or path.endswith("/index.html"):
97
+ return 200, [("Content-Type", "text/html")], default_html.encode()
98
+ else:
99
+ return 404, [], "404 not found".encode()
43
100
 
44
101
  response_headers = []
45
102
  target_path = static_path / path[1:]
@@ -62,10 +119,7 @@ async def main(args):
62
119
 
63
120
  if args.static:
64
121
  static_path = pathlib.Path(args.static).resolve()
65
- request_handler = static_handler
66
122
  mimetypes.init()
67
- else:
68
- request_handler = None
69
123
 
70
124
  if args.limits:
71
125
  ratelimit.enabled = True
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: wisp-python
3
- Version: 0.8.3
3
+ Version: 0.9.0
4
4
  Summary: An implementation of a Wisp proxy server in Python
5
5
  Author: ading2210
6
6
  Project-URL: Homepage, https://github.com/MercuryWorkshop/wisp-server-python
@@ -16,6 +16,7 @@ Requires-Dist: websockets
16
16
  Requires-Dist: asyncudp
17
17
  Requires-Dist: python-socks[asyncio]
18
18
  Requires-Dist: uvloop; sys_platform != "win32" and sys_platform != "cygwin"
19
+ Requires-Dist: winloop; sys_platform == "win32" or sys_platform == "cygwin"
19
20
  Dynamic: license-file
20
21
 
21
22
  # Python Wisp Server
@@ -2,14 +2,14 @@ wisp/__init__.py,sha256=02Fv0s_jJG8U4y5B6AzKPIOzxAuqJyY_So8Guz5bdTk,75
2
2
  wisp/util.py,sha256=AU7si8VwJqS8Dwc1PT2pu_f_9Y32GQxivRYwJ3-U0YQ,144
3
3
  wisp/server/__init__.py,sha256=LmgQ1nTlnqcTPqRJVc1NGapV9qVR-BK8iPVUMSJ74Qs,102
4
4
  wisp/server/__main__.py,sha256=zhDqmhOY9HgHx-BOq-kU5IoxZCMJwt_PEEEISaZr9Mk,68
5
- wisp/server/cli.py,sha256=4cCcs9rDkELPcufVMbdJ3DATJ7z7ThnEAX9sJ8IEWFo,3526
5
+ wisp/server/cli.py,sha256=pkpl_ojw_M3QH4FlTlBe5QQh17brgYS86fQJA_9ks-o,4408
6
6
  wisp/server/connection.py,sha256=0ztt3TejF0fY3eXtkTn9QozOj0_x-KOPC4vXberUsrQ,7665
7
- wisp/server/http.py,sha256=wEEpxhiodf6E4cacuRYm-WAzp7wZDqDloeESfjSSPu4,3460
7
+ wisp/server/http.py,sha256=Oq0byjMSrTHIvyQ0upNvby4Awu8S-F6C3rliE2PZ7lI,5339
8
8
  wisp/server/net.py,sha256=_Uxv9w1DCCcJO7VaSOfxQhM5PYpwrYm3Hd1XDQ5F_n8,3362
9
9
  wisp/server/ratelimit.py,sha256=cic_rlBaM-tp6kJaSz4ITGvUbsX4JLSGSUgF5cPO0FY,1926
10
- wisp_python-0.8.3.dist-info/licenses/LICENSE,sha256=DZak_2itbUtvHzD3E7GNUYSRK6jdOJ-GqncQ2weavLA,34523
11
- wisp_python-0.8.3.dist-info/licenses/LICENSE_third_party.md,sha256=_YgCpygAXxOtr7gpjRTv7W1DTqZU_f7IMwjdPvw4VxY,15360
12
- wisp_python-0.8.3.dist-info/METADATA,sha256=MpSHgqlNJcE15LB5NBalFApFpkGkC8P8ezTomQQnJYQ,4184
13
- wisp_python-0.8.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
14
- wisp_python-0.8.3.dist-info/top_level.txt,sha256=sv0LDAu8cHUc3_VTsCKfXU-6B9HdV4dW22ubeW8KPVs,5
15
- wisp_python-0.8.3.dist-info/RECORD,,
10
+ wisp_python-0.9.0.dist-info/licenses/LICENSE,sha256=DZak_2itbUtvHzD3E7GNUYSRK6jdOJ-GqncQ2weavLA,34523
11
+ wisp_python-0.9.0.dist-info/licenses/LICENSE_third_party.md,sha256=_YgCpygAXxOtr7gpjRTv7W1DTqZU_f7IMwjdPvw4VxY,15360
12
+ wisp_python-0.9.0.dist-info/METADATA,sha256=_SdbGaR4zVKLuWqpUMq2Se95VdtKxatHIb5gOONqc08,4260
13
+ wisp_python-0.9.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
14
+ wisp_python-0.9.0.dist-info/top_level.txt,sha256=sv0LDAu8cHUc3_VTsCKfXU-6B9HdV4dW22ubeW8KPVs,5
15
+ wisp_python-0.9.0.dist-info/RECORD,,