From 59429cdcbed8b88e80179c8701dd2ea529b12f19 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Wed, 17 Feb 2021 11:41:23 -0500 Subject: Move manual tests and rename --- .../socketpool/server/cpy-server.py | 31 ++++++++++++++++ .../socketpool/server/host-client.py | 18 +++++++++ .../socketpool/server/readme.md | 43 ++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 tests/circuitpython-manual/socketpool/server/cpy-server.py create mode 100644 tests/circuitpython-manual/socketpool/server/host-client.py create mode 100644 tests/circuitpython-manual/socketpool/server/readme.md (limited to 'tests/circuitpython-manual/socketpool/server') diff --git a/tests/circuitpython-manual/socketpool/server/cpy-server.py b/tests/circuitpython-manual/socketpool/server/cpy-server.py new file mode 100644 index 000000000..d865156bc --- /dev/null +++ b/tests/circuitpython-manual/socketpool/server/cpy-server.py @@ -0,0 +1,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]]) diff --git a/tests/circuitpython-manual/socketpool/server/host-client.py b/tests/circuitpython-manual/socketpool/server/host-client.py new file mode 100644 index 000000000..b5aacbfa8 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/server/host-client.py @@ -0,0 +1,18 @@ +import socket + +HOST = '192.168.10.128' # The server's hostname or IP address +PORT = 80 # The port used by the server + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(None) + + print("Connecting") + s.connect((HOST, PORT)) + + print("Sending") + s.send(b'Hello, world') + + print("Receiving") + data = s.recv(1024) + print('Received', repr(data)) + diff --git a/tests/circuitpython-manual/socketpool/server/readme.md b/tests/circuitpython-manual/socketpool/server/readme.md new file mode 100644 index 000000000..26118b8f1 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/server/readme.md @@ -0,0 +1,43 @@ +# Circuitpython as Client + +This example demonstrates the use of Socket as a server, accessed by a client program on a host development machine. This Circuitpython sketch uses the Bind, Listen, Accept, and recvfrom_into calls. + +## Prerequisites + +Circuitpython V6.2.0 minimum. Neither the host or client sketch has installed module prerequisites. + +## Setup + +Make sure that both devices are using the same WIFI! The Circuitpython Server will automatically pick an IP and print it over the CDC monitor. Copy this value into the host client sketch. Start the Server on Circuitpython first, then run the client sketch in this folder: + +``` +python host-client.py +``` + +Each sketch can have Timeout values changed. + +## Expected behavior + +The host machine will connect to the server running on the Circuitpython device, and send a "Hello, world" string which is then returned. + +Expected client output: + +``` +Connecting +Sending +Receiving +Received b'Hello, world' +``` + +Expected server output: +``` +Connecting to Wifi +Finding IP address +192.168.10.128 +Creating socket +Accepting connections +Connected by ('192.168.10.179', 33274) +Receiving +bytearray(b'Hello, world') +Sending +``` -- cgit v1.2.3 From e77981f86e53eb5a985e89e03cc984e5b318da37 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Thu, 18 Feb 2021 16:26:14 -0500 Subject: Fix file ends for CI --- tests/circuitpython-manual/socketpool/client/host-server.py | 2 +- tests/circuitpython-manual/socketpool/client/readme.md | 2 +- tests/circuitpython-manual/socketpool/server/host-client.py | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) (limited to 'tests/circuitpython-manual/socketpool/server') diff --git a/tests/circuitpython-manual/socketpool/client/host-server.py b/tests/circuitpython-manual/socketpool/client/host-server.py index 4a5faf2a0..fd7ceb7f1 100644 --- a/tests/circuitpython-manual/socketpool/client/host-server.py +++ b/tests/circuitpython-manual/socketpool/client/host-server.py @@ -24,4 +24,4 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: data = conn.recv(128) print("got: " + str(data)) conn.sendall(data) - print("sent: " + str(data)) \ No newline at end of file + print("sent: " + str(data)) diff --git a/tests/circuitpython-manual/socketpool/client/readme.md b/tests/circuitpython-manual/socketpool/client/readme.md index 1ef069531..45cdf23c8 100644 --- a/tests/circuitpython-manual/socketpool/client/readme.md +++ b/tests/circuitpython-manual/socketpool/client/readme.md @@ -43,4 +43,4 @@ Accepting connections Connected by ('192.168.10.128', 64509) got: b'Hello, world' sent: b'Hello, world' -``` \ No newline at end of file +``` diff --git a/tests/circuitpython-manual/socketpool/server/host-client.py b/tests/circuitpython-manual/socketpool/server/host-client.py index b5aacbfa8..b482755d8 100644 --- a/tests/circuitpython-manual/socketpool/server/host-client.py +++ b/tests/circuitpython-manual/socketpool/server/host-client.py @@ -15,4 +15,3 @@ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: print("Receiving") data = s.recv(1024) print('Received', repr(data)) - -- cgit v1.2.3