circuitpython/tests/circuitpython-manual/socketpool/server/cpy-server.py

32 lines
702 B
Python
Raw Normal View History

2021-02-11 17:36:40 -05:00
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)
2021-03-15 09:57:36 -04:00
PORT = 80 # Port to listen on
2021-02-11 17:36:40 -05:00
print("Creating socket")
2021-03-15 09:57:36 -04:00
sock = pool.socket(pool.AF_INET, pool.SOCK_STREAM)
2021-02-11 17:36:40 -05:00
sock.bind((HOST, PORT))
sock.listen(1)
print("Accepting connections")
conn, addr = sock.accept()
with conn:
2021-03-15 09:57:36 -04:00
print("Connected by", addr)
2021-02-11 17:36:40 -05:00
buff = bytearray(128)
print("Receiving")
numbytes = conn.recvfrom_into(buff)
2021-03-15 09:57:36 -04:00
print(buff[: numbytes[0]])
2021-02-11 17:36:40 -05:00
if numbytes:
print("Sending")
2021-03-15 09:57:36 -04:00
conn.send(buff[: numbytes[0]])