一般的web应用是js + java(serverlet/controller),python也可以充当服务器后台,代码如下:
过程如下:
form表单—>经过服务器解析的【目标文件】 ----->处理提交的数据: 响应页面-打印输入
1, 前后端:流程演示
a,前端: 发送请求
form.html: 提交请求给web目录下的cgi-bin或 htbin的form.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>form表单,数据提交后台python</title>
</head>
<body>
<form method="post" action="http://localhost/cgi-bin/form.py"> <!--目标文件必须在cgi-bin包下,否则服务器不能解析-->
name: <input type="text" name="user"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
python_web_req.py:通过python脚本发送http请求
#通过脚本,发送http请求
from urllib.request import urlopen
#调用方法: 发送http请求
conn=urlopen("http://localhost/cgi-bin/form.py?user=abc123")
res_data=conn.read()
print(res_data)
b, 后台: 处理数据
web_server.py: 启动服务器,解析请求的cgi文件
import os,sys
from http.server import CGIHTTPRequestHandler, HTTPServer
#设置属性
webdir="."
port=80
#启动http 服务
os.chdir(webdir)
server_addr=("",port)
server_obj=HTTPServer(server_addr, CGIHTTPRequestHandler)
server_obj.serve_forever()
form.py: 文件位置:必须在cgi-bin包下,因为含有cgi代码
import cgi
# 字段解析
form_data = cgi.FieldStorage()
if form_data:
user_name = cgi.escape(form_data['user'].value)
print("Content-type:text/html\n")
print("<title>reply page</title>")
# 判断输入的数据
if not 'user' in form_data:
print("<h1> who you are ? </h1> ")
else:
print("<h1> hello %s </h1>" % user_name)
启动服务器,通过浏览器:打开form.html
点击提交: 开始请求后台form.py
或者直接通过python脚本:发送请求,并且接收响应
2, 以web的形式:管理数据
a,前端:发送请求
people.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>people info manage page !</title>
</head>
<body>
<form method="post" action="http://localhost/cgi-bin/people_cgi.py">
key : <input type="text" name="key"><br/><br/>
name: <input type="text" name="name"><br/>
age : <input type="text" name="age"><br/>
job : <input type="text" name="job"><br/>
<input type="submit" value="fetch" name="action">
<input type="submit" value="update" name="action">
</form>
</body>
</html>
b, 后端监听80端口:处理请求
web_server.py: 代码同上
people_cgi.py
#处理web页面请求
import cgi,shelve,sys,os
#从字典文件中读取数据
file_name="../../oop/data/shelve_obj"
db=shelve.open(file_name)
#print(db["obj"]) #teacher对象: t2,43,teacher
#定义属性
fields=("name","age","job")
#解析:输入的数据
params = cgi.FieldStorage()
cwd = os.getcwd() #D:\pycharm\PyCharm 2018.3.4\pyproject\test1\chapter1\gui\web_shelve
sys.path.insert(0,cwd)
#定义方法
def fetch(db,form_param):
try:
key = form_param["key"].value
record = db[key]
field_dict = record.__dict__
field_dict["key"]=key
except:
field_dict=dict.fromkeys(fields,"?")
field_dict["key"]="missing or invalid key !"
return field_dict
#调用方法
if __name__=="__main__":
#解析参数
print("Context-type: text/html\n")
action_method=params["action"].value if "action" in params else None
if action_method=="fetch":
res_data=fetch(db,params)
print("""
<html>
<body>
<form method="post" action="http://localhost/cgi-bin/people_cgi.py">
""")
print('key : <input type="text" name="key" value=%s><br/><br/> '%res_data["key"])
for field in fields:
print('%s : <input type="text" name="key" value=%s><br/> '%(field,res_data[field]))
print("""
</form>
<input type="submit" value="fetch" name="action">
<input type="submit" value="update" name="action">
</body>
</html>
""")
点击fetch, 发送数据请求