Path: blob/main/python/python-wasm/data/socket/server.py
1067 views
"""12Run this via34python-wasm server.py56NOTE: I haven't implemented checking for signals when blocking on7posix-node yet, so you have to hit control+Z then kill the job,8rather than control+C.910"""1112import socket1314# TODO: ipv6 is not supported yet in posix-node/src/socket.zig.15# See comments there and use this code to test it:16#s = socket.create_server(("localhost", 2000), family=socket.AF_INET6)1718# TODO: when I try "" for the address then "wildcard resolved to multiple address"19# is hit in socketmodule.c, probably due to some option not being20# supported properly in getaddrinfo.21#s = socket.create_server(("", 2000), family=socket.AF_INET)2223s = socket.create_server(("localhost", 2000))24s.listen(1)2526print("listening on port 2000")2728SEND = b"Hello\n"29while True:30print("Waiting for connection...")31conn, addr = s.accept()32print("Accepted connection", conn, addr)33try:34import time; time.sleep(0.25)35print("Sending ", SEND)36conn.send(SEND)37print("Receiving...")38print(conn.recv(6))39conn.close()40except Exception as e:41print(e)424344