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

(yzsuai) #1
80, so this is the default if the port is omitted. See Chapter 12 if you need a refresher
on machine names and ports.

File path: ~lutz/tutor0.html
Finally, the URL gives the path to the desired file on the remote machine. The
HTTP web server automatically translates the URL’s file path to the file’s true
pathname: on the starship server, ~lutz is automatically translated to the
public_html directory in my home directory. When using the Python-coded web
server script in Example 15-1, files are mapped to the server’s current working
directory instead. URLs typically map to such files, but they can reference other
sorts of items as well, and as we’ll see in a few moments may name an executable
CGI script to be run when accessed.


Query parameters (used in later examples)
URLs may also be followed by additional input parameters for CGI programs.
When used, they are introduced by a? and are typically separated by & characters.
For instance, a string of the form ?name=bob&job=hacker at the end of a URL passes
parameters named name and job to the CGI script named earlier in the URL, with
values bob and hacker, respectively. As we’ll discuss later in this chapter when we
explore escaping rules, the parameters may sometimes be separated by ; characters
instead, as in ?name=bob;job=hacker, though this form is less common.
These values are sometimes called URL query string parameters and are treated the
same as form inputs by scripts. Technically speaking, query parameters may have
other structures (e.g., unnamed values separated by +), but we will ignore addi-
tional options in this text; more on both parameters and input forms later in this
tutorial.


To make sure we have a handle on URL syntax, let’s pick apart another example that
we will be using later in this chapter. In the following HTTP protocol URL:


http://localhost:80/cgi-bin/languages.py?language=All

the components uniquely identify a server script to be run as follows:



  • The server name localhost means the web server is running on the same machine
    as the client; as explained earlier, this is the configuration we’re using for our
    examples.

  • Port number 80 gives the socket port on which the web server is listening for con-
    nections (port 80 is the default if this part is omitted, so we will usually omit it).

  • The file path cgi-bin/languages.py gives the location of the file to be run on the
    server machine, within the directory where the server looks for referenced files.

  • The query string ?language=All provides an input parameter to the referenced
    script languages.py, as an alternative to user input in form fields (described later).


1138 | Chapter 15: Server-Side Scripting

Free download pdf