Python: Socket programming server-client application using threads

Have been building some small modules in Python recently. While have been working there, got a requirement on Socket programming. Hence this is the post to demonstrate a basic example of server-client application.

The Clients basically opens a socket connection and send queries to the server. The Server responds back. This design also takes care to continue periodical data push and pull. It’s useful to push data periodically so that the same client doesn’t need to open connection repeatedly.



To run this, open a terminal and enter
$ python server.py

Watch this terminal to understand the flow. Now open multiple clients and send messages.
$ python client.py
Enter 'quit' to exit
-> Anthony Gonsalves
->


$ python client.py
Enter 'quit' to exit
-> Kuntal Chandra
-> quit
$


$ python client.py
Enter 'quit' to exit
-> Chandra Kuntal
-> Brute Force
->


The interaction with the Server and output will be like this


$ python server.py
Socket created
Socket now listening
Connected with 127.0.0.1:37384
Processing the input received from client
Processed result: Hello ANTHONY GONSALVES
Connected with 127.0.0.1:37388
Processing the input received from client
Processed result: Hello KUNTAL CHANDRA
Connected with 127.0.0.1:37394
Processing the input received from client
Processed result: Hello CHANDRA KUNTAL
Processing the input received from client
Client is requesting to quit
Connection 127.0.0.1:37388 closed
Processing the input received from client
Processed result: Hello BRUTE FORCE



Threads are the most common way of handling multiple socket connections and clients. However, there are also other techniques to achieve the same e.g. Polling. In polling, the socket API will continuously check a bunch of sockets for some activity or event. If there is an event in one or multiple sockets, the process returns to the application the list of sockets on which the events occurred. Will come back on the same in detail in another post.

Some references to build this design: Socket — Low-level networking interface, Simple socket server in Python, Python 3 socket programming example, Simple socket server-client application in Python 3, Python 3 – Network Programming.

Advertisements

21 thoughts on “Python: Socket programming server-client application using threads

  1. thank you for the article
    can you help me with a little hint tho
    i need the clients to see the old msg of other clients when first connecting along with the time when they were sent
    thank you

    Like

  2. How to terminate the server in graceful way? If I type quit only client exits but the server still runs.. how to terminate the server if I want within the code?

    Like

    • In this kind of server-client design, the receiver is always supposed to be available to listen from the senders. That’s why we use supervisor or something like that to keep the listener available in a fail-safe manner. That’s the core purpose. If you still want to shut down the server then the design needs to be different and I can’t think such a practical use case. Could you give me an example use case?

      Like

      • Lets say I am running this server as a service in another machine/server. And for some reason, I don’t want this service to be running anymore in that machine. Then there should be some provision from outside to close this server without logging into the actual machine and then killing the process. e.g. if I send terminate from my client the server should get terminated in a graceful way. Hope this makes it clear about the use case.

        Like

      • The easiest option I can see is to trigger `soc.close()` and then `sys.exit()` when the server receives a specific input from the client.

        But again I would say that’s not the design I kept in the example. Think the server is shutting down due to the shut down request from a specific client but meanwhile some other clients’ requests were coming in and some others were in progress. It won’t be a fail-safe mechanism for a distributed server -> clients architecture.

        Like

  3. I have a situation where n server are connected to n clients, (let’s say, 1 server is connected to 2 clients) on the same machine and when the connection is established it should show the name of client to which the server is connected to, in the csv file(which i already have with other information) with a new column (connected with). for eg,
    id connected to
    s1 c1,c2
    s2 c1
    c1 s1,s2
    How can i do this?

    Like

    • I think you’re trying to establish a many to many connections. That’s quite normal in this kind of socket programming. To get the client name, you simply do something like this `host = socket.gethostname()`

      Like

  4. i have client computer and server computer which are connected with LAN cable. client computer has client.py file and server computer has server.py file when i am running the files in both computer then my client computer sending the text message to server computer that is good. But i want that when i run the client program the server computer get the text message in a pop box or in cmd (means i dont want to run separately server.py file in server computer it will automaitcally run when i run the client.py file) how i can do this.

    Like

  5. Many thanks to you, I’ve been trying to find a way to use multiple clients on a single ip and this is the only piece of code that actually works, I’ve been comparing a lot of them and yours is actually well coded and it works in python 3.7. I tried many scripts but their threading system never worked and I had problems with passing args, basically it was a pain in the butt. Yours was clear enough so I could understand something at least and I would like to thank you !

    Like

      • I tried the exact same code.
        $ python server.py
        Socket created
        Socket now listening

        $ python client.py
        Enter ‘quit’ to exit
        -> Hello
        Traceback (most recent call last):
        File “stest.py”, line 28, in
        main()
        File “stest.py”, line 16, in main
        message = input(” -> “)
        File “”, line 1, in
        NameError: name ‘Hello’ is not defined

        $ python server.py
        Connected with 127.0.0.1:43116
        Processing the input received from client
        Processed result: Hello
        Processing the input received from client
        Processed result: Hello
        Exception in thread Thread-1:
        Traceback (most recent call last):
        File “/usr/lib/python2.7/threading.py”, line 801, in __bootstrap_inner
        self.run()
        File “/usr/lib/python2.7/threading.py”, line 754, in run
        self.__target(*self.__args, **self.__kwargs)
        File “ctest.py”, line 56, in client_thread
        connection.sendall(“-“.encode(“utf8”))
        File “/usr/lib/python2.7/socket.py”, line 228, in meth
        return getattr(self._sock,name)(*args)
        error: [Errno 32] Broken pipe

        Like

      • 1. Asked you whether the port is open or not? I can see that you’re using port 43116. Can you check its availability via terminal?
        2. I see the environment is 2.7. I’ve tested my code only in Python3. You might need to test in an upgraded environment.
        3. If you’re running client.py and server.py then what is stest.py?

        Like

      • Anyway thanks for the example. I was able to replicate the usage of threads and create multiple clients in my program. Thank you! Cheers 🙂

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s