Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to code a Python Server

Which way is better?

Creating a while loop and then using the select module OR using ThreadedTCPServer with a custom class.

Im having problems with the Threaded TCP Server, although it could just be my coding.

like image 811
Anonymous Avatar asked Aug 09 '26 17:08

Anonymous


2 Answers

My personal recommendation is to use Twisted. It's a Python-based framework intended primarily for writing event-driven network software. The documentation has a lot of great examples of how to create various types of servers and clients, as well.

like image 63
Brian Avatar answered Aug 12 '26 09:08

Brian


I am sure there is no such a thing like the "correct" way.

If you want not, must not or cannot use any of the existing server implementations the general idea is (in pseudo code):

ss = serversocket()
ss.bind ()
while (True):
  cs = ss.accept ()
  spawnCommThread (cs)

In the CommThread for each client you take care of reading from the socket returned by accept, communicate with your client and die, when the client closes the connection or another criterion is given.

like image 36
Hyperboreus Avatar answered Aug 12 '26 08:08

Hyperboreus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!