aboutsummaryrefslogtreecommitdiff
path: root/tests/circuitpython-manual/socketpool/client
diff options
context:
space:
mode:
Diffstat (limited to 'tests/circuitpython-manual/socketpool/client')
-rw-r--r--tests/circuitpython-manual/socketpool/client/cpy-client.py26
-rw-r--r--tests/circuitpython-manual/socketpool/client/host-server.py27
-rw-r--r--tests/circuitpython-manual/socketpool/client/readme.md46
3 files changed, 99 insertions, 0 deletions
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..fd7ceb7f1
--- /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))
diff --git a/tests/circuitpython-manual/socketpool/client/readme.md b/tests/circuitpython-manual/socketpool/client/readme.md
new file mode 100644
index 000000000..45cdf23c8
--- /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'
+```