THost / app.py
Staticaliza's picture
Update app.py
fdcd9d0 verified
raw
history blame
1.1 kB
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()