[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1
C:\Users\mark\Stuff\Books\4E\PP4E\dev\Examples\PP4E\Internet\Sockets>python sele
ct-server.py
select-server loop starting
Connect: ('127.0.0.1', 59080) 21339352
got b'Hello network world' on 21339352
got b'' on 21339352
Connect: ('127.0.0.1', 59081) 21338128
got b'Sir' on 21338128
got b'Galahad' on 21338128
got b'' on 21338128
Connect: ('127.0.0.1', 59082) 21339352
got b'Hello network world' on 21339352
got b'' on 21339352

[testecho results]
Connect: ('127.0.0.1', 59083) 21338128
got b'Hello network world' on 21338128
got b'' on 21338128
Connect: ('127.0.0.1', 59084) 21339352
got b'Hello network world' on 21339352
got b'' on 21339352
Connect: ('127.0.0.1', 59085) 21338128
got b'Hello network world' on 21338128
got b'' on 21338128
Connect: ('127.0.0.1', 59086) 21339352
got b'Hello network world' on 21339352
got b'' on 21339352
Connect: ('127.0.0.1', 59087) 21338128
got b'Hello network world' on 21338128
got b'' on 21338128
Connect: ('127.0.0.1', 59088) 21339352
Connect: ('127.0.0.1', 59089) 21338128
got b'Hello network world' on 21339352
got b'Hello network world' on 21338128
Connect: ('127.0.0.1', 59090) 21338056
got b'' on 21339352
got b'' on 21338128
got b'Hello network world' on 21338056
got b'' on 21338056

Besides this more verbose output, there’s another subtle but crucial difference to
notice—a time.sleep call to simulate a long-running task doesn’t make sense in the
server here. Because all clients are handled by the same single loop, sleeping would
pause everything, and defeat the whole point of a multiplexing server. Again, manual
multiplexing servers like this one work well when transactions are short, but also gen-
erally require them to either be so, or be handled specially.


Before we move on, here are a few additional notes and options:


select call details
Formally, select is called with three lists of selectable objects (input sources, out-
put sources, and exceptional condition sources), plus an optional timeout. The
timeout argument may be a real wait expiration value in seconds (use floating-point


824 | Chapter 12: Network Scripting

Free download pdf