1 import socket 2 import sys 3 4 HOST, PORT = "localhost", 8000 5 data = " ".join(sys.argv[1:]) 6 7 # Create a socket (SOCK_STREAM means a TCP socket) 8 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 10 try: 11 # Connect to server and send data 12 sock.connect((HOST, PORT)) 13 sock.sendall(bytes(data + "\n", "utf-8")) 14 15 # Receive data from the server and shut down 16 received = str(sock.recv(1024), "utf-8") 17 finally: 18 sock.close() 19 20 print("Sent: {}".format(data)) 21 print("Received: {}".format(received))
时间: 2024-11-08 23:25:06