Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,36 @@
|
|
1 |
-
|
2 |
-
import socket
|
3 |
-
import threading
|
4 |
-
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
5 |
-
import ssl
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
def start_https():
|
10 |
-
https_port = int(os.getenv("HTTPS_PORT", "8443"))
|
11 |
-
# create ssl context
|
12 |
-
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
13 |
-
context.load_cert_chain("cert.pem", "key.pem")
|
14 |
-
httpd = HTTPServer(("0.0.0.0", https_port), SimpleHTTPRequestHandler)
|
15 |
-
httpd.socket = context.wrap_socket(httpd.socket, server_side=True)
|
16 |
-
print(f"https listening on 0.0.0.0:{https_port}")
|
17 |
-
httpd.serve_forever()
|
18 |
-
|
19 |
-
thread = threading.Thread(target=start_https, daemon=True)
|
20 |
-
thread.start()
|
21 |
-
|
22 |
-
repo_id = os.getenv("REPO_ID", "spaceslab/thost")
|
23 |
-
user, space = repo_id.split("/")
|
24 |
port = int(os.getenv("PORT", "5678"))
|
25 |
-
host = f"{space}.{user}.hf.space"
|
26 |
|
27 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
38 |
conn.close()
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os, socket, threading, sys
|
|
|
|
|
|
|
3 |
|
4 |
+
host = "0.0.0.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
port = int(os.getenv("PORT", "5678"))
|
|
|
6 |
|
7 |
+
print("listening on", host, port)
|
8 |
+
print("hostname:", socket.gethostname())
|
9 |
+
try:
|
10 |
+
print("local ips:", socket.gethostbyname_ex(socket.gethostname())[2])
|
11 |
+
except Exception as e:
|
12 |
+
print("ip lookup error:", e)
|
13 |
+
sys.stdout.flush()
|
14 |
|
15 |
+
def handle_client(conn, addr):
|
16 |
+
print("client connected:", addr)
|
17 |
+
try:
|
18 |
+
while True:
|
19 |
+
data = conn.recv(1024)
|
20 |
+
if not data:
|
21 |
+
break
|
22 |
+
print("received:", data.decode(errors="ignore").strip())
|
23 |
+
conn.sendall(data)
|
24 |
+
except Exception as err:
|
25 |
+
print("error:", err)
|
26 |
+
finally:
|
27 |
conn.close()
|
28 |
+
print("client disconnected:", addr)
|
29 |
+
|
30 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv:
|
31 |
+
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
32 |
+
srv.bind((host, port))
|
33 |
+
srv.listen()
|
34 |
+
while True:
|
35 |
+
conn, addr = srv.accept()
|
36 |
+
threading.Thread(target=handle_client, args=(conn, addr), daemon=True).start()
|