In this article, I would like to share a basic Client-Server application coded in Python language.
A Client-Server application involves a server and a client. A server is basically a system who has some resources that are made available for the clients. The clients usually connect to a server to utilize the resources available on the server. Python is powerful yet fun.
Server Side Code
from threading import Thread
clientData = ”
print “\n Client connected with IP: “, self.client_address
clientData = self.request.recv(1024)
print “Client says: ” + clientData
if not clientData: break
print “\n Client exited”
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
t = ThreadedTCPServer((”,15200), service)
Client Side Code
The client will try to connect to the server on port 15200. I am using 127.0.0.1 (loopback) IP address intentionally so that I can simulate the connection on the same machine. Upon successfully connecting to the server, the client will send “Hello Server” data to the server. The Client will wait for 3 seconds and then close the connection.
host = “127.0.0.1”
port = 15200
clientData = “Hello Server”
myClientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print “Trying remote port: “,port
print “Connected to remote port: “,port
You could run these scripts on the same machine for the testing purpose. First, run the Server script & then run the Client script. Once the Client is connected to the Server, the command window or shell will show the output as shown below, in this case, we see that the Client connected from 127.0.0.1 IP address and Client’s port number is 7445:
Server Side Output
On the Client side window you shall see the following output, in this case, we could see that the Client connected to the port 15200 of the Server.
Client Side Output
I hope you find this helpful! Please feel free to ask questions or provide your suggestion or opinions.
For more articles from this author: https://trybe.one/members/prab636/blog
Your Remaining Votes (within 24hrs) : 10 of 10