add example using serversocket from https://docs.python.org/2/library/socketserver.html
This commit is contained in:
21
socketserver/TcpClient.py
Normal file
21
socketserver/TcpClient.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import socket
|
||||
import sys
|
||||
|
||||
HOST, PORT = "localhost", 9999
|
||||
data = " ".join(sys.argv[1:])
|
||||
|
||||
# Create a socket (SOCK_STREAM means a TCP socket)
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
|
||||
try:
|
||||
# Connect to server and send data
|
||||
sock.connect((HOST, PORT))
|
||||
sock.sendall(bytes(data + "\n"))
|
||||
|
||||
# Receive data from the server and shut down
|
||||
received = str(sock.recv(1024));
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
print("Sent: {}".format(data))
|
||||
print("Received: {}".format(received))
|
||||
Reference in New Issue
Block a user