cookiePostResult.cgi
#!/usr/bin/python
# -*- coding:utf-8 -*-
import cgi
import cgitb
# エラー発生時にレポートを表示
cgitb.enable()
f = cgi.FieldStorage()
field1 = f.getfirst('field1', '')
field2 = f.getfirst('field2', '')
# Cookie に保存
print "Set-Cookie: FIELD1=%s" % (field1)
print "Set-Cookie: FIELD2=%s" % (field2)
print "\n"
html = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Cookie POST Result</title>
</head>
<body>
<h1>Cookie POST Result</h1>
Field1: %s<br/>
Field2: %s<br/>
<a href="/cgi-bin/python/cookiePostResult2.cgi">次へ</a>
</body>
</html>
"""
print html % (field1, field2)
Set-Cookie でクライアントのブラウザに Cookie 保存するように指示する。保存された Cookie を読み込んで表示する
cookiePostResult2.cgi
#!/usr/bin/python # -*- coding:utf-8 -*- import os import cgi import cgitb import Cookie # Cookie の値を読み出す cookie = Cookie.SimpleCookie() cookie.load(os.environ["HTTP_COOKIE"]) field1 = cookie["FIELD1"].value field2 = cookie["FIELD2"].value html = """ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Cookie POST Result 2</title> </head> <body> <h1>Cookie POST Result 2</h1> Field1: %s<br/> Field2: %s<br/> </body> </html> """ print html % (field1, field2)
 
0 件のコメント:
コメントを投稿