And here is what appears in the Windows console window where I run the client. A
“connected by” message appears in the server SSH window each time the client script
is run in the client window:
C:\...\PP4E\Internet\Sockets> python echo-client.py learning-python.com
Client received: b'Echo=>Hello network world'
C:\...\PP4E\Internet\Sockets> python echo-client.py learning-python.com ni Ni NI
Client received: b'Echo=>ni'
Client received: b'Echo=>Ni'
Client received: b'Echo=>NI'
C:\...\PP4E\Internet\Sockets> python echo-client.py learning-python.com Shrubbery
Client received: b'Echo=>Shrubbery'
The ping command can be used to get an IP address for a machine’s domain name;
either machine name form can be used to connect in the client:
C:\...\PP4E\Internet\Sockets> ping learning-python.com
Pinging learning-python.com [97.74.215.115] with 32 bytes of data:
Reply from 97.74.215.115: bytes=32 time=94ms TTL=47
Ctrl-C
C:\...\PP4E\Internet\Sockets> python echo-client.py 97.74.215.115 Brave Sir Robin
Client received: b'Echo=>Brave'
Client received: b'Echo=>Sir'
Client received: b'Echo=>Robin'
This output is perhaps a bit understated—a lot is happening under the hood. The client,
running on my Windows laptop, connects with and talks to the server program running
on a Linux machine perhaps thousands of miles away. It all happens about as fast as
when client and server both run on the laptop, and it uses the same library calls; only
the server name passed to clients differs.
Though simple, this illustrates one of the major advantages of using sockets for cross-
program communication: they naturally support running the conversing programs on
different machines, with little or no change to the scripts themselves. In the process,
sockets make it easy to decouple and distribute parts of a system over a network when
needed.
Socket pragmatics
Before we move on, there are three practical usage details you should know. First, you
can run the client and server like this on any two Internet-aware machines where Python
is installed. Of course, to run the client and server on different computers, you need
both a live Internet connection and access to another machine on which to run the
server.
This need not be an expensive proposition, though; when sockets are opened, Python
is happy to initiate and use whatever connectivity you have, be it a dedicated T1 line,
wireless router, cable modem, or dial-up account. Moreover, if you don’t have a server
796 | Chapter 12: Network Scripting