[client window: requested file downloaded in a thread on server]
C:\...\Internet\Sockets> python getfile.py –mode client
-host learning-python.com
-port 50001 -file python.exe
C:\...\Internet\Sockets> python getfile.py
-host learning-python.com -file index.html
One subtle security point here: the server instance code is happy to send any server-
side file whose pathname is sent from a client, as long as the server is run with a user-
name that has read access to the requested file. If you care about keeping some of your
server-side files private, you should add logic to suppress downloads of restricted files.
I’ll leave this as a suggested exercise here, but we will implement such filename checks
in a different getfile download tool later in this book.#
Adding a User-Interface Frontend
After all the GUI commotion in the prior part of this book, you might have noticed that
we have been living in the realm of the command line for this entire chapter—our socket
clients and servers have been started from simple DOS or Linux shells. Nothing is
stopping us from adding a nice point-and-click user interface to some of these scripts,
though; GUI and network scripting are not mutually exclusive techniques. In fact, they
can be arguably “sexy” when used together well.
For instance, it would be easy to implement a simple tkinter GUI frontend to the client-
side portion of the getfile script we just met. Such a tool, run on the client machine,
may simply pop up a window with Entry widgets for typing the desired filename, server,
and so on. Once download parameters have been input, the user interface could either
import and call the getfile.client function with appropriate option arguments, or
build and run the implied getfile.py command line using tools such as os.system,
os.popen, subprocess, and so on.
Using row frames and command lines
To help make all of this more concrete, let’s very quickly explore a few simple scripts
that add a tkinter frontend to the getfile client-side program. All of these examples
assume that you are running a server instance of getfile; they merely add a GUI for
the client side of the conversation, to fetch a file from the server. The first, in Exam-
ple 12-18, uses form construction techniques we met in Chapters 8 and 9 to create a
dialog for inputting server, port, and filename information, and simply constructs the
#We’ll see three more getfile programs before we leave Internet scripting. The next chapter’s getfile.py fetches
a file with the higher-level FTP interface instead of using raw socket calls, and its http-getfile scripts fetch files
over the HTTP protocol. Later, Chapter 15 presents a server-side getfile.py CGI script that transfers file
contents over the HTTP port in response to a request made in a web browser client (files are sent as the output
of a CGI script). All four of the download schemes presented in this text ultimately use sockets, but only the
version here makes that use explicit.
A Simple Python File Server | 843