Python Forum
multi-threaded tcp server-newbie
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multi-threaded tcp server-newbie
#1
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?
Reply
#2
gary Wrote:Could anybody provide pointers?
We are glad to help, but please show us what you have tried so far.
rob101 likes this post
Reply
#3
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
Reply
#4
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?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
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()
Larz60+ write Oct-29-2022, 08:32 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#6
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#7
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?
Reply
#8
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#9
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.
Reply
#10
But you've still not said what kind of a server. ftp? http? https? pop3?
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Paramiko Server -- Exception (server): Error reading SSH protocol banner ujlain 3 4,635 Jul-24-2023, 06:52 AM
Last Post: Gribouillis
  Python script multi client server sonra 1 2,480 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  Multi connection socket server help! MuntyScruntfundle 0 2,728 Feb-19-2019, 12:03 PM
Last Post: MuntyScruntfundle
  newbie here - need help with building very basic http server BenSalem 0 2,397 Aug-14-2017, 08:06 AM
Last Post: BenSalem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020