Dmitry
Dmitry

Reputation: 423

Whats the best way to implement python TCP client?

I need to write python script which performs several tasks:

  1. read commands from console and send to server over tcp/ip

  2. receive server response, process and make output to console.

What is the best way to create such a script? Do I have to create separate thread to listen to server response, while interacting with user in main thread? Are there any good examples?

Upvotes: 0

Views: 113

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149125

Calling for a best way or code examples is rather off topic, but this is too long to be a comment.

There are three general ways to build those terminal emulator like applications :

  • multiple processes - the way the good old Unix cu worked with a fork
  • multiple threads - a variant from the above using light way threads instad of processes
  • using select system call with multiplexed io.

Generally, the 2 first methods are considered more straightforward to code with one thread (or process) processing upward communication while the other processes the downward one. And the third while being trickier to code is generally considered as more efficient

As Python supports multithreading, multiprocessing and select call, you can choose any method, with a slight preference for multithreading over multiprocessing because threads are lighter than processes and I cannot see a reason to use processes.

Following in just my opinion

Unless if you are writing a model for rewriting it later in a lower level language, I assume that performance is not the key issue, and my advice would be to use threads here.

Upvotes: 1

Related Questions