Learning Python Network Programming

(Sean Pound) #1

Client and Server


Applications


In the previous chapter, we looked at exchanging data between devices by using
the sockets interface. In this chapter, we're going to use sockets to build network
applications. Sockets follow one of the main models of computer networking, that
is, the client/server model. We'll look at this with a focus on structuring server
applications. We'll cover the following topics:



  • Designing a simple protocol

  • Building an echo server and client

  • Building a chat server and client

  • Multithreaded and event-driven server architectures

  • The eventlet and asyncio libraries


The examples in this chapter are best run on Linux or a Unix operating system.
The Windows sockets implementation has some idiosyncrasies, and these can create
some error conditions, which we will not be covering here. Note that Windows does
not support the poll interface that we'll use in one example. If you do use Windows,
then you'll probably need to use ctrl + break to kill these processes in the console,
rather than using ctrl - c because Python in a Windows command prompt doesn't
respond to ctrl – c when it's blocking on a socket send or receive, which will be quite
often in this chapter! (and if, like me, you're unfortunate enough to try testing these
on a Windows laptop without a break key, then be prepared to get very familiar with
the Windows Task Manager's End task button).

Free download pdf