The following pair of scripts is a modified version of a previous post which shows how to set up a TCP connection in Python. The scripts are to be run in two terminal windows on the same machine.
Run the server script and the server will start waiting for client connection using port 5000. Then, you can start a client script in another terminal window to connnect to the server. Once the client connects to the server, you can send the following messages to the server on the client side:
- h - get the help menu from the server.
- c - close the client connection without closing the server.
- x - close both the client and the server.
- Ctrl-C - quit the client abnormally.
- other - send text to the server and get an echo.
Source Code
Server Side
# TCP server example
import socket, time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(1) # listen to at most 1 client
server_socket.settimeout(5) # timeout for server socket = 5 sec.
mEndServer = False
while not mEndServer:
print "Waiting for call..."
try:
client_socket, address = server_socket.accept()
except socket.timeout:
if mEndServer:
break
continue
print "I got a connection from ", address
# handle a client connection
while True:
mdata = client_socket.recv(512)
if mdata=='':
# client is quit abnormally
print 'Client is quit.'
break
elif mdata=='c':
# close client without
# ending server
print "Client is closed\n"
break
elif mdata=='x':
# ending server
print "Server is closed\n"
mEndServer = True
break
elif mdata=='h':
# help
print 'Client calls for help'
mMsg = '\nc - close client connection\n'
mMsg += 'h - print help message\n'
mMsg += 'x - turn off server\n'
client_socket.send(mMsg)
elif mdata!='':
print "RECEIVED from client:" , mdata
mMsg = "Your data is %s\n" % mdata
client_socket.send(mMsg)
client_socket.close()
server_socket.close()
Client Side
# TCP client example
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 5000))
while True:
data = raw_input("Message sent to server:")
client_socket.send(data)
if data == 'x':
# tell server to close
break
if data == 'c':
# close client only
break
if data == '':
# close client only
break
print "Waiting for server to feed back..."
data = client_socket.recv(512)
print "RECEIVED from server:" , data
client_socket.close()
If you want to connect to the server with a different machine on the network, you should change localhost in the client script to the IP of the server (say 192.168.0.80).
Output
Server Side
Waiting for call...
I got a connection from ('127.0.0.1', 48448)
RECEIVED from client: hello
RECEIVED from client: how are you?
RECEIVED from client: can you see me?
Client calls for help
Client is closed
Waiting for call...
I got a connection from ('127.0.0.1', 48450)
Server is closed
Client SideMessage sent to server:hello Waiting for server to feed back... RECEIVED from server: Your data is hello Message sent to server:how are you? Waiting for server to feed back... RECEIVED from server: Your data is how are you? Message sent to server:can you see me? Waiting for server to feed back... RECEIVED from server: Your data is can you see me? Message sent to server:h Waiting for server to feed back... RECEIVED from server: c - close client connection h - print help message x - turn off server Message sent to server:c
# python c.py Message sent to server:x
No comments:
Post a Comment