python核心编程二十章例题asvcgi.py中的错误与修改
来源:互联网 发布:徐老师淘宝外设店名字 编辑:程序博客网 时间:2024/06/07 03:01
在python核心编程例题20.8中,按照作者代码是无法正常运行的,需要修改一下代码。主要是15行修改一下,48-51行添加四段代码。修改后代码如下:
#!/usr/bin/env python# -*- coding:utf-8from cgi import FieldStoragefrom os import environfrom cStringIO import StringIOfrom urllib import quote, unquotefrom string import join, capwordsclass AdvCGI: header = 'Content-Type: text/html\n\n' url = '/cgi-bin/advcgi.py' formhtml = '''<HTML><HEAD><TITLE>Advanced CGI Demo</TITLE></HEAD><BODY><H2>Advanced CGI Demo Form</H2><FORM METHOD=post ACTION="%s" ENCTYPE="multipart/form-data"><H3>My Cookie Setting</H3><LI> <CODE><B>CPPuser = %s</B></CODE><H3>Enter cookie value<BR><INPUT NAME=cookie value="%s"> (<I>optional</I>)</H3><H3>Enter your name<BR><INPUT NAME=person VALUE="%s"> (<I>required</I>)</H3><H3>What languages can you program in?(<I>at least one required</I>)</H3>%s<H3>Enter file to upload</H3><INPUT TYPE=file NAME=upfile VALUE="%s" SIZE=45><P><INPUT TYPE=submit></FORM></BODY></HTML>''' langSet = ('Python', 'PERL', 'Java', 'C++', 'PHP', 'C', 'JavaScript') langItem = '<INPUT TYPE=checkbox NAME=lang VALUE="%s"%s> %s\n' def getCPPCookies(self): # reads cookies from client if 'HTTP_COOKIE' in environ: cookies = [x.strip() for x in environ['HTTP_COOKIE'].split(';')] for eachCookie in cookies: if len(eachCookie) > 6 and eachCookie[:3] == 'CPP': tag = eachCookie[3:7] try: self.cookies[tag] = eval(unquote(eachCookie[8:])) except (NameError, SyntaxError): self.cookies[tag] = unquote(eachCookie[8:]) if 'info' not in self.cookies: self.cookies['info'] = '' if 'user' not in self.cookies: self.cookies['user'] = '' else: self.cookies['info'] = self.cookies['user'] = '' if self.cookies['info'] != '': self.who, langStr, self.fn = self.cookies['info'].split(':') self.langs = langStr.split(',') else: self.who = self.fn = '' self.langs = ['Python'] def showForm(self): # show fill-out form self.getCPPCookies() langStr = '' for eachLang in AdvCGI.langSet: if eachLang in self.langs: langStr = langStr + AdvCGI.langItem % \ (eachLang, ' CHECKED', eachLang) else: langStr = langStr + AdvCGI.langItem % \ (eachLang, '', eachLang) if 'user' not in self.cookies or \ self.cookies['user'] == '': cookieStatus = '<I>(cookie has not been set yet)</I>' userCook = '' else: userCook = cookieStatus = self.cookies['user'] print AdvCGI.header + AdvCGI.formhtml % (AdvCGI.url, cookieStatus, userCook, self.who, langStr, self.fn) errhtml = '''<HTML><HEAD><TITLE>Advanced CGI Demo</TITLE></HEAD><BODY><H3>ERROR</H3><B>%s</B><P><FORM><INPUT TYPE=button VALUE=BackONCLICK="window.history.back()"></FORM></BODY></HTML>''' def showError(self): print AdvCGI.header + AdvCGI.errhtml % (self.error) reshtml = '''<HTML><HEAD><TITLE>Advanced CGI Demo</TITLE></HEAD><BODY><H2>Your Uploaded Data</H2><H3>Your cookie value is: <B>%s</B></H3><H3>Your name is: <B>%s</B></H3><H3>You can program in the following languages:</H3><UL>%s</UL><H3>Your uploaded file...<BR>Name: <I>%s</I><BR>Contents:</H3><PRE>%s</PRE>Click <A HREF="%s"><B>here</B></A> to return to form.</BODY></HTML>''' def setCPPCookies(self): for eachCookie in self.cookies.keys(): print 'Set-Cookie: CPP%s=%s; path=/' % \ (eachCookie, quote(self.cookies[eachCookie])) def doResults(self): MAXBYTES = 1024 langlist = '' for eachLang in self.langs: langlist = langlist + '<LI>%s<BR>' % eachLang filedata = '' while len(filedata) < MAXBYTES: # read file chunks data = self.fp.readline() if data == '': break filedata = filedata + data else: # truncate if too long filedata = filedata + \ '... <B><I>(file truncated due to size)</I></B>' self.fp.close() if filedata == '': filedata = \ '<B><I>(file upload error or file not given)</I></B>' filename = self.fn if not ('user' in self.cookies and self.cookies['user']): cookieStatus = '<I>(cookie has not been set yet)</I>' userCook = '' else: userCook = cookieStatus = self.cookies['user'] self.cookies['info'] = join([self.who, join(self.langs, ','), filename], ':') self.setCPPCookies() print AdvCGI.header + AdvCGI.reshtml % \ (cookieStatus, self.who, langlist, filename, filedata, AdvCGI.url) def go(self): # determine which page to return self.cookies = {} self.error = '' form = FieldStorage() if form.keys() == []: self.showForm() return if 'person' in form: self.who = capwords(form['person'].value.strip()) if self.who == '': self.error = 'Your name is required. (blank)' else: self.error = 'Your name is required. (missing)' if 'cookie' in form: self.cookies['user'] = unquote(form['cookie'].value.strip()) else: self.cookies['user'] = '' self.langs = [] if 'lang' in form: langdata = form['lang'] if isinstance(langdata, list): for eachLang in langdata: self.langs.append(eachLang.value) else: self.langs.append(langdata.value) else: self.error = 'At least one language required.' if 'upfile' in form: upfile = form["upfile"] self.fn = upfile.filename or '' if upfile.file: self.fp = upfile.file else: self.fp = StringIO('(no data)') else: self.fp = StringIO('(no file)') self.fn = '' if not self.error: self.doResults() else: self.showError()if __name__ == '__main__': page = AdvCGI() page.go()
0 0
- python核心编程二十章例题asvcgi.py中的错误与修改
- python核心编程“第一个Python程序”makeTextFile.py中错误纠正
- python:核心编程中makeTextFile.py和readTextFile.py
- python核心编程第二版中的网络爬爬虫修改
- Python核心编程 示例错误
- 《Python核心编程》简单的登录系统 userpw.py
- Python中的__init__.py与模块导入
- <PY>Python修改excel
- python核心编程学习-书中错误
- Python核心编程----错误和异常
- Python中的setup.py
- Hanol towers [Gym-101243B]———Python语言在编程中的文件读入与输出,例题解释
- python核心编程-搜索与替换
- python核心编程&字典与集合
- python核心编程&条件判断与循环
- 《Python核心编程》第10章 错误和异常 练习
- python核心编程七——错误和异常,函数
- 《Python核心编程》第十章:错误和异常
- time_wait()是什么状态?
- Android中有几种数据存储方式,每种方式有哪些特点?
- 从零开始的Android新项目5 - Repository层(上)
- java入门的一个游戏
- TCP为什么是进行三次握手与四次挥手?
- python核心编程二十章例题asvcgi.py中的错误与修改
- 为什么要对ListView控件进行优化,以及如何优化。
- MonkeyRunner API使用说明-------------第一章
- VC中CListCtrl删除所有的列
- android 小知识点
- Android之Jsoup解析数据
- Javascript动画学习
- HDU5753 Permutation Bo
- hdu2196 Computer 【树形dp】