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

This python script will open up a socket on port 15200 and start listening for the connections. Once a client is connected it shall print the IP address of the connected client and as well as the port number of the client. The server will listen for any data transmitted by the client & when data is received it will be printed. 

 

import SocketServer
from threading import Thread
class service(SocketServer.BaseRequestHandler):
          def handle(self):
                   clientData = ”
                   print “\n Client connected with IP: “, self.client_address
                   while 1:
                                 clientData = self.request.recv(1024)
                                 print “Client says: ” + clientData
                                if not clientData: break
                  print “\n Client exited”
                  self.request.close()
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
            pass
t = ThreadedTCPServer((”,15200), service)
t.serve_forever()

——————————————————————————————————————————————————————

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. 

import socket
import time
host = “127.0.0.1”
port = 15200
clientData = “Hello Server”
def startConn():
                           try:
                                  myClientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                                  print “Trying remote port: “,port
                                 myClientSocket.connect((‘127.0.0.1’,port))
                                 print “Connected to remote port: “,port
                                 myClientSocket.send(clientData)
                                 time.sleep(3)
                                 myClientSocket.close()
                    except:
                                 startConn()
startConn()

Testing

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.

Thanks 🙂

For more articles from this author: https://trybe.one/members/prab636/blog


Your Remaining Votes (within 24hrs) : 10 of 10
10 votes, average: 5.00 out of 510 votes, average: 5.00 out of 510 votes, average: 5.00 out of 510 votes, average: 5.00 out of 510 votes, average: 5.00 out of 5 (10 votes, average: 5.00 out of 5)
You need to be a registered member to rate this.
(1721 total tokens earned)
Loading...

Responses