python CGI编程-part1

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

2、创建web服务器程序 :run_server.py

# -*- coding: utf-8 -*-
"""
Created on Wed Sep  9 09:38:52 2020
Web服务器
所有的Web应用都要在Web服务器上运行,
实际上所有的web服务器都支持CGI,
无论是Apache、IIS、nginx、Lighttpd还是其他服务器,
它们都支持用python编写的cgi脚本。这些web服务器都比较强大,
这里我们使用python自带的简单的web服务器,
这个web服务器包含在http.server库模块中。
@author: hanxiao
"""
from http.server import HTTPServer, CGIHTTPRequestHandler

port = 8099

httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

3、第一个CGI程序:firstCGI.py

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 31 16:58:52 2021

@author:hanxiao
第一个CGI程序
首先启动run_server.py(web服务器)
之后浏览器访问http://localhost:8099/cgi-bin/firstCGI.py
"""

print ("Content-type:text/html")
print ()                             # 空行,告诉服务器结束头部
print ('<html>')
print ('<head>')
print ('<meta charset="gb2312">') #使用utf-8中文乱码
print ('<title>Hello Word - 我的第一个 CGI 程序!</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! 我是第一CGI程序</h2>')
print ('</body>')
print ('</html>')

(1)、首先启动run_server.py(web服务器)
(2)、之后浏览器访问http://localhost:8099/cgi-bin/firstCGI.py

(3)、HTTP头部

4、第二个CGI程序:secondCGI.py(输出环境变量)

# -*- coding: utf-8 -*-
"""
Created on Wed Mar 31 17:18:26 2021

@author: 韩笑
输出CGI的环境变量
"""
import os

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

(1)、浏览器访问http://localhost:8099/cgi-bin/secondCGI.py

(2)、CGI环境变量

5、GET和POST方法:浏览器客户端通过两种方法向服务器传递信息,这两种方法就是 GET 方法和 POST 方法。

6、使用get方法传输数据GET方法发送编码后的用户信息到服务端,数据信息包含在请求页面的URL上,以"?"号分割, 如下所示:

http://www.test.com/cgi-bin/firstCGI_get.py?key1=value1&key2=value2

有关 GET 请求的其他一些注释:

  • GET 请求可被缓存

  • GET 请求保留在浏览器历史记录中

  • GET 请求可被收藏为书签

  • GET 请求不应在处理敏感数据时使用

  • GET 请求有长度限制

  • GET 请求只应当用于取回数据

第三个CGI程序:firstCGI_get.py

# -*- coding: utf-8 -*-
"""
Created on Fri Apr  2 11:02:26 2021

@author: hx
简单的url实例:GET方法
"""
# CGI处理模块
import cgi,cgitb 

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

# 获取数据
site_name = form.getvalue('name')
site_url  = form.getvalue('url')

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset=\"gb2312\">")
print ("<title>CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2>%s官网:%s</h2>" % (site_name, site_url))
print ("</body>")
print ("</html>")

浏览器访问:http://localhost:8099/cgi-bin/firstCGI_get.py?name=百度&url=https://www.baidu.com/

7、简单的表单实例:GET方法

通过HTML的表单使用GET方法向服务器发送两个数据,提交的服务器脚本同样是firstCGI_get.py文件,firstCGI_get.html 代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>get方法简单的表单实例</title>
</head>
<body>
<form action="/cgi-bin/firstCGI_get.py" method="get">
站点名称: <input type="text" name="name">  <br />

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

浏览器访问:http://localhost:8099/test/firstCGI_get.html

点击”提交“按钮

8、使用post方法传递数据:使用POST方法向服务器传递数据是更安全可靠的,像一些敏感信息如用户密码等需要使用POST传输数据。

以下同样是firstCGI_get.py ,它也可以处理浏览器提交的POST表单数据,post相关html代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>post方法简单的表单实例</title>
</head>
<body>
<form action="/cgi-bin/firstCGI_get.py" method="post">
站点名称: <input type="text" name="name">  <br />

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

浏览器访问:http://localhost:8099/test/firstCGI_post.html

点击”提交“按钮,红框处没有入参的显示

9、通过CGI程序传递checkbox数据

checkbox相关HTML代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>checkbox前台</title>
</head>
<body>
<form action="/cgi-bin/checkbox.py" method="POST" target="_blank">
<input type="checkbox" name="baidu" value="on" /> 百度
<input type="checkbox" name="google" value="on" /> Google
<input type="submit" value="选择站点" />
</form>
</body>
</html>

checkbox.py代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  4 11:01:02 2021

@author: hx
接收checkbox数据
"""
# 引入 CGI 处理模块 
import cgi, cgitb 

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

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

if form.getvalue('baidu'):
   runoob_flag = "是"
else:
   runoob_flag = "否"

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset=\"gb2312\">")
print ("<title>CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2> 百度是否选择了 : %s</h2>" % runoob_flag)
print ("<h2> Google 是否选择了 : %s</h2>" % google_flag)
print ("</body>")
print ("</html>")

浏览器访问:http://localhost:8099/test/checkbox.html

勾选想要选择的站点,点击”选择站点“按钮

10、通过CGI程序传递Radio数据

radio.html代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>radio页面</title>
</head>
<body>
<form action="/cgi-bin/radio.py" method="post" target="_blank">
<input type="radio" name="site" value="baidu" /> 百度
<input type="radio" name="site" value="google" /> Google
<input type="submit" value="提交" />
</form>
</body>
</html>

radio.py代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  4 14:30:43 2021

@author: hx
CGI程序传递Radio数据
"""
# 引入 CGI 处理模块 
import cgi, cgitb 

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

# 接收字段数据
if form.getvalue('site'):
   site = form.getvalue('site')
else:
   site = "提交数据为空"

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset=\"gb2312\">")
print ("<title>CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2> 选中的网站是 %s</h2>" % site)
print ("</body>")
print ("</html>")

浏览器访问:http://localhost:8099/test/radio.html

选中一项,点击”提交“按钮

11、CGI程序传递Textarea数据

textarea.html代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>textarea页面</title>
</head>
<body>
<form action="/cgi-bin/textarea.py" method="post" target="_blank">
<textarea name="textcontent" cols="40" rows="4">
在这里输入内容...
</textarea>
<input type="submit" value="提交" />
</form>
</body>
</html>

textarea.py代码如下所示:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  4 14:46:48 2021

@author: hx
CGI程序传递Textarea数据
"""
# 引入 CGI 处理模块 
import cgi, cgitb 

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

# 接收字段数据
if form.getvalue('textcontent'):
   text_content = form.getvalue('textcontent')
else:
   text_content = "没有内容"

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset=\"gb2312\">")
print ("<title>CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2> 输入的内容是:%s</h2>" % text_content)
print ("</body>")
print ("</html>")

浏览器访问:http://localhost:8099/test/textarea.html

在文本框中输入内容,点击”提交“按钮

12、通过CGI程序传递下拉数据。

dropdown.html代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dropdown页面</title>
</head>
<body>
<form action="/cgi-bin/dropdown.py" method="post" target="_blank">
<select name="dropdown">
<option value="baidu" selected>百度</option>
<option value="google">Google</option>
</select>
<input type="submit" value="提交"/>
</form>
</body>
</html>

dropdown.py代码如下:

# -*- coding: utf-8 -*-
"""
Created on Sun Apr  4 15:06:48 2021

@author: hx
通过CGI程序传递下拉数据。
"""

# 引入 CGI 处理模块 
import cgi, cgitb 

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

# 接收字段数据
if form.getvalue('dropdown'):
   dropdown_value = form.getvalue('dropdown')
else:
   dropdown_value = "没有内容"

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<meta charset=\"gb2312\">")
print ("<title>CGI 测试实例</title>")
print ("</head>")
print ("<body>")
print ("<h2> 选中的选项是:%s</h2>" % dropdown_value)
print ("</body>")
print ("</html>")

浏览器访问:http://localhost:8099/test/dropdown.html

下拉框选好之后,点击”提交“按钮

喜欢我的文章希望和我一起成长的宝宝们,可以搜索并添加公众号TryTestwonderful ,或者扫描下方二维码添加公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半夏映浮光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值