Advertisement
sashaatx

Untitled

Jun 23rd, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import socket
  2. import json
  3.  
  4. def handle_client(client_socket):
  5. # Receive the data from the client.
  6. data = client_socket.recv(1024)
  7.  
  8. # Decode the data as JSON.
  9. json_data = json.loads(data.decode())
  10.  
  11. # Process the JSON data.
  12. print(json_data)
  13.  
  14. # Send a response to the client.
  15. response = json.dumps({"message": "Hello, world!"})
  16. client_socket.sendall(response.encode())
  17.  
  18. def main():
  19. # Create a socket.
  20. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  21.  
  22. # Bind the socket to a port.
  23. server_socket.bind(("localhost", 8080))
  24.  
  25. # Listen for incoming connections.
  26. server_socket.listen(1)
  27.  
  28. while True:
  29. # Accept an incoming connection.
  30. client_socket, _ = server_socket.accept()
  31.  
  32. # Handle the client connection.
  33. handle_client(client_socket)
  34.  
  35. if __name__ == "__main__":
  36. main()
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement