Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import json
- def handle_client(client_socket):
- # Receive the data from the client.
- data = client_socket.recv(1024)
- # Decode the data as JSON.
- json_data = json.loads(data.decode())
- # Process the JSON data.
- print(json_data)
- # Send a response to the client.
- response = json.dumps({"message": "Hello, world!"})
- client_socket.sendall(response.encode())
- def main():
- # Create a socket.
- server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- # Bind the socket to a port.
- server_socket.bind(("localhost", 8080))
- # Listen for incoming connections.
- server_socket.listen(1)
- while True:
- # Accept an incoming connection.
- client_socket, _ = server_socket.accept()
- # Handle the client connection.
- handle_client(client_socket)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement