Home > python > A basic socket client server example

A basic socket client server example

Here I present a basic socket client server example. It can be used as a starting point for a more serious project.

Problem
You want to have a server that is listening on a port. The server can receive data and these pieces of data must be processed one after the other. That is, the server must manage a queue and the received data are put in this queue. The elements in the queue must be processed one by one.

We also need a client whose job is to send data to the server.

(1) Common config part

# config.py
PORT=3030

The port number won’t be repeated in the server and in the client. Instead, it is read from a config file.

(2) The server

#!/usr/bin/env python
# server.py

import socket
import select
import config as cfg
import Queue
from threading import Thread
from time import sleep
from random import randint
import sys

class ProcessThread(Thread):
    def __init__(self):
        super(ProcessThread, self).__init__()
        self.running = True
        self.q = Queue.Queue()

    def add(self, data):
        self.q.put(data)

    def stop(self):
        self.running = False

    def run(self):
        q = self.q
        while self.running:
            try:
                # block for 1 second only:
                value = q.get(block=True, timeout=1)
                process(value)
            except Queue.Empty:
                sys.stdout.write('.')
                sys.stdout.flush()
        #
        if not q.empty():
            print "Elements left in the queue:"
            while not q.empty():
                print q.get()

t = ProcessThread()
t.start()

def process(value):
    """
    Implement this. Do something useful with the received data.
    """
    print value
    sleep(randint(1,9))    # emulating processing time

def main():
    s = socket.socket()         # Create a socket object
    host = socket.gethostname() # Get local machine name
    port = cfg.PORT                # Reserve a port for your service.
    s.bind((host, port))        # Bind to the port
    print "Listening on port {p}...".format(p=port)

    s.listen(5)                 # Now wait for client connection.
    while True:
        try:
            client, addr = s.accept()
            ready = select.select([client,],[], [],2)
            if ready[0]:
                data = client.recv(4096)
                #print data
                t.add(data)
        except KeyboardInterrupt:
            print
            print "Stop."
            break
        except socket.error, msg:
            print "Socket error! %s" % msg
            break
    #
    cleanup()

def cleanup():
    t.stop()
    t.join()

#########################################################

if __name__ == "__main__":
    main()

We create a thread that is running in the background and manages the queue. In a loop it checks if there is an element in the queue. If yes, then it takes out the first element and processes it.

In the main function we start listening on a given port. Incoming data are passed to the thread, where the thread puts it in the queue.

You can stop the server with CTRL+C. The cleanup method stops the thread nicely: it changes a variable to False, thus the thread quits from its infinite loop. Notice the parameters of q.get: we block for 1 second only. This way we have a chance to stop the thread even if the queue is empty. With t.join() we wait until it completely stops.

(3) The client

#!/usr/bin/env python
# client.py

import config as cfg
import sys
import socket

def main(elems):
    try:
        for e in elems:
            client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            host = socket.gethostname()
            client.connect((host, cfg.PORT))
            client.send(e)
            client.shutdown(socket.SHUT_RDWR)
            client.close()
    except Exception as msg:
        print msg

#########################################################

if __name__ == "__main__":
    main(sys.argv[1:])

Command line parameters are passed one by one to the server to be processed.

Usage
Start the server in a terminal:

$ ./server.py 
Listening on port 3030...
........

Start the client in another terminal and pass some values to the server:

$ ./client.py 1 5 8

Switch back to the server:

$ ./server.py 
Listening on port 3030...
............1
5
8
....

Related links

[ discussion @reddit ]

About these ads
Categories: python Tags: , ,
You must be logged in to post a comment.
Follow

Get every new post delivered to your Inbox.

Join 61 other followers

%d bloggers like this: