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

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

  1. 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