?? fig27_20.py
字號:
#!C:\Python\python.exe
# Fig. 27.20: fig27_20.py
# Writing a cookie to a client's machine
import cgi
import Cookie
import time
form = cgi.FieldStorage() # get form information
try: # extract form values
name = form[ "name" ].value
height = form[ "height" ].value
color = form[ "color" ].value
except:
print "Content-type: text/html"
print
print """<h3>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></h3>"""
else:
# construct cookie expiration date and path
expirationFormat = "%A, %d-%b-%y %X %Z"
expirationTime = time.localtime( time.time() + 300 )
expirationDate = time.strftime( expirationFormat,
expirationTime )
path = "/"
# construct cookie contents
cookie = Cookie.Cookie()
cookie[ "Name" ] = name
cookie[ "Name" ][ "expires" ] = expirationDate
cookie[ "Name" ][ "path" ] = path
cookie[ "Height" ] = height
cookie[ "Height" ][ "expires" ] = expirationDate
cookie[ "Height" ][ "path" ] = path
cookie[ "Color" ] = color
cookie[ "Color" ][ "expires" ] = expirationDate
cookie[ "Color" ][ "path" ] = path
# print cookie to user and page to browser
print cookie
print "Content-type: text/html"
print
print """<body background = "/images/back.gif">
<basefont face = "arial,sans-serif" size = "3">
The cookie has been set with the folowing data: <br><br>
<font color = "blue">Name:</font> %s<br>
<font color = "blue">Height:</font> %s<br>
<font color = "blue">Favorite Color:</font>
<font color = "%s"> %s<br>""" % ( name, height, color, color )
print """<br><a href= "fig27_21.py">
Read cookie values</a>"""
##########################################################################
# (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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -