一、问题
折腾了大半天,终于搞定。被网上各种模模糊糊Apache关于CGI 配置弄得心力交瘁。
最终是通过python 3 中http.server搞定本地服务器。
二、方案
1.脚本
前端网页 friends.html
<HTML>
<HEAD>
<TITLE>Friends CGI Demo (static screen)</TITLE>
</HEAD>
<BODY>
<H3> Friends list for: <I>NEW USER</I></H3>
<FORM ACTION="/cgi-bin/friendsA.py">
<B>Enter your Name:</B>
<INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
<P><B>How many friends do you have?</B>
<INPUT TYPE=radio NAME=howmany VALUE="0" CHECKED> 0
<INPUT TYPE=radio NAME=howmany VALUE="10" CHECKED> 10
<INPUT TYPE=radio NAME=howmany VALUE="25" CHECKED> 25
<INPUT TYPE=radio NAME=howmany VALUE="50" CHECKED> 50
<INPUT TYPE=radio NAME=howmany VALUE="100" CHECKED> 100
<p><INPUT TYPE=submit>
</FORM>
</BODY>
</HTML>
py脚本 friendsA.py
import cgi
reshtml = '''Content-Type: text/html\n
<HTML>
<HEAD><TITLE>Friends CGI Demo (dynamic screen)</TITLE></HEAD>
<BODY>
<H3>Friends list for: <I>%s</I></H3>
Your name is: <B>%s</B><P></P>
You have <B>%s</B> friends.
</BODY>
</HTML>
'''
form = cgi.FieldStorage()
who = form['person'].value
howmany = form['howmany'].value
print(reshtml % (who, who, howmany))
2.目录结构
3.启动服务器
python -m http.server 8000
4.web测试
http://localhost:8000/friends.html
点击“提交”或回车,自动跳转
【原文】
1.从 Python 快速启动 CGI 服务器 https://blog.youkuaiyun.com/weixin_34419326/article/details/92878854