python基础教程项目六:使用CGI进行远程编辑

本文介绍了一个使用Python CGI实现的简易远程文件编辑器。该编辑器允许用户通过网络编辑服务器上的文件,并通过SHA摘要确保密码的安全存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

远程访问是远程用其他机器通过网络连接远程服务器,对资源进行访问和修改
本文用python的CGI编程,完成一个远程编程的小工具.
程序逻辑如下:
1.创建文件名表单

2.创建编辑器脚本
  应检查文件名是否存在,存在才可进行编辑
  
#!/usr/bin/env python

from os.path import join,abspath
import cgi,sys
BASE_DIR=abspath('data')
form=cgi.FieldStorage()
filename=form.getvalue('filename')
if not filename:
	print ''
    sys.exit()
text=open(join(BASE_DIR,filename)).read()
print """
<html>
 <head>
  <title>Editing...</title>
 </head>
 <body>
 <form action='/test/cgi-bin/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)


  
 3.编写保存脚本,其中会涉及到安全性设置的摘要,可以通过sha模块,用它提供的摘要密码安全的存储摘要信息
 
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#处理前端提交的数据,并保存
from os.path import join,abspath
import cgi,sha,sys
BASE_DIR=abspath()
form=cgi.FieldStorage()
text=form.getvalue('text')
filename=form.getvalue('filename')
password=form.getvalue()

if not (filename and text and password)
   print ''
   sys.exit()

if sha.sha(password).hexdigest!=''
	print ''
   sys.exit()

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值