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/client/cpy-client.py | 26 ++++++++++++ .../socketpool/client/host-server.py | 27 +++++++++++++ .../socketpool/client/readme.md | 46 ++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 tests/circuitpython-manual/socketpool/client/cpy-client.py create mode 100644 tests/circuitpython-manual/socketpool/client/host-server.py create mode 100644 tests/circuitpython-manual/socketpool/client/readme.md (limited to 'tests/circuitpython-manual/socketpool/client') diff --git a/tests/circuitpython-manual/socketpool/client/cpy-client.py b/tests/circuitpython-manual/socketpool/client/cpy-client.py new file mode 100644 index 000000000..dcc742c9f --- /dev/null +++ b/tests/circuitpython-manual/socketpool/client/cpy-client.py @@ -0,0 +1,26 @@ +import wifi +import socketpool +import ssl +import time + +TIMEOUT = None +HOST = '192.168.10.179' +PORT = 5000 + +# Connect to wifi +print("Connecting to wifi") +wifi.radio.connect("mySSID", "myPASS") +pool = socketpool.SocketPool(wifi.radio) + +print("Creating Socket") +with pool.socket(pool.AF_INET, pool.SOCK_STREAM) as s: + s.settimeout(TIMEOUT) + + print("Connecting") + s.connect((HOST, PORT)) + print("Sending") + sent = s.send(b'Hello, world') + print("Receiving") + buff = bytearray(128) + numbytes = s.recv_into(buff) +print(repr(buff)) diff --git a/tests/circuitpython-manual/socketpool/client/host-server.py b/tests/circuitpython-manual/socketpool/client/host-server.py new file mode 100644 index 000000000..4a5faf2a0 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/client/host-server.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +import socket + +TIMEOUT = 10 +HOST = "192.168.10.179" +PORT = 5000 + +print("Create Socket") +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(TIMEOUT) + s.bind((HOST, PORT)) + s.listen() + print("Accepting connections") + while True: + try: + conn, addr = s.accept() + break + except BlockingIOError: + pass + + with conn: + s.settimeout(TIMEOUT) + print('Connected by', addr) + data = conn.recv(128) + print("got: " + str(data)) + conn.sendall(data) + print("sent: " + str(data)) \ No newline at end of file diff --git a/tests/circuitpython-manual/socketpool/client/readme.md b/tests/circuitpython-manual/socketpool/client/readme.md new file mode 100644 index 000000000..1ef069531 --- /dev/null +++ b/tests/circuitpython-manual/socketpool/client/readme.md @@ -0,0 +1,46 @@ +# Circuitpython as Client + +This example demonstrates the use of Socket as a client, accessing a server on a host development machine. This Circuitpython sketch uses the Connect, Send, and Recv_Into methods. + +## Prerequisites + +Circuitpython V6.2.0 minimum. Neither the host or client sketch has installed module prerequisites. + +## Setup + +Find a viable IP address for the host machine first and insert it in both sketches as HOST. On mac, this can be done by going to System Preferences/Network and checking the IP address used to connect to the local wireless network. Make sure that both devices are using the same WIFI! + +Each sketch can have Timeout values changed. The host sketch usually needs a value above 0, or the recv() will fail. Currently, Circuitpython's Connect function is always blocking, so changing the client timeout will not cause much change in behavior. + +Start the Server on the host PC first, within this folder: + +``` +python host-server.py +``` + +Then, reload the client sketch in Circuitpython. + +## Expected Behavior + +The example should connect to a server running on the host machine. The client will send a "Hello world" string to the server, which will return it. + +Expected client output: + +``` +Connecting to wifi +Creating Socket +Connecting +Sending +Receiving +bytearray(b'Hello, world') +``` + +Expected Server output (IP/port values will vary): + +``` +Create Socket +Accepting connections +Connected by ('192.168.10.128', 64509) +got: b'Hello, world' +sent: b'Hello, world' +``` \ No newline at end of file -- 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/client') 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