Small Python Client/Server Application
Client
#!/usr/bin/env python
import socket
TCP_IP = '10.0.0.10'
TCP_PORT = 21
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data
Server
#!/usr/bin/env python
import socket
TCP_IP = '10.0.0.14'
TCP_PORT = 21
BUFFER_SIZE = 20 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connection address:', addr
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
print "received data:", data
conn.send(data) # echo
conn.close()
Start Server
cisco@server-2:~$ sudo python server.py
Start Client Side
cisco@server-1:~$ sudo python client.py
received data: Hello, World!
cisco@server-1:~$
Result on Server Side
cisco@server-2:~$ sudo python server.py
Connection address: ('10.0.0.6', 38522)
received data: Hello, World!
cisco@server-2:~$