Python Forum

Full Version: multi-threaded tcp server-newbie
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am a newbie and starting to learn python. I have a task of developing multi-threaded server which can handle upto 3 clients. I have looked at different forums and undoubtedly there are zillion ways to write compact code. But I am looking for the code organization
as follows:
My clients A,B and C connect to port 1000, 2000 and 3000 respectively

Server should connect and handle threads such as

Thread1:
Connect to A
Read data from A
Process data
Send to B

Thread2:
Connect to B
Read data from B
Process data
Send to c

Thread3:
Connect to C
Read data from C
Process data
Send to A

If the code could be organized in the above fashion, it would be easier for me.

Could anybody provide pointers?
gary Wrote:Could anybody provide pointers?
We are glad to help, but please show us what you have tried so far.
I could not get started. Still learning. Essentially looking for concurrent servers which are each bound to different ports
example: Server IP: 10.100.x.x, ports: 10000, 20000 and say 30000
If you are "a newbie and starting to learn python", do you not think that having "a task of developing multi-threaded server which can handle up to 3 clients" is a little ahead of your skill set?
I am a newbie in python. I have experience with C and have developed modest TCP server with winsock. Yes, I am not a full time programmer, but pointers would certainly help. Ok I found some code on the web
How to convert the below code as parallel 2 servers, one server with IP: 10.203.xx.yy and port 3000 and second is 10.203.xx.yy with port 4000

# import socket programming library
import socket
 
# import thread module
from _thread import *
import threading
 
print_lock = threading.Lock()
 
# thread function
def threaded(c):
    while True:
 
        # data received from client
        data = c.recv(1024)
        
        if not data:
            print('Bye')
        # lock released on exit
            print_lock.release()
            break
 
        # reverse the given string from client
        data = data[::-1]
         
        # send back reversed string to client
        c.send(data)
        
 
    # connection closed
    c.close()
    



def Main():
    host = ""
 
    # reserve a port on your computer
    # in our case it is 12345 but it
    # can be anything
    port = 3000
    port1 = 4000 // I Need another instance of server that connects to PORT1
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    s.bind(("10.203.3.16", port))
    print("socket binded to port", port)
 
    # put the socket into listening mode
    s.listen(5)
    print("socket is listening")
 
    # a forever loop until client wants to exit
    while True:
 
        # establish connection with client
        c, addr = s.accept()
         
        # lock acquired by client
        print_lock.acquire()
        print('Connected to :', addr[0], ':', addr[1])
 
        # Start a new thread and return its identifier
        start_new_thread(threaded, (c,))
        
        
    s.close()
    
 
 
if __name__ == '__main__':
    Main()
I always look for the simplest solution for any given task, as in "Occam's razor".

So, if I needed two http servers, running on two different ports, my solution for this would be to simply run two instances of http.server from a command line:

python3 -m http.server 8000

python3 -m http.server 9000

Now I have two http servers; one on port 8000 and one on port 9000

Would this approach work for you?

I should add (in case you've not come across feature before) that what is 'served' is the contents of whatever is in the file directory, from which said server is started.
How do I assign the IP address (btw, I am not developing web server) and how do I read data and redirect to other clients using the http.server?
The ip address is that of the host machine. I don't understand what you mean by "how do I read data and redirect to other clients using the http.server?"

Okay, so you're not wanting a http server, but you've never stated what kind of a server you are wanting.

Btw, this method can be used as a simple file server, by not having any .html files in the file directory: a client would simply connect using a web browser and click to download any file. I've not tried to connect using a ftp client (such as FileZilla), but it's an interesting point, so I will try that and see what happens.

To add..

As suspected, it can't be used with a ftp client; it's a http sever not a ftp server.
I am building a client server model ( towards laboratory automation) to exchange data. In my application there are 3 servers that need to run concurently and each server is connected to one port. I have 3 clients that connect to respective server. I cannot change this specification by any means.
But you've still not said what kind of a server. ftp? http? https? pop3?
Pages: 1 2