- 是最后的demo,要对CGI告一段落啦
- 写了一个比较完整的,包括前后端的,用户登录注册-写留言-查看留言板的过程
最终效果
-
需要编写美化后的登录界面new_login.html、注册界面new_register.html和发布留言界面message.html,以及一些用来美化css、js等
-
需要编写四个cgi脚本:验证用户登录注册的new_login_sql.py和new_register_sql.py,对用户输入留言时进行数据库增删改查的message_sql.py,以及展示留言板数据库中保存的留言的messagelist_sql.py
-
登录界面:
-
注册界面:
-
发布留言界面:
-
留言版列表界面:
第一步:建数据库!
-
建了一个articles库,内容如下:
- ok!建完库开始代码
第二步:编写美化的前端界面
- 包括①登录界面new_login.html、②注册界面new_register.html和③发布留言界面message.html,以及若干css、js等,界面美化大同小异,代码太长啦,所以就只放核心html代码吧:
01 登录界面new_login.html
<form action="http://localhost/cgi-bin/new_login_sql.py" method="post" class="form">
<div class="content">
<div class="input-group form-group-no-border input-lg">
<span class="input-group-addon">
<i class="now-ui-icons users_circle-08"></i>
</span>
<input type="text" class="form-control" name="uname" placeholder="请输入用户名...">
</div>
<div class="input-group form-group-no-border input-lg">
<span class="input-group-addon">
<i class="now-ui-icons text_caps-small"></i>
</span>
<input type="password" name="upass" placeholder="请输入密码..." class="form-control"/>
</div>
</div>
<div class="footer text-center">
<input class="btn btn-primary btn-round btn-lg btn-block" type="submit" value="登录">
</div>
</form>
<div class="pull-left">
<h6>
<a href="#" class="link">需要帮助?</a>
</h6>
</div>
<div class="pull-right">
<h6>
<a href="new_register.html" class="link">注册账号</a>
</h6>
</div>
02 注册界面new_register.html
<form action="http://localhost/cgi-bin/new_register_sql.py" method="post" class="form">
<div class="content">
<div class="input-group form-group-no-border input-lg">
<span class="input-group-addon">
<i class="now-ui-icons users_circle-08"></i>
</span>
<input type="text" class="form-control" name="rname" placeholder="请输入用户名...">
</div>
<div class="input-group form-group-no-border input-lg">
<span class="input-group-addon">
<i class="now-ui-icons text_caps-small"></i>
</span>
<input type="password" name="rpass" placeholder="请输入密码..." class="form-control"/>
</div>
</div>
<div class="footer text-center">
<input class="btn btn-primary btn-round btn-lg btn-block" type="submit" value="注册">
</div>
</form>
<div class="pull-left">
<h6>
<a href="#" class="link">需要帮助?</a>
</h6>
</div>
<div class="pull-right">
<h6>
<a href="new_login.html" class="link">已有账号</a>
</h6>
</div>
03 发布留言界面message.html
<form action="http://localhost/cgi-bin/message_sql.py" method="post" target="_blank" class="form">
<div class="content">
<div class="input-group form-group-no-border input-lg">
<textarea class="form-control" name="textcontent" cols="40" rows="4" placeholder="请输入留言" maxlength="20" style="height:180px;border-radius:4px;"></textarea>
</div>
</div>
<div class="footer text-center">
<input class="btn btn-primary btn-round btn-lg btn-block" type="submit" value="发布留言">
</div>
</form>
<div class="pull-center">
<h6>
<a href="http://localhost/cgi-bin/messagelist_sql.py" class="link">️✨查看留言板✨</a>
</h6>
</div>
第三步:编写cgi脚本
- 这才是重中之重呀,需要编写如下四个cgi脚本:①验证用户登录注册的new_login_sql.py和②new_register_sql.py,③对用户输入留言时进行数据库增删改查的message_sql.py,④以及展示留言板数据库中保存的留言的messagelist_sql.py
01 验证用户登录的new_login_sql.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# CGI处理模块
import cgi
# 我的电脑由于种种不可知原因,即便给电脑中存在每个版本的python都import了pymysql,运行起来cgi还是会报错无pymysql,最后只好使用引用绝对路径的方式引入pymysql
# 见仁见智,如果直接import pymysql成功则不需引入sys这两句
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages")
import pymysql
# 创建 FieldStorage 的实例化
form = cgi.FieldStorage()
#获取前端用户输入的数据
uname = form.getvalue("uname")
upass = form.getvalue("upass")
#连接数据库
#这里的user,password,db都是各有不同的蛤,需要修改你自己的库信息
con = pymysql.connect(host='localhost', user='root', password='cyj123', db='pydemo', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with con.cursor() as cursor:
#查找
sql = "select password from users where email = %s"
cursor.execute(sql, str(uname))
con.commit()
result = cursor.fetchone()
print("Content-type:text/html")
print('')
print('<html>')
print('<head>')
print('<meta charset="utf-8">')
#判断账户密码是否正确
if result == None or upass != result["password"]:
print('<meta http-equiv="Refresh" content="1;url=/new_login/new_login.html">')
print('<title>登录</title>')
print('</head>')
print('<body>')
print('<h>用户名或密码错误!正在跳转至重新登录界面...</h>')
else:
print('<meta http-equiv="Refresh" content="1;url=/new_login/message.html">')
print('<title>登录</title>')
print('</head>')
print('<body>')
print('<h>登录成功!正在跳转至留言页...<h>')
print('</body>')
print('</html>')
finally:
con.close()
02 验证用户注册的new_register_sql.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# CGI处理模块
import cgi
# 我的电脑由于种种不可知原因,即便给电脑中存在每个版本的python都import了pymysql,运行起来cgi还是会报错无pymysql,最后只好使用引用绝对路径的方式引入pymysql
# 见仁见智,如果直接import pymysql成功则不需引入sys这两句
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages")
import pymysql
# 创建 FieldStorage 的实例化
form = cgi.FieldStorage()
#获取前端用户输入的数据
rname = form.getvalue("rname")
rpass = form.getvalue("rpass")
#连接数据库
#这里的user,password,db都是各有不同的蛤,需要修改你自己的库信息
con = pymysql.connect(host='localhost', user='root', password='cyj123', db='pydemo', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
#判断是否输入空信息
if rname == None or rpass == None:
print("Content-type:text/html")
print('')
print('<html>')
print('<head>')
print('<meta charset="utf-8">')
print('<meta http-equiv="Refresh" content="3;url=/new_register/new_register.html">')
print('<title>注册</title>')
print('</head>')
print('<body>')
print('<h>用户名或密码不得为空!正在跳转至重新注册页面...<h>')
print('</body>')
print('</html>')
else:
try:
with con.cursor() as cursor:
#先查询是否已有相同账户
sql = "select * from users where email = %s"
cursor.execute(sql, str(rname))
con.commit()
result = cursor.fetchone()
print("Content-type:text/html")
print('')
print('<html>')
print('<head>')
print('<meta charset="utf-8">')
#再添加
if result == None:
sql = "insert into users (email,password) values (%s,%s)"
cursor.execute(sql, (str(rname), str(rpass)))
con.commit()
print('<meta http-equiv="Refresh" content="1;url=/new_login/new_login.html">')
print('<title>注册</title>')
print('</head>')
print('<body>')
print('<h>注册成功,正在跳转至登录页面...</h>')
else:
print('<meta http-equiv="Refresh" content="1;url=/new_login/new_register.html">')
print('<title>注册</title>')
print('</head>')
print('<body>')
print('<h>此账号已存在!正在跳转至重新注册页面...<h>')
print('</body>')
print('</html>')
finally:
con.close()
03 对用户输入留言时进行数据库增删改查的message_sql.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# CGI处理模块
import cgi, cgitb
import datetime
# 我的电脑由于种种不可知原因,即便给电脑中存在每个版本的python都import了pymysql,运行起来cgi还是会报错无pymysql,最后只好使用引用绝对路径的方式引入pymysql
# 见仁见智,如果直接import pymysql成功则不需引入sys这两句
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages")
import pymysql
# 创建 FieldStorage 的实例化
form = cgi.FieldStorage()
# 接收字段数据
if form.getvalue('textcontent'):
text_content = form.getvalue('textcontent')
else:
text_content = "没有内容"
#连接数据库
#这里的user,password,db都是各有不同的蛤,需要修改你自己的库信息
con = pymysql.connect(host='localhost', user='root', password='cyj123', db='pydemo', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print("Content-type:text/html")
print('')
print('<html>')
print('<head>')
print('<meta charset="utf-8">')
try:
with con.cursor() as cursor:
# 写
sql = "insert into articles (content,time) values (%s,%s)"
now = datetime.datetime.now()
now = now.strftime("%Y-%m-%d %H:%M:%S")
cursor.execute(sql, (str(text_content),now))
con.commit()
print('<meta http-equiv="Refresh" content="3;url= messagelist_sql.py">')
print('<title>留言板</title>')
print('</head>')
print('<body>')
print('发布成功,正在跳转至留言板...')
finally:
con.close()
print('</body>')
print('</html>')
04 展示留言板数据库中保存的留言的messagelist_sql.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# CGI处理模块
import cgi, cgitb
import datetime
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages")
import pymysql
reload(sys)
#重新设置字符集
sys.setdefaultencoding("utf-8")
#连接数据库
#这里的user,password,db都是各有不同的蛤,需要修改你自己的库信息
con = pymysql.connect(host='localhost', user='root', password='cyj123', db='pydemo', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print("Content-type:text/html")
print('')
print('<html>')
print('<head>')
print('<meta charset="utf-8">')
print('<title>Message List</title>')
print('<link href="/new_login/assets/css/bootstrap.min.css" rel="stylesheet"/>')
print('<link href="/new_login/assets/css/now-ui-kit.css" rel="stylesheet"/>')
print('</head>')
print('<body class="login-page sidebar-collapse">')
print('<div class="page-header" filter-color="orange">')
print('<div class="page-header-image">')
print('<img src="/new_login/assets/img/bg11.jpg">')
print('</div>')
print('<div style="width: 100%;height: 600px;position: relative;">')
print('<h3 style="text-align:center;padding-top:80px">留言板</h3>')
print('<div style="width: 30%;height: 300px;position: absolute;top: 0;right: 0;bottom: 0;left: 0;margin: auto;overflow: scroll;">')
print('<div style="height="100px";">')
try:
with con.cursor() as cursor:
# 读
sql = "select content,time from articles"
cursor.execute(sql)
result = cursor.fetchall()
for i in result:
print('<div style="padding-top:10px;padding-left:10px;padding-right:10px;height:75px;background-color: green;background-color:rgba(250,250,250,0.1);border-radius:4px">')
print('%s'%i['content'])
print('<div style="float:right">')
print('<br/>')
print('%s'%i['time'])
print('</div>')
print('</div>')
print('<br/>')
finally:
con.close()
print('</div>')
print('</div>')
print('<h1 style="text-align:center;margin-top:350px"><a href="/new_login/message.html" class="link" style="font-size: 14px">✏️️发布留言✏️️</a></h1>')
print('</div>')
print('</div>')
print('</div>')
print('</body>')
print('</html>')
结束啦!完结撒花!✿✿ヽ(°▽°)ノ✿
-
可以看到数据库中存入了数据
- 结束!