- 在實(shí)戰(zhàn)二的基礎(chǔ)上蔫巩,加上PyMySQL數(shù)據(jù)庫對(duì)用戶登錄注冊的操作
最終效果
- 建立數(shù)據(jù)庫pydemo,前端還是用實(shí)戰(zhàn)二那兩個(gè)界面(login.html和register.html)快压,編寫login_sql.py和register_sql.py兩個(gè)CGI腳本文件進(jìn)行數(shù)據(jù)庫的操作
第一步:建數(shù)據(jù)庫圆仔!
PyMySQL的安裝配置網(wǎng)上有挺多帖子的,如果建表也有很多介紹蔫劣,這里就不贅述了
因?yàn)樘珣械么虼a來建表了坪郭,推薦可以使用這些傻瓜式建表的數(shù)據(jù)庫可視化工具:我使用的是Navicat Premium,安裝配置也可自行搜索~
-
建立了一個(gè)pydemo庫脉幢,設(shè)計(jì)了users表歪沃,表中包含的內(nèi)容如下:
image.png ok!完成建表后就可以進(jìn)入編寫代碼嫌松!
第二步:小小修改login.html & register.html
-
其實(shí)就是修改下點(diǎn)擊提交后沪曙,觸發(fā)的cgi文件路徑,將名字改成我們一會(huì)要編寫的這兩個(gè)cgi
image.png
image.png
第三步:編寫判斷用戶登錄的腳本文件 login_sql.py
- 這里我遇到了個(gè)很大的關(guān)于import pymysql的坑萎羔,在pycharm里運(yùn)行不報(bào)錯(cuò)液走,一到打開cgi就報(bào)錯(cuò),查看apache的日志,報(bào)“沒有pymysql這個(gè)包”的錯(cuò)誤育灸,看到一個(gè)帖子也遇到了這樣的問題腻窒,指出是pycharm運(yùn)行的py版本是它自己的,而cgi似乎是運(yùn)行系統(tǒng)默認(rèn)那個(gè)py版本
- 于是在終端install pymysql折騰了好久磅崭,給電腦里每個(gè)版本的python都安裝了pymysql儿子,但還是報(bào)錯(cuò)QAQ,最后互聯(lián)網(wǎng)海洋撈針砸喻,發(fā)現(xiàn)了import sys的方法引入pymysql柔逼!
- 希望大家都比我好運(yùn)蛤,不會(huì)遇到那么多奇妙bug
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# CGI處理模塊
import cgi
# 我的電腦由于種種不可知原因割岛,即便給電腦中存在每個(gè)版本的python都import了pymysql愉适,運(yùn)行起來cgi還是會(huì)報(bào)錯(cuò)無pymysql,最后只好使用引用絕對(duì)路徑的方式引入pymysql
# 見仁見智癣漆,如果直接import pymysql成功則不需引入sys這兩句
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages")
import pymysql
# 創(chuàng)建 FieldStorage 的實(shí)例化
form = cgi.FieldStorage()
#獲取前端用戶輸入的數(shù)據(jù)
uname = form.getvalue("uname")
upass = form.getvalue("upass")
#連接數(shù)據(jù)庫
#這里的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="3;url=/login.html">')
print('<title>登錄</title>')
print('</head>')
print('<body>')
print('<h>用戶名或密碼錯(cuò)誤癌蓖!正在跳轉(zhuǎn)至重新登錄界面...</h>')
else:
print('<title>登錄</title>')
print('</head>')
print('<body>')
print('<h>登錄成功!<h>')
print('</body>')
print('</html>')
finally:
con.close()
第四步:編寫判斷用戶注冊的腳本文件 register_sql.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# CGI處理模塊
import cgi
# 我的電腦由于種種不可知原因婚肆,即便給電腦中存在每個(gè)版本的python都import了pymysql租副,運(yùn)行起來cgi還是會(huì)報(bào)錯(cuò)無pymysql,最后只好使用引用絕對(duì)路徑的方式引入pymysql
# 見仁見智较性,如果直接import pymysql成功則不需引入sys這兩句
import sys
sys.path.append("/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages")
import pymysql
# 創(chuàng)建 FieldStorage 的實(shí)例化
form = cgi.FieldStorage()
#獲取前端用戶輸入的數(shù)據(jù)
rname = form.getvalue("rname")
rpass = form.getvalue("rpass")
#連接數(shù)據(jù)庫
#這里的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=/register.html">')
print('<title>注冊</title>')
print('</head>')
print('<body>')
print('<h>用戶名或密碼不得為空责循!正在跳轉(zhuǎn)至重新注冊頁面...<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">')
#若無,再進(jìn)行添加操作
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=/login.html">')
print('<title>注冊</title>')
print('</head>')
print('<body>')
print('<h>注冊成功攀操!正在跳轉(zhuǎn)至登錄頁面...</h>')
else:
print('<meta http-equiv="Refresh" content="1;url=/register.html">')
print('<title>注冊</title>')
print('</head>')
print('<body>')
print('<h>此賬號(hào)已存在院仿!正在跳轉(zhuǎn)至重新注冊頁面...<h>')
print('</body>')
print('</html>')
finally:
con.close()
結(jié)束!
run一下看看有無哪有問題叭崔赌!