python的cgi编程
文件夹如下
文件: python进行cgi编程/cgi-bin/main.py
#!/usr/bin/env python
import cgi,cgitb
from jkxy import *
form1 = cgi.FieldStorage()
num1=form1.getvalue("Num1")
num2=form1.getvalue("Num2")
num3=None
if not num1 is None and not num2 is None:
num1=int(num1)
num2=int(num2)
num3=num1+num2
print (start_response())
print (start_div("center","margin-top:40px;"))
print (img("../views/add.png"))
print (end_div())
print start_div("center","margin-top:60px;")
print start_form()
print input_label("Num1","adder-1")
print "+"
print input_label("Num2","adder-2")
print "="
if num3 is None:
print input_label("Num3","result","","readonly")
else:
print input_label("Num3","result",str(num3),"readonly")
print end_form()
print end_div()
文件: python进行cgi编程/cgi-bin/jkxy.py
def start_response(resp="text/html"):
return 'Content-type: '+resp+ '\n\n'
def start_form(the_url="",form_type="GET"):
return '<form action="'+the_url+'"method="'+form_type+'">'
def end_form(submit_msg="Submit"):
return '<p></p><input type="submit" value="' +submit_msg+'">'
def input_label(name,placeholder="",value="",readonly=None):
if readonly is None:
return '<input type="text" value="'+value+'" name="'+name+'" placeholder="'+placeholder+'">'
else:
return '<input type="text" value="'+value+'" readonly="'+readonly+'" name="'+name+'" placeholder="'+placeholder+'">'
def context(word):
return word
def start_div(align,style):
return '<div align="'+align+'"style="'+style+'">'
def end_div():
return "</div>"
def img(src):
return '<img src="'+src+'">'
文件: python进行cgi编程/cgi-bin/jkxy.py
def start_response(resp="text/html"):
return 'Content-type: '+resp+ '\n\n'
def start_form(the_url="",form_type="GET"):
return '<form action="'+the_url+'"method="'+form_type+'">'
def end_form(submit_msg="Submit"):
return '<p></p><input type="submit" value="' +submit_msg+'">'
def input_label(name,placeholder="",value="",readonly=None):
if readonly is None:
return '<input type="text" value="'+value+'" name="'+name+'" placeholder="'+placeholder+'">'
else:
return '<input type="text" value="'+value+'" readonly="'+readonly+'" name="'+name+'" placeholder="'+placeholder+'">'
def context(word):
return word
def start_div(align,style):
return '<div align="'+align+'"style="'+style+'">'
def end_div():
return "</div>"
def img(src):
return '<img src="'+src+'">'
文件: python进行cgi编程/cgi-bin/first_cgi.py
print("Content-type:text/html \n\n")
# print("Hello Web Development \n\n")
import cgi,cgitb
form1 = cgi.FieldStorage()
name=form1.getvalue("name")
print("Hello: " + name)
文件: python进行cgi编程/views/add.png
在该目录下运行python2 -m CGIHTTPServer 8081 启动
X:\python3.64\Flask\jikexueyuan_flask\用Python进行CGI编程>python2 -m CGIHTTPServer 8081
Serving HTTP on 0.0.0.0 port 8081 ...
127.0.0.1 - - [13/Apr/2021 14:43:14] "GET /cgi-bin/first_cgi.py?name=sdafrt356555 HTTP/1.1" 200 -
127.0.0.1 - - [13/Apr/2021 14:43:14] command: C:\Python27\python2.exe -u X:\python3.64\Flask\jikexueyuan_flask\用Python进行CGI编程\cgi-bin\first_cgi.p
y
127.0.0.1 - - [13/Apr/2021 14:43:15] CGI script exited OK
127.0.0.1 - - [13/Apr/2021 14:43:32] "GET /cgi-bin/main.py HTTP/1.1" 200 -
127.0.0.1 - - [13/Apr/2021 14:43:32] command: C:\Python27\python2.exe -u X:\python3.64\Flask\jikexueyuan_flask\用Python进行CGI编程\cgi-bin\main.py ""
127.0.0.1 - - [13/Apr/2021 14:43:33] CGI script exited OK
浏览器输入与显示
用原生的wsgi编程,只能在python2上运行
#_*_ encoding: utf-8 _*_ @author: ty hery 2018/12/29
def application(environ, start_response):
response_body = "<h1> Hello World </h1>"
header =[('Content-Type', 'text/html')]
status = '200 OK'
start_response(status, header)
print("environ http request method:"+ environ['REQUEST_METHOD'])
return [response_body]
# 这个只能在python2上面运行
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server("0.0.0.0", 8000, application)
print("httpd run on :"+ str(httpd.server_port))
httpd.serve_forever()
写入自己的博客中才能记得长久