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

(yzsuai) #1

file; get-style URLs don’t work for uploading files (adding their contents to the end of
the URL doesn’t make sense). When we visit this HTML file, the page shown in Fig-
ure 15-33 is delivered. Pressing its Browse button opens a standard file-selection dialog,
while Upload sends the file.


Figure 15-33. File upload selection page


On the client side, when we press this page’s Upload button, the browser opens and
reads the selected file and packages its contents with the rest of the form’s input fields
(if any). When this information reaches the server, the Python script named in the form
action tag is run as always, as listed in Example 15-30.


Example 15-30. PP4E\Internet\Web\cgi-bin\putfile.py


#!/usr/bin/python
"""
##################################################################################
extract file uploaded by HTTP from web browser; users visit putfile.html to
get the upload form page, which then triggers this script on server; this is
very powerful, and very dangerous: you will usually want to check the filename,
etc; this may only work if file or dir is writable: a Unix 'chmod 777 uploads'
may suffice; file pathnames may arrive in client's path format: handle here;


caveat: could open output file in text mode to wite receiving platform's line
ends since file content always str from the cgi module, but this is a temporary
solution anyhow--the cgi module doesn't handle binary file uploads in 3.1 at all;
##################################################################################
"""


import cgi, os, sys
import posixpath, ntpath, macpath # for client paths
debugmode = False # True=print form info


Transferring Files to Clients and Servers | 1219
Free download pdf