• 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+ '
    
    '
    
    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+ '
    
    '
    
    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 
    
    ")
    # print("Hello Web Development 
    
    ")
    
    import cgi,cgitb
    form1 = cgi.FieldStorage()
    name=form1.getvalue("name")
    print("Hello: " + name)
    

    文件: python进行cgi编程/views/add.png

    在该目录下运行python2 -m CGIHTTPServer 8081 启动

    X:python3.64Flaskjikexueyuan_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:Python27python2.exe -u X:python3.64Flaskjikexueyuan_flask用Python进行CGI编程cgi-binfirst_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:Python27python2.exe -u X:python3.64Flaskjikexueyuan_flask用Python进行CGI编程cgi-binmain.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()
    
    
    
    写入自己的博客中才能记得长久
  • 相关阅读:
    Asc函数与Chr函数
    IsNumeric 函数
    VB之Collection---Collection集合类
    [转]android.support.v4.app.Fragment和android.app.Fragment区别
    【原创】LogCat信息演示Activity生命周期
    [转]基础总结篇之一:Activity生命周期
    [转]两分钟彻底让你明白Android Activity生命周期(图文)
    [转]Android Studio常用快捷键
    [转] Android 命名规范 (提高代码可以读性)
    [转]Android ListView最佳处理方式,ListView拖动防重复数据显示,单击响应子控件
  • 原文地址:https://www.cnblogs.com/heris/p/14651084.html
Copyright © 2020-2023  润新知