Загрузка данных


import RPi.GPIO as GPIO
import socket
import threading

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

led_pins = [4, 17, 27, 16, 20, 21, 12, 5, 6, 13, 19, 26]
for pin in led_pins:
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.LOW)

def handle_client(conn):
    request = conn.recv(1024).decode()
    print("Request:", request[:100])
    
    if '/led_on/' in request:
        pin = int(request.split('/led_on/')[1].split()[0])
        if pin in led_pins:
            GPIO.output(pin, GPIO.HIGH)
            response = "LED ON"
    elif '/led_off/' in request:
        pin = int(request.split('/led_off/')[1].split()[0])
        if pin in led_pins:
            GPIO.output(pin, GPIO.LOW)
            response = "LED OFF"
    elif '/all_on' in request:
        for pin in led_pins:
            GPIO.output(pin, GPIO.HIGH)
        response = "All LEDs ON"
    elif '/all_off' in request:
        for pin in led_pins:
            GPIO.output(pin, GPIO.LOW)
        response = "All LEDs OFF"
    else:
        response = """<html>
        <body>
            <h1>LED Control</h1>
            {}
        </body>
        </html>""".format(''.join([f'<a href="/led_on/{pin}">LED {pin} ON</a> | <a href="/led_off/{pin}">OFF</a><br>' for pin in led_pins]) + 
                          '<br><a href="/all_on">ALL ON</a> | <a href="/all_off">ALL OFF</a>')
    
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    conn.close()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 8080))
s.listen(5)
print("Server running on port 8080")

while True:
    conn, addr = s.accept()
    threading.Thread(target=handle_client, args=(conn,)).start()




>>> %Run WEB.py
Server running on port 8080
Request: GET / HTTP/1.1
Host: 192.168.31.53:8080
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User
Exception in thread Thread-1 (handle_client):
Traceback (most recent call last):
  File "/usr/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/usr/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminnii/Desktop/11/WEB.py", line 44, in handle_client
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Request: GET / HTTP/1.1
Host: 192.168.31.53:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-
Exception in thread Thread-2 (handle_client):
Traceback (most recent call last):
  File "/usr/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/usr/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminnii/Desktop/11/WEB.py", line 44, in handle_client
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Request: GET / HTTP/1.1
Host: 192.168.31.53:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-
Exception in thread Thread-3 (handle_client):
Traceback (most recent call last):
  File "/usr/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/usr/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminnii/Desktop/11/WEB.py", line 44, in handle_client
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Request: GET / HTTP/1.1
Host: 192.168.31.53:8080
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User
Exception in thread Thread-4 (handle_client):
Traceback (most recent call last):
  File "/usr/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/usr/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminnii/Desktop/11/WEB.py", line 44, in handle_client
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Request: GET / HTTP/1.1
Host: 192.168.31.53:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-
Exception in thread Thread-5 (handle_client):
Traceback (most recent call last):
  File "/usr/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/usr/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminnii/Desktop/11/WEB.py", line 44, in handle_client
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'
Request: GET / HTTP/1.1
Host: 192.168.31.53:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-
Exception in thread Thread-6 (handle_client):
Traceback (most recent call last):
  File "/usr/lib/python3.13/threading.py", line 1043, in _bootstrap_inner
    self.run()
    ~~~~~~~~^^
  File "/usr/lib/python3.13/threading.py", line 994, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/adminnii/Desktop/11/WEB.py", line 44, in handle_client
    conn.send("HTTP/1.1 200 OK\nContent-Type: text/html\n\n" + response)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'str'