python在cgi文件的运行小问题

坚强的豁着 2012-06-01 12:40:15

#!/usr/bin/env python

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)

这是一个CGI的文件,环境是正确的,我能确定。
但是加了Python的代码后,页面显示“Please enter a file name”,下面的html都没有显示,这是怎么回事,是上面的Python有问题吗?
请各位路过的朋友帮帮忙
...全文
229 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
坚强的豁着 2012-06-04
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

Python code
text=open(join(BASE_DIR,filename)).read()

文件名应该没对吧。最好如下,然后再打开后进行读写:
Python code
pathname = BASE_DIR + '/' + filename
fd = open(pathname)
text = fd.read()
[/Quote]
谢谢,我估计是权限的问题。用你的代码确实可以了。我再琢磨一下
bugs2k 2012-06-04
  • 打赏
  • 举报
回复
text=open(join(BASE_DIR,filename)).read()

文件名应该没对吧。最好如下,然后再打开后进行读写:
pathname = BASE_DIR + '/' + filename
fd = open(pathname)
text = fd.read()


坚强的豁着 2012-06-04
  • 打赏
  • 举报
回复
这是HTML文件的代码,它跟cgi不是在一个目录

<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' />
</form>
</body>
</html>
panghuhu250 2012-06-01
  • 打赏
  • 举报
回复
看起来是连向这个cgi的form的问题,贴出那个form的html代码。

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧