Running Socket Programs Remotely
To make these scripts talk over the Internet rather than on a single machine and sample
the broader scope of sockets, we have to do some extra work to run the server on a
different computer. First, upload the server’s source file to a remote machine where
you have an account and a Python. Here’s how I do it with FTP to a site that hosts a
domain name of my own, learning-python.com; most informational lines in the fol-
lowing have been removed, your server name and upload interface details will vary,
and there are other ways to copy files to a computer (e.g., FTP client GUIs, email, web
page post forms, and so on—see “Tips on Using Remote Servers” on page 798 for
hints on accessing remote servers):
C:\...\PP4E\Internet\Sockets> ftp learning-python.com
Connected to learning-python.com.
User (learning-python.com:(none)): xxxxxxxx
Password: yyyyyyyy
ftp> mkdir scripts
ftp> cd scripts
ftp> put echo-server.py
ftp> quit
Once you have the server program loaded on the other computer, you need to run it
there. Connect to that computer and start the server program. I usually Telnet or SSH
into my server machine and start the server program as a perpetually running process
from the command line. The & syntax in Unix/Linux shells can be used to run the server
script in the background; we could also make the server directly executable with a #!
line and a chmod command (see Chapter 3 for details).
Here is the text that shows up in a window on my PC that is running a SSH session
with the free PuTTY client, connected to the Linux server where my account is hosted
(again, less a few deleted informational lines):
login as: xxxxxxxx
[email protected]'s password: yyyyyyyy
Last login: Fri Apr 23 07:46:33 2010 from 72.236.109.185
[...]$ cd scripts
[...]$ python echo-server.py &
[1] 23016
Now that the server is listening for connections on the Net, run the client on your local
computer multiple times again. This time, the client runs on a different machine than
the server, so we pass in the server’s domain or IP name as a client command-line
argument. The server still uses a machine name of "" because it always listens on what-
ever machine it runs on. Here is what shows up in the remote learning-python.com
server’s SSH window on my PC:
[...]$ Server connected by ('72.236.109.185', 57697)
Server connected by ('72.236.109.185', 57698)
Server connected by ('72.236.109.185', 57699)
Server connected by ('72.236.109.185', 57700)
Socket Programming | 795