summaryrefslogtreecommitdiff
path: root/tests/circuitpython-manual/socketpool/server
diff options
context:
space:
mode:
authorDavePutz <dwputz@gmail.com>2021-02-24 10:09:52 -0600
committerGitHub <noreply@github.com>2021-02-24 10:09:52 -0600
commiteeb89a97b4a8fa25fb38e4a5d5db78c0c3df9b15 (patch)
treee18ea9f61c7930d1f21d72fb69125a0270558038 /tests/circuitpython-manual/socketpool/server
parentcec921f3747beccff10c2d2dfbc237c305083f62 (diff)
parente41137c745af65b6342c7e2d3b09d8930eefedb0 (diff)
Merge pull request #41 from adafruit/main
Update from adafruit main
Diffstat (limited to 'tests/circuitpython-manual/socketpool/server')
-rw-r--r--tests/circuitpython-manual/socketpool/server/cpy-server.py31
-rw-r--r--tests/circuitpython-manual/socketpool/server/host-client.py17
-rw-r--r--tests/circuitpython-manual/socketpool/server/readme.md43
3 files changed, 91 insertions, 0 deletions
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..b482755d8
--- /dev/null
+++ b/tests/circuitpython-manual/socketpool/server/host-client.py
@@ -0,0 +1,17 @@
+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
+```