Handling stateful protocols
The HTTP protocol supports persistent connections that reuse the same underlying TCP/IP connection for multiple request/response pairs. Sometimes you need to persist session cookies across requests. There are still a lot of network protocols that use a persistent TCP/IP connection for multiple asynchronous requests and responses. One way or another, there are times when you need to build a multi-threaded Tuxedo server.
To keep our examples simple, we will use a simple TCP/IP connection and for each character the server receives, it will reply with the same uppercase character. It will be our TCP/IP TOUPPER
service. Here is the code for toupper.py
, which listens on port 8765
:
import socket sock = socket.socket() sock.bind(("", 8765)) sock.listen(1) while True: con, _ = sock.accept() while True: c = con.recv(1024) &...