File size: 648 Bytes
0fb68a4
 
b664970
 
 
 
644fdcd
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
print("HI")

import os
import socket

# get hf space repo id in format "user/space"
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 the tcp url
print(f"tcp://{host}:{port}")

# setup tcp server
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()