File size: 1,100 Bytes
b664970
 
752afed
 
 
 
 
 
 
fdcd9d0
752afed
 
 
 
 
 
 
 
 
 
 
 
b664970
644fdcd
b664970
 
 
 
 
 
 
 
 
 
fdcd9d0
b664970
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import socket
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler
import ssl

print("HI")

def start_https():
    https_port = int(os.getenv("HTTPS_PORT", "8443"))
    httpd = HTTPServer(("0.0.0.0", https_port), SimpleHTTPRequestHandler)
    httpd.socket = ssl.wrap_socket(
        httpd.socket,
        certfile="cert.pem",
        keyfile="key.pem",
        server_side=True
    )
    print(f"https listening on 0.0.0.0:{https_port}")
    httpd.serve_forever()

thread = threading.Thread(target=start_https, daemon=True)
thread.start()

repo_id = os.getenv("REPO_ID", "spaceslab/thost")
user, space = repo_id.split("/")
port = int(os.getenv("PORT", "5678"))
host = f"{space}.{user}.hf.space"

print(f"tcp://{host}:{port}")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("0.0.0.0", port))
sock.listen()
print(f"listening on 0.0.0.0:{port}")

while True:
    conn, addr = sock.accept()
    print("connection from", addr)
    data = conn.recv(1024)
    if not data:
        conn.close()
        continue
    conn.sendall(data)
    conn.close()