python CGI编程

python CGI编程

###什么是CGI

CGI(Commom Gateway Interface)通用网关接口,它是一段程序,运行在服务器。如HTTP服务器,提供同客户端HTML页面的接口。

网页浏览

通过在网页上点击一个链接或URL的流程了解CGI是如何工作的

  1. 使用浏览器访问URL连接到HTTP web 服务器
  2. web 服务器接收到请求以后解析URL,并查找访问文件是否在HTTP服务器上存在,如果存在返回内容,不存在返回错误信息
  3. CGI程序可以是python脚本,Perl脚本,shell脚本,C或C++程序等

CGI架构图及其本地环境配置

  • 采用PHPstudy+windows测试,修改httpd.conf配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 查找一下自己的DocumentRoot 
    DocumentRoot "E:\Environment\phpstudy\PHPTutorial\WWW"

    <Directory />
    AllowOverride All
    Options +ExecCGI
    Order allow,deny
    Allow from all
    #Require all granted
    </Directory>
    #让apache识别py文件为cgi程序:
    AddHandler cgi-script .cgi .py

    只允许在特别目录下执行cgi程序:
    ScriptAlias /cgi-bin/ "D:/Program/phpStudy/Apache/cgi-bin/"
  • 测试实例(python不用指定编码,测试了时候指定编码出现乱码)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #!D:/python36/python.exe

    print ("Content-type:text/html")
    print () # 空行,告诉服务器结束头部
    print ('<html>')
    print ('<head>')
    # print ('<meta charset="UTF-8">')
    print ('<title>Hello Word 你好123!</title>')
    print ('</head>')
    print ('<body>')
    print ('<h2> Hello Word! 菜鸟123</h2>')
    print ('</body>')
    print ('</html>')

    #作为http协议的要求,一定要输出http headers
    #在存在http headers的前提下,一定要在headers后面打印一个空行,否则服务器会报错

CGI HTTP头部及CGI环境变量

CGI实例

  1. 通过CGI输出CGI环境变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #!D:/python36/python.exe

    import os
    print("Content-type: text/html")
    print()
    print("<b>环境变量</b><br>")
    print("<ul>")
    for i in os.environ.keys():
    print("<li><span style='color:green'>%30s</span> : %s</li>" % (i,os.environ[i]))
    print("</ul>")

  2. 通过CGI实现GET传递信息(也可以通过构造表单的方法实现GET信息传递,默认情况下只存放脚本文件)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #!D:/python36/python.exe

    import cgi, cgitb

    #创建filedstorage的实例、
    form = cgi.FieldStorage()

    #获取数据
    s_name = form.getvalue('name')
    s_url = form.getvalue('url')

    print("Content-type:text/html")
    print()
    print("<html>")
    print("<head>")
    print("<title>测试</title>")
    print("</head>")
    print("<body>")
    print("<h2>%s博客:%s</h2>" % (s_name,s_url))
    print("</body>")
    print("</html>")

  3. 通过CGI用POST传递数据(使用上面的GET脚本结合表单来POST传递)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>菜鸟</title>
    </head>
    <body>
    <form action="get1.py" method="post">
    站点名称: <input type="text" name="name"> <br />

    站点 URL: <input type="text" name="url" />
    <input type="submit" value="提交" />
    </form>
    </body>
    </html>
    </form>

  4. 通过CGI程序来传递CheckBox数据(需要一个表单和CGI处理的脚本文件)

    • cgi脚本文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      #!D:/python36/python.exe

      import cgi, cgitb

      form = cgi.FieldStorage()

      #接收字段数据
      if form.getvalue('google'):
      google_flag = "是"
      else:
      google_flag = "否"

      if form.getvalue('lsowl'):
      lsowl_flag = "是"
      else:
      lsowl_flag = "否"
      print("Content-type:text/html")
      print()
      print("<html>")
      print("<head>")
      print("<title>测试</title>")
      print("</head>")
      print("<body>")
      print("<h2>lsowl是否选择了 : %s</h2>" % lsowl_flag)
      print("<h2>google是否选择了 : %s</h2>" % google_flag)
      print("</body>")
      print("</html>")
    • 传递数据的表单

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <title>测试</title>
      </head>
      <body>
      <form action="checkbox.py" method="POST" target="_blank">
      <input type="checkbox" name="lsowl" value="on" /> lsowl
      <input type="checkbox" name="google"value="on" /> google
      <input type="submit" value="选择站点"/>
      </form>
      </body>
      </html>

  5. 通过CGI传递Radio数据(只向服务器传输一个数据)

    • 传递Radio数据的CGI脚本

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      #!D:/python36/python.exe

      import cgi, cgitb

      form = cgi.FieldStorage()

      if form.getvalue('site'):
      site = form.getvalue('site')
      else:
      site = '提交的数据为空'

      print ("Content-type:text/html")
      print ()
      print ("<html>")
      print ("<head>")
      print ("<title>测试</title>")
      print ("</head>")
      print ("<body>")
      print ("<h2> 选中的网站是 %s</h2>" % site)
      print ("</body>")
      print ("</html>")
    • 传递Radio数据的CGI表单

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <title>菜鸟教程(runoob.com)</title>
      </head>
      <body>
      <form action="radion.py" method="post" target="_blank">
      <input type="radio" name="site" value="lsowl" /> lsowl
      <input type="radio" name="site" value="google" /> Google
      <input type="submit" value="提交" />
      </form>
      </body>
      </html>

  6. 通过CGI程序传递 Textarea 数据、下拉数据等、设置cookie、上传下载文件,这些只需要改一些HTML方法就可以实现,和上面的本质上没有什么区别

总结

本文是对python的CGI编程的一个学习,学习了CGI及python CGI编程的特性,通过CGI结合前端实现许多功能,由于后面上传下载文件这些只是修改一些方法和HTML元素,本质上差别不大,就没有写完了,可以参考链接

tp://www.runoob.com/python3/python3-cgi-programming.html