fig27_18.py

来自「PERL语言资料 可以用于PERL程序设计」· Python 代码 · 共 100 行

PY
100
字号
#!c:\Python\python.exe
# Fig. 27.18: fig27_18.py
# Program to read information sent to the server from the
# form in the form.html document.

import cgi
import re

# the regular expression for matching most American phone numbers.
telephoneExpression = \
   re.compile( r'\(\d\d\d\)\d\d\d-\d\d\d\d' )

def printContent():
   print "Content-type: text/html"
   print
   
def printReply():
   print """Hi <font color = "blue"><b>%(firstName)s</b></font>.
      Thank you for completing the survey.<BR>
      You have been added to the 
      <font color = "blue"><strong>%(book)s </strong></font>
      mailing list.<br><br>

      <strong>The following information has been saved 
      in our database:</strong><br>

      <table border = "0" cellpadding = "0"
          cellspacing = "10">

      <tr><td bgcolor = "#FFFFAA">Name </td>
          <td bgcolor = "#FFFFAA">Email</td>
          <td bgcolor = "#FFFFAA">Phone</td>
          <td bgcolor = "#FFFFAA">OS</td></tr>

      <tr><td>%(firstName)s %(lastName)s</td><td>%(email)s</td>
          <td>%(phone)s</td><td>%(os)s</td></tr>
      </table>

      <br><br><br>

      <center><font size = "-3">
      This is only a sample form. 
      You have not been added to a mailing list.
      </font></center>
      """ % personInfo

def printPhoneError():

   print """<font color = "red" size = "+2">
      INVALID PHONE NUMBER</font><br>
      A valid phone number must be in the form
      <strong>(555)555-5555</strong>
      <font color = "blue"> Click the Back button, 
      enter a valid phone number and resubmit.<br><br>
      Thank You."""
      
def printFormError():

   print """<font color = "red" size = "+2">
      FORM ERROR</font><br>
      You have not filled in all fields.

      <font color = "blue"> Click the Back button, 
      fill out the form and resubmit.<br><br>
      Thank You.</font>"""
     
printContent()

form = cgi.FieldStorage()

try:
   personInfo = { 'firstName' : form[ "firstname" ].value,
                  'lastName' : form[ "lastname" ].value,
                  'email' : form[ "email" ].value,
                  'phone' : form[ "phone" ].value,
                  'book' : form[ "book" ].value,
                  'os' : form[ "os" ].value }
except:
   printFormError()
      
if telephoneExpression.match( personInfo[ 'phone' ] ):
   printReply()
else:
   printPhoneError()  


########################################################################## 
# (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     #
# All Rights Reserved.                                                   #
#                                                                        #
# DISCLAIMER: The authors and publisher of this book have used their     #
# best efforts in preparing the book. These efforts include the          #
# development, research, and testing of the theories and programs        #
# to determine their effectiveness. The authors and publisher make       #
# no warranty of any kind, expressed or implied, with regard to these    #
# programs or to the documentation contained in these books. The authors #
# and publisher shall not be liable in any event for incidental or       #
# consequential damages in connection with, or arising out of, the       #
# furnishing, performance, or use of these programs.                     #
##########################################################################

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?