In this tutorial you will learn about in network programming. You will learn about the client-server model that is in use for the World Wide Web, E-mail and many other applications.
Client server (with email protocol)
The client server model is a model where there are n clients and one server. The clients make data requests to a server. The server replies to those messages received. A client can be any device such as your computer or tablet. Servers are generally dedicated computers which are to be connected 24/7.
Related course: If you prefer a course or certification:
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
This client simply mimics the behavior we did in Telnet.
Limitations of the server code The server code above can only interact with one client. If you try to connect with a second terminal it simply won’t reply to the new client. To let the server interact with multiple clients you need to use multi-threading. We rebuild the server script to accept multiple client connections:
#!/usr/bin/env python import socket from threading import Thread from SocketServer import ThreadingMixIn
def__init__(self,ip,port): Thread.__init__(self) self.ip = ip self.port = port print"[+] New thread started for "+ip+":"+str(port)
defrun(self): whileTrue: data = conn.recv(2048) ifnot data: break print"received data:", data conn.send(data) # echo TCP_IP = '0.0.0.0' TCP_PORT = 62 BUFFER_SIZE = 20# Normally 1024, but we want fast response
So far we have simply sent messages back and forth. Every message can have a specific meaning in an application. This is known as the protocol. The meaning of these messages must be the same on both the sender and receiver side. The Transport Layer below makes sure that messages are received (TCP). The Internet Layer is the IPv4 protocol. All we have to define is the Application Layer.
Below we modified the server to accept simple commands (We use the non-threading server for simplicity). We changed the port to 64. Server code with a protocol:
#!/usr/bin/env python import socket TCP_IP = '127.0.0.1' TCP_PORT = 64 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 while1: data = conn.recv(BUFFER_SIZE) ifnot data: break print"received data:", data #conn.send(data) # echo if"/version"in data: conn.send("Demo versionn")
if"/echo"in data: data = data.replace("/echo","") conn.send(data + "n")
Run the server with:
sudo python server.py
A client can then connect with telnet (make sure you pick the right IP):
$ telnet 127.0.0.164 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is'^]'. message /version Demo version /echo Repeat this Repeat this