第25章 项目6:使用CGI进行远程编辑

初次实现

25-1 simple_edit.cgi ——简单的网页编辑器

#!D:\Program Files\python27\python.exe
import cgi
form = cgi.FieldStorage()

text = form.getvalue('text', open('simple_edit.dat').read())
f = open('simple_edit.dat', 'w')
f.write(text)
f.close()
print """Content-type: text/html

<html>
<head>
<title>A simple Editor</title>
</head>
<body>
<form action='simple_edit.cgi' method='POST'>
<textarea rows='10' cols='20' name='text'>%s</textarea><br />
<input type='submit' />
</form>
</body>
</html>
""" % text

simple_edit.dat文件:随意输入一些即可。

将此脚本及空白的simple_edit.dat文件(没有该文件则无法运行)放在D:\Program Files\Apache24\cgi-bin目录下

在浏览器中输入:http://localhost/cgi-bin/simple_edit.cgi

输出如下:

按照书上输入Nickety, nockety, noo, noo, noo...并提交

从页面看没什么变化,但simple_edit.dat中写入了以上内容

再次打开http://localhost/cgi-bin/simple_edit.cgi,页面显示如下:

且每次在查询内容中输入不同的字符,都会重新写入simple_edit.dat

 

再次实现

CGI脚本拆分为3个:一个带有能输入文件名的表单的网页index.html,在文本域中显示给定文件的脚本edit.cgi,保存收到的文本到给定文件的脚本save.cgi

index.htmlHTML文件,包括用于输入文件名的表单

<html>
<head>
<title>File Editor</title>
</head>
<body>
<form action='edit.cgi' method='POST'>
<b>File name:</b><br />
<input type='text' name='filename' />
<input type='submit' value='Open' />
</body>
</html>

25-2 edit.cgi ——编辑器脚本

edit.cgi显示网页,还有一个用于输入密码的文本框。

abspath函数被用于获取data目录的绝对路径,文件名保存在hidden表单元素中。会被传递到下一个脚本save.cgi中。

#!D:\Program Files\python27\python.exe

print 'Content-type: text/html\n'

from os.path import join, abspath

import cgi, sys

BASE_DIR = abspath('data')

form = cgi.FieldStorage()
filename = form.getvalue('filename')
if not filename:
print 'Please enter a file name'
sys.exit()
text = open(join(BASE_DIR, filename)).read()

print """
<html>
<head>
<title>Editing...</title>
</head>
<body>
<form action='save.cgi' method='POST'>
<b>File:</b> %s<br />
<input type='hidden' value='%s' name='filename' />
<b>Password:</b><br />
<input name='password' type='password' /><br />
<b>Text:</b><br />
<textarea name='text' cols='40' rows='20'>%s</textarea><br />
<input type='submit' value='Save' />
</form>
</body>
</html>
""" % (filename, filename, text)

密码处理——sha模块(SecureHash Algorithm,安全哈希算法)

从输入字符串中提取看似随机数据的根本上无意义字符串的一种方法。

>>> from sha import sha

>>> sha('foobar').hexdigest()

'8843d7f92416211de9ebb963ff4ce28125932878'

>>> sha('foobaz').hexdigest()

'21eb6533733a5e4763acacd1d45a60c2e0e404e1'

密码中的微小改变会输出完全不同的摘要。

 

25-3 save.cgi ——实现保存功能的脚本

处理前端提交的数据,并保存:接受一个文件名、一个密码和一些文本,并且检查密码是否正确。

#!D:\Program Files\python27\python.exe

print'Content-type: text/html\n'

from os.path import join, abspath
import cgi, sha, sys

BASE_DIR = abspath('data')

form = cgi.FieldStorage()
text = form.getvalue('text')
filename = form.getvalue('filename')
password = form.getvalue('password')

if not(filename and text and password):
print 'Invalid parameters.'
sys.exit()
if sha.sha(password).hexdigest() != '8843d7f92416211de9ebb963ff4ce28125932878':
print 'Invalid password'
sys.exit()

f = open(join(BASE_DIR, filename), 'w')
f.write(text)
f.close()

print 'The file has been saved.'

index.html及脚本edit.cgisave.cgi放入到D:\Program Files\Apache24\htdocs目录下,并创建data文件夹,新建空白的edit.dat文件。

在浏览器中输入:localhost127.0.0.1即可调出 index.html

输入edit.dat然后点击open

密码默认为foobar,输入文本Nickety, nockety, noo, noo, noo...,并提交:

 

文本写入到edit.dat中:

若密码输入错误,显示如下:

若无任何密码或文本输入,显示如下:

posted @ 2016-12-04 20:14  Sumomo  阅读(1135)  评论(0编辑  收藏  举报