summaryrefslogtreecommitdiff
path: root/tests/circuitpython-manual/socketpool/server/cpy-server.py
blob: bce1f9d6a2bb558396c7f684a27831823c759d6b (plain)
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
import wifi
import socketpool

TIMEOUT = None

print("Connecting to Wifi")
wifi.radio.connect("mySSID", "myPASS")

pool = socketpool.SocketPool(wifi.radio)

print("Finding IP address")
print(wifi.radio.ipv4_address)
HOST = str(wifi.radio.ipv4_address)
PORT = 80  # Port to listen on

print("Creating socket")
sock = pool.socket(pool.AF_INET, pool.SOCK_STREAM)

sock.bind((HOST, PORT))
sock.listen(1)
print("Accepting connections")
conn, addr = sock.accept()
with conn:
    print("Connected by", addr)
    buff = bytearray(128)
    print("Receiving")
    numbytes = conn.recvfrom_into(buff)
    print(buff[: numbytes[0]])
    if numbytes:
        print("Sending")
        conn.send(buff[: numbytes[0]])