Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/data/socket/server.py
1067 views
1
"""
2
3
Run this via
4
5
python-wasm server.py
6
7
NOTE: I haven't implemented checking for signals when blocking on
8
posix-node yet, so you have to hit control+Z then kill the job,
9
rather than control+C.
10
11
"""
12
13
import socket
14
15
# TODO: ipv6 is not supported yet in posix-node/src/socket.zig.
16
# See comments there and use this code to test it:
17
#s = socket.create_server(("localhost", 2000), family=socket.AF_INET6)
18
19
# TODO: when I try "" for the address then "wildcard resolved to multiple address"
20
# is hit in socketmodule.c, probably due to some option not being
21
# supported properly in getaddrinfo.
22
#s = socket.create_server(("", 2000), family=socket.AF_INET)
23
24
s = socket.create_server(("localhost", 2000))
25
s.listen(1)
26
27
print("listening on port 2000")
28
29
SEND = b"Hello\n"
30
while True:
31
print("Waiting for connection...")
32
conn, addr = s.accept()
33
print("Accepted connection", conn, addr)
34
try:
35
import time; time.sleep(0.25)
36
print("Sending ", SEND)
37
conn.send(SEND)
38
print("Receiving...")
39
print(conn.recv(6))
40
conn.close()
41
except Exception as e:
42
print(e)
43
44