The code below is trying to send an API POST request to a Shelly Plug S, setting it up to run on my network without having to open every single device's config site or pair with their native app.
import http.client
ssid = input("WiFi SSID: ")
pwd = input("Password: ")
conn = http.client.HTTPSConnection("192.168.33.1", "80")
payload = 'ssid='+ssid+'&key='+pwd+'&ipv4_method=dhcp&enabled=true'
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
conn.request("POST", "/settings/sta", payload, headers)
res = conn.getresponse()
data = res.read()
print(data. Decode("utf-8"))
Problem is, [Errno 104] is thrown "Connection reset by peer" traceback:
Traceback (most recent call last):
File "/home/smarthome/shelly.py", line 12, in <module>
conn.request("POST", "/settings/sta", payload, headers)
File "/usr/lib/python3.10/http/client.py", line 1282, in request
File "/usr/lib/python3.10/http/client.py", line 1328, in _send_request
File "/usr/lib/python3.10/http/client.py", line 1277, in endheaders
File "/usr/lib/python3.10/http/client.py", line 1037, in _send_output
File "/usr/lib/python3.10/http/client.py", line 975, in send
File "/usr/lib/python3.10/http/client.py", line 1454, in connect
File "/usr/lib/python3.10/ssl.py", line 512, in wrap_socket
File "/usr/lib/python3.10/ssl.py", line 1070, in _create
File "/usr/lib/python3.10/ssl.py", line 1341, in do_handshake
ConnectionResetError: [Errno 104] Connection reset by peer
Sending the request from Postman, the command goes out and it does as I ask, it connects to my WiFi. (In fact, the python code is generated by Postman from the working request).
Update: Running the python script (with basically no changes) now throws a new error,
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it