0

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

1 Answer 1

1

Won't mark this as an answer as it's really more of a workaround but I dithched python and just used wget in a shell script:

#!/bin/sh

WIFINAME=$1
PWD=$2

 wget --quiet \
  --method POST \
  --header 'Accept: */*' \
  --header 'Content-Type: application/x-www-form-urlencoded' \
  --body-data "ssid=$WIFINAME&key=$PWD&ipv4_method=dhcp&enabled=true" \
  --output-document \
  - http://192.168.33.1/settings/sta
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.