使用CGI进行远程编辑

这里有3个文件,分别是index.html,edit.cgi,save.cgi

localhost/index.html

其中index.html文件要放在网站的跟目录下,即:/var/www,其他两个文件放在cgi-bin文件夹下

index.html 代码

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

edit.cgi 代码

#!/usr/bin/python
import cgitb;cgitb.enable()
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)

save.cgi 的代码

#!/usr/bin/python
import cgitb;cgitb.enable()
print 'Content-type:text/html\n'
from os.path import join,abspath
import cgi,hashlib,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()

#hash Encryption
if hashlib.md5(password).hexdigest() != 'a68a1e95537d3522068539dda2a7ac0c':
print 'Invalid password'
sys.exit()
f = open(join(BASE_DIR,filename),'w')
f. write(text)
f.close()
print 'The file has been saved.'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值