From 43dd21ce221d68fc81407a20466c0c69248976ed Mon Sep 17 00:00:00 2001 From: Dan Jackson Date: Fri, 13 Jan 2023 16:39:20 -0800 Subject: [PATCH 1/5] Update SimpleServer example to accept >1 connection This moves the `accept()` call into the `while True` loop, leaving the server accepting connections as long as the `server` socket is valid. --- examples/wiznet5k_simpleserver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index 548af79..1d6771b 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -28,8 +28,8 @@ server.bind((server_ip, server_port)) # Bind to IP and Port server.listen() # Begin listening for incoming clients -conn, addr = server.accept() # Wait for a connection from a client. while True: + conn, addr = server.accept() # Wait for a connection from a client. with conn: data = conn.recv(1024) if data: # Wait for receiving data From c5208fb01bd37c93e692e30ca8568e6fad63d01f Mon Sep 17 00:00:00 2001 From: Dan Jackson Date: Fri, 20 Jan 2023 22:43:47 -0800 Subject: [PATCH 2/5] Adding some print messages inside server loop --- examples/wiznet5k_simpleserver.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index 1d6771b..2392ebf 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -29,9 +29,12 @@ server.listen() # Begin listening for incoming clients while True: + print(f"Accepting connections on {server_ip}:{server_port}") conn, addr = server.accept() # Wait for a connection from a client. + print(f"Connection accepted from {addr}, reading exactly 1024 bytes from client") with conn: data = conn.recv(1024) if data: # Wait for receiving data print(data) conn.send(data) # Echo message back to client + print("Connection closed") From f4364f1097250597e80458b7151482bf3119cb2b Mon Sep 17 00:00:00 2001 From: Dan Jackson Date: Fri, 20 Jan 2023 22:55:21 -0800 Subject: [PATCH 3/5] Adding client for simpleserver from @anecdata --- examples/client_for_wiznet5k_simpleserver.py | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 examples/client_for_wiznet5k_simpleserver.py diff --git a/examples/client_for_wiznet5k_simpleserver.py b/examples/client_for_wiznet5k_simpleserver.py new file mode 100755 index 0000000..3f616f5 --- /dev/null +++ b/examples/client_for_wiznet5k_simpleserver.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import socket +import time + +print("A simple client for the wiznet5k_simpleserver.py example in this directory") +print("Run this on any device connected to the same network as the server, after editing this script with the correct HOST & PORT\n") +# Or, use any TCP-based client that can easily send 1024 bytes. For example: +# python -c 'print("1234"*256)' | nc 192.168.10.1 50007 + + +# edit host and port to match server +HOST = "192.168.10.1" +PORT = 50007 +TIMEOUT = 10 +INTERVAL = 5 +MAXBUF = 1024 + +while True: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(TIMEOUT) + print(f"Connecting to {HOST}:{PORT}") + s.connect((HOST, PORT)) + # wiznet5k_simpleserver.py wants exactly 1024 bytes + size = s.send(b'A5'*512) + print("Sent", size, "bytes") + buf = s.recv(MAXBUF) + print('Received', buf) + s.close() + time.sleep(INTERVAL) From 5003ca3cf38b042db39b648fd73922234178fc6c Mon Sep 17 00:00:00 2001 From: Dan Jackson Date: Fri, 20 Jan 2023 23:03:55 -0800 Subject: [PATCH 4/5] Splitting long line for linter --- examples/client_for_wiznet5k_simpleserver.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/client_for_wiznet5k_simpleserver.py b/examples/client_for_wiznet5k_simpleserver.py index 3f616f5..2a3b95e 100755 --- a/examples/client_for_wiznet5k_simpleserver.py +++ b/examples/client_for_wiznet5k_simpleserver.py @@ -3,7 +3,8 @@ import time print("A simple client for the wiznet5k_simpleserver.py example in this directory") -print("Run this on any device connected to the same network as the server, after editing this script with the correct HOST & PORT\n") +print("Run this on any device connected to the same network as the server, after " + "editing this script with the correct HOST & PORT\n") # Or, use any TCP-based client that can easily send 1024 bytes. For example: # python -c 'print("1234"*256)' | nc 192.168.10.1 50007 From 8ea6a1778c292935c815632f4bd96816f4d73dba Mon Sep 17 00:00:00 2001 From: foamyguy Date: Mon, 23 Jan 2023 16:28:08 -0600 Subject: [PATCH 5/5] license info for new file. use dhcp instaed of static. rename new example. --- ...iznet5k_cpython_client_for_simpleserver.py} | 18 ++++++++++++++---- examples/wiznet5k_simpleserver.py | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) rename examples/{client_for_wiznet5k_simpleserver.py => wiznet5k_cpython_client_for_simpleserver.py} (64%) diff --git a/examples/client_for_wiznet5k_simpleserver.py b/examples/wiznet5k_cpython_client_for_simpleserver.py similarity index 64% rename from examples/client_for_wiznet5k_simpleserver.py rename to examples/wiznet5k_cpython_client_for_simpleserver.py index 2a3b95e..ae89d8b 100755 --- a/examples/client_for_wiznet5k_simpleserver.py +++ b/examples/wiznet5k_cpython_client_for_simpleserver.py @@ -1,10 +1,20 @@ +# SPDX-FileCopyrightText: 2023 ladyada +# +# SPDX-License-Identifier: MIT #!/usr/bin/env python3 + +""" +This example client runs on CPython and connects to / sends data to the +simpleserver example. +""" import socket import time print("A simple client for the wiznet5k_simpleserver.py example in this directory") -print("Run this on any device connected to the same network as the server, after " - "editing this script with the correct HOST & PORT\n") +print( + "Run this on any device connected to the same network as the server, after " + "editing this script with the correct HOST & PORT\n" +) # Or, use any TCP-based client that can easily send 1024 bytes. For example: # python -c 'print("1234"*256)' | nc 192.168.10.1 50007 @@ -22,9 +32,9 @@ print(f"Connecting to {HOST}:{PORT}") s.connect((HOST, PORT)) # wiznet5k_simpleserver.py wants exactly 1024 bytes - size = s.send(b'A5'*512) + size = s.send(b"A5" * 512) print("Sent", size, "bytes") buf = s.recv(MAXBUF) - print('Received', buf) + print("Received", buf) s.close() time.sleep(INTERVAL) diff --git a/examples/wiznet5k_simpleserver.py b/examples/wiznet5k_simpleserver.py index 2392ebf..82a2cc3 100644 --- a/examples/wiznet5k_simpleserver.py +++ b/examples/wiznet5k_simpleserver.py @@ -18,12 +18,12 @@ spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) # Initialize ethernet interface -eth = WIZNET5K(spi_bus, cs, is_dhcp=False) +eth = WIZNET5K(spi_bus, cs, is_dhcp=True) # Initialize a socket for our server socket.set_interface(eth) server = socket.socket() # Allocate socket for the server -server_ip = "192.168.10.1" # IP address of server +server_ip = eth.pretty_ip(eth.ip_address) # IP address of server server_port = 50007 # Port to listen on server.bind((server_ip, server_port)) # Bind to IP and Port server.listen() # Begin listening for incoming clients