README
搭建Apache环境
Mac 自动Apache环境,调整配置支持cgi、py
/etc/apache2/httpd.conf
删除注释
LoadModule cgi_module libexec/apache2/mod_cgi.so
删除注释
ScriptAliasMatch ^/cgi-bin/((?!(?i:webobjects)).*$) "/Library/WebServer/CGI-Executables/$1"
增加解析文件格式
AddHandler cgi-script .cgi .pl .py .sh
Apache服务启停
sudo apachectl start
sudo apachectl stop
sudo apachectl restart
查看启动日志
sudo apachectl -k start
.CGI文件放置路径(赋值755权限)
/Library/WebServer/CGI-Executables
访问方式
localhost/cgi-bin/FileName.cgi
Resource
hello.cgi
#!/usr/bin/python
# -*- coding: UTF-8 -*-
print "Content-type:text/html"
print
print '<html>'
print '<head>'
print '<meta charset="utf-8">'
print '<title>Hello, World!</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! </h2>'
print '</body>'
print '</html>'
simple.cgi
#!/usr/bin/env python
import cgi
form = cgi.FieldStorage()
name = form.getvalue('name', 'world')
print """Content-type: text/html
<html>
<head>
<title>Greeting Page</title>
</head>
<body>
<h1>Hello, %s!</h1>
<form action='simple.cgi'>
Change name <input type='text' name='name' />
<input type='submit' />
</form>
</body>
</html>
""" % name
index.cgi
#!/usr/bin/env python
print """Content-type: text/html
<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>"""
edit.cgi
#!/usr/bin/env python
print 'Content-type: text/html\n'
from os.path import join, abspath
import cgi, sys
#BASE_DIR = abspath('data')
BASE_DIR = '/Users/nassue/Script'
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/env python
print 'Content-type: text/html\n'
from os.path import join, abspath
import cgi, sha, sys
#BASE_DIR = abspath('data')
BASE_DIR = '/Users/nassue/Script'
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() != '3c32d261f1b03e2a2a1c44d89ff92fc263b050d9':
print 'Invalid password'
sys.exit()
f = open(join(BASE_DIR,filename), 'w')
f.write(text)
f.close()
print 'The file has been saved.'