Untitled

mail@pastecode.io avatar
unknown
plain_text
4 months ago
1.3 kB
2
Indexable
import socket


def start_server(host='127.0.0.1', port=65432):
    state = None  # This will store the received byte as a string
    error_flag = False

    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
        server_socket.bind((host, port))
        server_socket.listen()
        print(f"Server listening on {host}:{port}...")

        conn, addr = server_socket.accept()
        with conn:
            print(f"Connected by {addr}")
            while True:
                data = conn.recv(1)  # Read a single byte
               #if not data:
                #    break

                received_char = data.decode('utf-8')
                ascii_value = ord(received_char)

                # Check if the character is between 'A' and 'G'
                if 'A' <= received_char <= 'G':
                    State = received_char
                    print(f"State updated to: {State}")
                else:
                    error_flag = True
                    print("Error: Received invalid character.")

                # For demonstration purposes, let's send a response back
                if error_flag:
                    response = "Error: Invalid character received."
                else:
                    response = f"State set to: {State}"

                conn.sendall(response.encode('utf-8'))
Leave a Comment