标签:
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))
标签:
原文地址:http://www.cnblogs.com/liuaqu/p/5277626.html