CGI简单介绍

一、什么是cgi

最早的Web服务器简单地响应浏览器发来的HTTP请求,并将存储在服务器上的HTML文件返回给浏览器,也就是静态html。事物总是不断发展,网站也越来越复杂,所以出现动态技术。但是服务器并不能直接运行 php,asp这样的文件,自己不能做,外包给别人吧,但是要与第三做个约定,我给你什么,然后你给我什么,就是握把请求参数发送给你,然后我接收你的处理结果给客户端。那这个约定就是 common gateway interface,简称cgi。这个协议可以用vb,c,php,python 来实现。cgi只是接口协议,根本不是什么语言。

简单总结:通用网关接口,用来解析后端脚本。

Python 自带轻量级的cgi web服务器,我们在对应目录下可以通过命令进行启动

Python -m http.server --cgi [port] 端口默认8000

通常服务器会自动解析根目录下的index.html文件,所以我们可以默认访问到index.html

在浏览器当做cgi的ip+端口代表根目录

二、Cgi可以解析python脚本作为HTML

1、需要在cgi的根目录下创建cgi-bin目录

2、需要用print的方法来返回HTML的头信息

print("Content-type:text/html;charset:utf8") #响应头

#content-type:返回内容的类型

# text/html文本

# application/json json文本

# image/jpg jpg图片

print("\n") #声明响应头结束

print(html) #返回内容的正文

3、cgi.FieldStorage可以接收前端传递过来的get或者post请求的数据,我们需要用getvalue方 法通过前端form表达的name来取到input的值。

案例:通过cgi解析编写的python语言作为HTML

首先建立如图所示文件夹及文件

编写一些html文件

其中saveData.py具体内容为:

import cgi
import pymysql

html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="gbk">
    <title>Title</title>
    <style>
        
        h1{
            width: 200px;
            height: 200px;
            text-align: center;
            line-height: 200px;
            color: #2aabd2;
        }
    </style>
</head>
<body>
    <h1>hello world</h1>
    <ul>
        %s
    </ul>
</body>
</html>
"""
requestData = cgi.FieldStorage()
username = requestData.getvalue("username")
age = requestData.getvalue('age')
if username and age:
    sql = 'INSERT INTO person(names,age) VALUE("%s","%s")'%(username,age)
    try:
        db = pymysql.connect(
            host="localhost",
            user="root",
            password="1234",
            database="school",
            port=3306,
            charset='utf8'
        )
        cursor = db.cursor()
        exe = cursor.execute(sql)

        db.commit()
        cursor.close()
        db.close()
    except Exception as e:
        html = html % str(e)
    else:
        html = html % "保存成功,<a href='index.py'>点我跳转</a>"

print("Content-type:text/html;charset:gbk")
print("\n")
print(html)

index.py文件具体内容为:

import pymysql

# 创建数据库连接
db = pymysql.connect(
    host="localhost",
    user="root",
    password="1234",
    database="school",
    port=3306,
    charset="utf8",
)

# 创建游标:游标用于传递python 给mysql的命令和mysql返回的内容
cursor = db.cursor()
exe = cursor.execute("select names,age from person")
# result = cursor.fetchone()
# result=cursor.fetchmany()
result = cursor.fetchall()
# print(result)
db.commit()
cursor.close()
db.close()
li = ""
for tu in result:
    li += "<li>姓名:%s &nbsp&nbsp&nbsp 年龄:%s</li>" % tu

html = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="gbk">
    <title>Title</title>
    <style>
        ul li{
            list-style:none
        }
        h1{
            width: 200px;
            height: 200px;
            text-align: center;
            line-height: 200px;
            color: #2aabd2;
        }
    </style>
</head>
<body>
    <h1>hello world</h1>
    <ul>%s</ul>
</body>
</html>
""" % li
print("Content-type:text/html;charset:gbk")
print("\n")
print(html)

打开index.py文件

打开浏览器

猜你喜欢

转载自blog.csdn.net/weixin_44239541/article/details/88563011
CGI