adafruit / adafruit/Adafruit_CircuitPython_HTTPServer

Connection refused after some time/multiple requests on Wiznet5k Pico W5100S

Open
#41 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
52
Forks
30
PR merge metrics
No merged PRs in 30d

Description

Adafruit CircuitPython 8.0.3 on 2023-02-23; Raspberry Pi Pico with rp2040
I am building HTTP server with W5100S and can't get this library to work properly because of adafruit_wiznet5k_socket, i think. I cut some code to demonstrate the problem, so if you need explanation in something, i'll explain
**code.py**
```
import board
import busio
import digitalio
import time

import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
from adafruit_httpserver.request import HTTPRequest
from adafruit_httpserver.response import HTTPResponse
from adafruit_httpserver.server import HTTPServer
from adafruit_httpserver.methods import HTTPMethod

IP_ADDRESS = (192, 168, 1, 244)
SUBNET_MASK = (255, 255, 255, 0)
GATEWAY_ADDRESS = (192, 168, 1, 1)
DNS_SERVER = (8, 8, 8, 8)
W5x00_RSTn = board.GP20
ethernetRst = digitalio.DigitalInOut(W5x00_RSTn)
ethernetRst.direction = digitalio.Direction.OUTPUT
ethernetRst.value = False
time.sleep(1)
ethernetRst.value = True

SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17
spi_bus = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)

cs = digitalio.DigitalInOut(SPI0_CSn)
eth = WIZNET5K(spi_bus, cs, is_dhcp=False)

def routes(server):
@server.route("/", HTTPMethod.POST)
def base(request: HTTPRequest):
raw_text = request.body.decode("UTF-8")
print(raw_text)
with open('index.html', 'r') as f:
html_string = f.read()
f.close()
with HTTPResponse(request) as response:
response.send(html_string, content_type="text/html")

@server.route("/", HTTPMethod.GET)
def base(request: HTTPRequest):
with open('index.html', 'r') as f:
html_string = f.read()
f.close()
with HTTPResponse(request) as response:
response.send(html_string, content_type="text/html")

def raise_serv():
eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)

socket.set_interface(eth)
server_ip = eth.pretty_ip(eth.ip_address)

print(f"Listening on http://{server_ip}:80")
return HTTPServer(socket), server_ip

while True:
print("Raising server..")
server, server_ip = raise_serv()
routes(server)
server.start(str(server_ip))
print("Server raised.")
while True:
try:
server.poll()
except OSError as error:
print(error)
continue
```
**index.html**
```




Document

body {
margin: 0;
}
.wrapper {
background: #ffffff;
box-sizing: border-box;
margin: 0 auto;
max-width: 1300px;
min-height: 100vh;
}
.container {
display: grid;
grid-template-columns: 340px repeat(6, auto);
}
.el {
padding: 2px;
font-size: 17px;
}
input {
text-align: right;
}
.btn-apply {
text-align: right;
padding: 15px 2px;
}
.row-border{
border-top: 1px solid rgb(191, 252, 198);
grid-column: 1 / 8;
}
.square {
position: relative;
padding-left: 17px;
margin-left: 5px;
}
.square:after {
position:absolute;
content:"";
width:17px;
height:17px;
top:2px;
left:0;
}
.square-green {
background-color:rgb(0, 190, 0);
}
.square-red {
background-color:rgb(190, 0, 0);
}





1

2

3

4

5

6










1

1

1

1

1

1






































1

2

3

4

5

6




a


```
After first request, server stops responding after some time, giving "ERR_CONNECTION_REFUSED" in browsers. Also, multiple POST requests(2 fast clicks on button) are doing the same, but faster. I've done some tests with different systems, that gave me no results. The only thing, that helped me is "Incognito" mode. Other versions of CircuitPython don't help.
When I interrupt programm with CTRL+C it gives.
```
File "/lib/adafruit_httpserver/response.py", line 167, in send
File "/lib/adafruit_httpserver/response.py", line 242, in _send_bytes
File "/lib/adafruit_wiznet5k/adafruit_wiznet5k_socket.py", line 422, in send
File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 1034, in socket_write
File "/lib/adafruit_wiznet5k/adafruit_wiznet5k.py", line 614, in write
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the supplied code.py reproduction and follow the traceback through adafruit_httpserver/response.py send and _send_bytes into adafruit_wiznet5k/adafruit_wiznet5k_socket.py send and adafruit_wiznet5k.py socket_write/write. Reproduce the failure with repeated GET and POST requests, then verify that the server remains responsive after those requests without ERR_CONNECTION_REFUSED.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.