Python_电子词典

功能说明:

  • 用户可注册用户,要求用户名不能重复
  • 用户登陆后可查询单词,查询历史记录,清空历史记录

数据库结构:

  • word表 保存单词及解释
CREATE TABLE `words` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `word` char(30) DEFAULT NULL,
  `mean` text,
  PRIMARY KEY (`id`)
) 
  • user表 保存用户名和密码
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) NOT NULL,
  `passwd` char(64) NOT NULL,
  PRIMARY KEY (`id`)
) 
  • hist表 保存历史记录
CREATE TABLE `hist` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) DEFAULT NULL,
  `word` varchar(30) DEFAULT NULL,
  `time` datetime DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
)

客户端

"""
    客户端
"""
# 导入相关模块
from socket import *
import sys

# 绑定服务端地址
HOST = "127.0.0.1"
PORT = 8888
ADDR = (HOST, PORT)


# 发送查询单词请求
def do_query(s):
    # 循环查询单词
    while True:
        word = input("=========输入##退出查询========\n查询单词:")
        if word == "##":
            break
        msg = "Q %s" % word
        s.send(msg.encode())
        # 等待结果并处理
        data = s.recv(1024).decode()
        print(data)


# 发送查询历史请求
def do_history(s):
    s.send(b"H")
    # 等待结果并处理
    data = s.recv(1024).decode()
    print(data)


# 发送清空历史请求
def clear_history(s):
    s.send(b"C")
    # 等待结果并处理
    feedback = s.recv(1024).decode()
    if feedback == "OK":
        print("已清空查询历史!")
    else:
        print("发生错误!")


# 二级界面
def second_view(s):
    # 二级界面循环
    while True:
        print("=================查询==================")
        print("1:查单词  2:查历史   3:清历史 4:注销")
        print("=======================================")
        cmd2 = input(">>>")
        if cmd2 == "1":
            do_query(s)
        elif cmd2 == "2":
            do_history(s)
        elif cmd2 == "3":
            clear_history(s)
        elif cmd2 == "4":
            return
        else:
            print("输入错误!")


# 发送注册请求
def do_register(s):
    print("==============注册================")
    name = input("输入用户名:")
    passwd = input("输入密码:")
    print("==================================")
    msg = "R %s %s" % (name, passwd)
    s.send(msg.encode())
    # 等待结果并处理
    data = s.recv(1024).decode()
    if data == "OK":
        print("注册成功!")
        # 进入二级界面
        second_view(s)
    else:
        print("用户名重复!")


# 发送登录请求
def do_login(s):
    print("==============登录================")
    name = input("输入用户名:")
    passwd = input("输入密码:")
    print("==================================")
    msg = "L %s %s" % (name, passwd)
    s.send(msg.encode())
    # 等待结果并处理
    data = s.recv(1024).decode()
    if data == "OK":
        print("登录成功!")
        # 进入二级界面
        second_view(s)
    else:
        print("登录失败!")


# 主程序
def main():
    # 连接服务端
    s = socket()
    s.connect(ADDR)
    # 一级界面循环
    while True:
        print("==============Welcome================")
        print("R:注册       L:登录           E:退出")
        print("=====================================")
        cmd1 = input(">>>")
        if cmd1 == "R":
            do_register(s)
        elif cmd1 == "L":
            do_login(s)
        elif cmd1 == "E":
            s.send(b"E")
            sys.exit("程序退出!")
        else:
            print("输入错误!")


if __name__ == '__main__':
    main()

服务端

"""
    服务端
"""
# 导入相关模块
from socket import *
from multiprocessing import Process
import signal
from dict_db import Database

# 绑定服务端地址
HOST = "127.0.0.1"
PORT = 8888
ADDR = (HOST, PORT)
# 创建操作数据库对象
db = Database()


# 处理注册请求
def deal_register(c, name, passwd):
    if db.register(name, passwd):
        c.send(b"OK")
    else:
        c.send(b"Not OK")


# 处理登录请求
def deal_login(c, name, passwd):
    if db.login(name, passwd):
        c.send(b"OK")
    else:
        c.send(b"Not OK")


# 处理查单词请求
def deal_query(c, word):
    res = db.query(word)
    c.send(res.encode())


# 处理查历史请求
def deal_history(c):
    res_tuple = db.history()
    if not res_tuple:
        c.send("无查询历史!".encode())
    else:
        res = ""
        for i in res_tuple:
            res += "%s,%s,%s\n" % (i[0], i[1], i[2])
        c.send(res.encode())


# 处理清空历史请求
def deal_clear_history(c):
    if db.clear_history():
        c.send(b"OK")
    else:
        c.send(b"Not OK")


# 子进程主程序
def handle(c):
    # 创建游标
    db.create_cursor()
    while True:
        data = c.recv(1024).decode()
        if not data or data == "E":
            return
        cmd = data.split(" ", 2)
        if cmd[0] == "R":
            deal_register(c, cmd[1], cmd[2])
        elif cmd[0] == "L":
            deal_login(c, cmd[1], cmd[2])
        elif cmd[0] == "Q":
            deal_query(c, cmd[1])
        elif cmd[0] == "H":
            deal_history(c)
        elif cmd[0] == "C":
            deal_clear_history(c)


# 主程序
def main():
    # 创建监听套接字
    s = socket()
    s.bind(ADDR)
    s.listen(5)
    # 处理僵尸进程
    signal.signal(signal.SIGCHLD, signal.SIG_IGN)
    # 循环创建多个子进程
    while True:
        c, addr = s.accept()
        p = Process(target=handle, args=(c,))
        p.start()


if __name__ == '__main__':
    main()

操作数据库模块

"""
    数据库处理
"""
# 导入pymysql模块
import pymysql


# 创建Database类
class Database:
    def __init__(self):
        self.host = 'localhost'
        self.port = 3306
        self.user = "root"
        self.password = "123456"
        self.database = "dict"
        self.charset = "utf8"
        # 初始化创建db对象
        self.__connect_database()
        # 初始化name属性,查询历史,清空历史使用
        self.name = None

    def __connect_database(self):
        """
        创建db属性
        """
        self.db = pymysql.connect(host=self.host,
                                  port=self.port,
                                  user=self.user,
                                  password=self.password,
                                  database=self.database,
                                  charset=self.charset)

    def create_cursor(self):
        """
        创建游标属性
        """
        self.cur = self.db.cursor()

    def register(self, name, passwd):
        """
        注册方法,如果name存在,返回False,不存在则插入user表,返回True,过程出错返回False
        """
        sql = "select name from user where name=%s;"
        self.cur.execute(sql, [name])
        r = self.cur.fetchone()
        if r:
            return False
        else:
            try:
                sql = "insert into user (name,passwd) VALUES (%s,%s);"
                self.cur.execute(sql, [name, passwd])
                self.db.commit()
                self.name = name
                return True
            except:
                self.db.rollback()
                return False

    def login(self, name, passwd):
        """
        登录方法,用户不存在,返回False,存在但密码不正确,返回False,正确返回True
        """
        sql = "select passwd from user where name=%s;"
        self.cur.execute(sql, [name])
        r = self.cur.fetchone()
        if r:
            if r[0] == passwd:
                self.name = name
                return True
            else:
                return False
        else:
            return False

    def query(self, word):
        """
        查询单词,查不到单词也写入hist且返回错误,查到单词返回解释,过程错误返回错误
        """
        sql = "select mean from words where word=%s;"
        self.cur.execute(sql, [word])
        r = self.cur.fetchone()
        if not r:
            try:
                sql = "insert into hist (name,word) VALUES (%s,%s);"
                self.cur.execute(sql, [self.name, word])
                self.db.commit()
            except:
                self.db.rollback()
            return "查询错误"
        else:
            try:
                sql = "insert into hist (name,word) VALUES (%s,%s);"
                self.cur.execute(sql, [self.name, word])
                self.db.commit()
                return r[0]
            except:
                self.db.rollback()
                return "查询错误"

    def history(self):
        """
        查询历史,查不到返回False,查到返回结果二维元组
        """
        sql = "select name,word,time from hist where name =%s ORDER BY time DESC limit 10;"
        self.cur.execute(sql, [self.name])
        res = self.cur.fetchall()
        if not res:
            return False
        else:
            return res

    def clear_history(self):
        """
        清空历史,清空返回True,失败返回False
        :return:
        """
        try:
            sql = "delete from hist where name=%s;"
            self.cur.execute(sql, [self.name])
            self.db.commit()
            return True
        except:
            self.db.rollback()
            return False

阶段代码 on 29/2/2020

你可能感兴趣的:(tiny_project)