The Python standard library, the Twisted networking engine, and the PyQt QtNetwork module provide considerable support for networking, from low-level sockets to various high-level protocols, including FTP and HTTP.
To write client/server applications we must ensure that the client and the server programs can communicate. This means that the server must run at a known IP address and listen at a specific port address. Both client and server must communicate using an agreed-upon protocol such as UDP, or more commonly, TCP. They must also agree on how the data is to be transmitted, whether as lines of text or as blocks of binary data—and in both cases, they must know what format each request and response must take.
The scenario shown in this chapter is a very common one: The server sits waiting for requests, and clients send requests and then read back the server's responses. Before a client can communicate at all it must establish a connection, and then, once the connection has been established, it can send its data. The server may respond with data, or some problem may have occurred. If data is received, we must make sure that we never attempt to read more bytes (or lines) than are available.
PyQt's QTcpServer and QTcpSocket classes make it very easy to implement servers. And although it is possible to read and write lines of textual data, using binary data is much more versatile, allowing us to send and receive any type of data and with no need to write a parser.
One theoretical problem with the TCP server we have implemented is that it is single threaded. This means that it may have to block to handle one request at a time if multiple requests arrive at the same moment. This can be solved by using a threaded server, as we will see in the next chapter.
Was this article helpful?
Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.