CGI 简单的python显示的页面
Posted eat-too-much
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CGI 简单的python显示的页面相关的知识,希望对你有一定的参考价值。
简介
python 进行服务器的页面的显示 cgi common gateway interface 公用网关接口
简单操作
- python3 -m http.server --cgi 8001
- 新建一个cgi-bin目录
- 里面存入一个 py文件
#!/usr/bin/env python
#coding=utf-8
import cgi
header = 'Content-Type: text/html\n\n'
formhtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE></HEAD>
<BODY><H3>Friends list for: <I>NEW USER</I></H3>
<FORM ACTION:"/cgi-bin/friends2.py">
<B>Enter your Name:</B>
<INPUT TYPE=hidden NAME=action VALUE=edit>
<INPUT TYPE=text NAME=person VALUE="NEW USER" SIZE=15>
<P><B>How many friends do you have?</B>
%s
<P><INPUT TYPE=submit></FORM></BODY></HTML>'''
fradio = '<INPUT TYPE=radio NAME=howmany VALUE="%s" %s> %s\n'
def showForm():
friends = ''
for i in [0, 10, 25, 50, 100]:
checked = ''
if i == 0:
checked = 'CHECKED'
friends = friends + fradio % (str(i), checked, str(i))
print header + formhtml % (friends)
reshtml = '''<HTML><HEAD><TITLE>
Friends CGI Demo</TITLE><HEAD>
<BODY><H3>Friends list for:<I>%s</I></H3>
Your name is: <B>%s</B><p>
You have <B>%s</B> friends.
</BODY></HTML>'''
def doResults(who, howmany):
print header + reshtml %(who, who, howmany)
def process():
form = cgi.FieldStorage()
if form.has_key('person'):
who = form['person'].value
else:
who = 'NEW USER'
if form.has_key('howmany'):
howmany = form['howmany'].value
else:
howmany = 0
if form.has_key('action'):
doResults(who, howmany)
else:
showForm()
if __name__ == '__main__':
process()
访问
http://localhost:8001/cgi-bin/cgii.py
参考链接
以上是关于CGI 简单的python显示的页面的主要内容,如果未能解决你的问题,请参考以下文章
Python CGI 内部错误 login.html login.cgi
python3.x搭建简单CGI服务器时cgi-bin下的脚本无法被解释执行