zoukankan      html  css  js  c++  java
  • 一个cgi的例子

    cgi的详细资料可以参考 http://httpd.apache.org/docs/2.4/howto/cgi.html

    下面是一个python实现的cgi脚本,里面体现了一些cgi的用法,使用其他脚本实现也都类似。Python可能有一些更简单的方式,比如cgi库。

    这个例子能够处理get和post两种请求,并获取两种请求的参数。

    #!/usr/bin/python
    #coding=utf-8
    
    import psycopg2
    import os
    import sys
    
    #返回信息中必须有Content-type
    print "Content-type: text/html
    
    "
    
    #通过环境变量REQUEST_METHOD获得请求方法
    if os.environ["REQUEST_METHOD"] == "GET":
        #cgi get请求通过系统变量 QUERY_STRING 来获取参数
        limit = os.environ["QUERY_STRING"].split("=")[1]
    else:
        #cgi post请求从标准输入中获得参数
        limit =  sys.stdin.readline().split("=")[1]
    
    #从数据库中读取数据
    conn = psycopg2.connect(database="svmanager", user="svmanager", password="bT9Mkkhu7XBkz7uBa1UNHx", host="192.168.1.56", port="5432")
    print "=========================="
    
    cur = conn.cursor()
    
    cur.execute("SELECT vm_name , vm_type from  vms limit %s offset 0;" % limit )
    rows = cur.fetchall()
    
    print "<table style='border:1px solid black'>"
    for row in rows:
       print "<tr><td>", row[0], "</td><td>" , row[1]  ,"</td></tr>"
    print "</table>"
    
    print "========================"
    conn.close() 
  • 相关阅读:
    tarjan algorithm
    最小生成树
    Manacher's Algorithm (马拉车算法)
    KMP
    Trie(字典树)
    Hash
    GDB调试
    图论
    扫描线
    STL
  • 原文地址:https://www.cnblogs.com/zh1164/p/6623334.html
Copyright © 2011-2022 走看看