Learning Python Network Programming

(Sean Pound) #1
Chapter 8

For all our servers, we would also want to add a logging mechanism. I strongly
recommend the standard library logging module for this, the documentation for
this is complete and full of good examples. The basic tutorial is a good place to start
if you've not used it before, and it can be found at https://docs.python.org/3/
howto/logging.html#logging-basic-tutorial.


We also want to handle errors more comprehensively. Since the intention is that
our server should be long running with minimal intervention, we want to make sure
that nothing less than a critical exception causes the process to exit. We also want
to make sure that errors that occur when handling one client do not affect other
connected clients.


Finally there are some basic features of chat programs that it may be fun to add:
letting users enter a name, which would be shown beside their messages on the other
clients; adding chat rooms; and adding TLS encryption to the socket connections to
provide privacy and security.


Summary


We looked at how to develop network protocols while considering aspects such
as the connection sequence, framing of the data on the wire, and the impact these
choices will have on the architecture of the client and server programs.


We worked through different architectures for network servers and clients,
demonstrating the differences between the multithreaded and event-driven models
by writing a simple echo server and upgrading it to a multi-client chat server. We
discussed performance issues around threaded and event-driven architectures.
Finally, we looked at the eventlet and asyncio frameworks, which can greatly
simplify the process of writing servers when using an event-driven approach.


In the next and final chapter of this book, we will look at bringing several threads of
this book together for writing server-side web applications.

Free download pdf