需求:
1、用Socket开发双工聊天软件,服务端与客户端交互
2、聊天记录要记录到MySQL数据库
3、客户端可以查看聊天记录
4、用三层架构开发
程序架构
各文件作用解析
- model model层
- dialogue.py dialoue表
- users.py users表
- utility 公共层
- sql_helper.py 公共配置文件
- client.py 客户端文件
- conf.py 配置文件
- index.py 主程序文件
- server.py 服务器端文件
公共配置之sql_helper文件
#cat sql_helper.py #!/usr/bin/env python #coding:utf-8 import MySQLdb import conf class MySQLHelper(object): def __init__(self): self.__conn_dict = conf.conf_dict self.__t1_dict = conf.t1_dict #插入数据 def Get_Dict(self,sql,params): conn = MySQLdb.connect(**self.__t1_dict) cur = conn.cursor() reCount = cur.execute(sql,params) conn.commit() cur.close() conn.close() #return reCount #查询用户名和密码 def Get_One(self,sql,params): conn = MySQLdb.connect(**self.__conn_dict) cur = conn.cursor() reCounts = cur.execute(sql,params) data = cur.fetchone() cur.close() conn.close() return data #查询聊天记录 def Get_all(self,sql,params): conn = MySQLdb.connect(**self.__t1_dict) cur = conn.cursor() reCounts = cur.execute(sql,params) data = cur.fetchall() cur.close() conn.close() return data
Model层之users表
#cat users.py #!/usr/bin/env python #coding:utf-8 from utility.sql_helper import MySQLHelper class Users(object): def __init__(self): self.__helper = MySQLHelper() #查询命令 def CheckValidate(self,username,password): sql = "select * from user where user = %s and password = %s" params = (username,password) return self.__helper.Get_One(sql,params)
Model层之dialogue表
#cat dialogue.py #!/usr/bin/env python #coding:utf-8 from utility.sql_helper import MySQLHelper #把类导入进来 class Inserts(object): def __init__(self): self.__helper = MySQLHelper() #插入数据SQL语句 def CheckInsert(self,name,time,dialogue): sql = "insert into t1(name,time,dialogue) values(%s,%s,%s)" params = (name,time,dialogue) return self.__helper.Get_Dict(sql,params) #查询聊天记录SQL语句 def ShowDialogue(self,name,names): sql = "select * from t1 where name = %s or name = %s" params = (name,names) return self.__helper.Get_all(sql,params)
配置文件
#cat conf.py #!/usr/bin/env python #coding:utf-8 conf_dict = dict(host=‘192.168.1.109‘,user=‘root‘,passwd=‘123456‘,db=‘mysql‘) t1_dict = dict(host=‘192.168.1.109‘,user=‘root‘,passwd=‘123456‘,db=‘wsyht‘)
服务端文件
# cat server.py #!/usr/bin/env python #coding:utf-8 def StartSocket(): from model.dialogue import Inserts import socket inserts = Inserts() sk = socket.socket() ip_port = (‘127.0.0.1‘,9915) sk.bind(ip_port) sk.listen(5) #阻塞数量 while True: conn,address = sk.accept() conn.send(‘hello‘) print ‘新用户进入系统‘ print ‘server:hello‘ flag = True while flag: import time data = conn.recv(1024) if data == ‘exit‘: flag = False print ‘对方已退出系统‘ break print ‘client:‘,data inp = raw_input(‘server:‘) conn.send(inp) name = ‘server‘ time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) result = inserts.CheckInsert(name,time,inp) conn.close()
客户端文件
#cat clieny.py #!/usr/bin/env python #coding:utf-8 import socket client = socket.socket() ip_port = (‘127.0.0.1‘,9915) client.connect(ip_port) print "输入1可以查看聊天记录" while True: from model.dialogue import Inserts import time inserts = Inserts() data = client.recv(1024) print ‘server:‘,data inp = raw_input(‘client:‘) client.send(inp) if inp == ‘1‘: print ‘dn al‘ from model.dialogue import Inserts inserts = Inserts() result = inserts.ShowDialogue(‘server‘,‘client‘) print result import time name = ‘client‘ time = time.strftime(‘%Y-%m-%d %H:%M:%S‘) result = inserts.CheckInsert(name,time,inp) if inp == ‘exit‘: break
主程序文件
#!/usr/bin/env python #coding:utf-8 from model.users import Users import server def main(): user = raw_input(‘username:‘) pwd = raw_input(‘password:‘) users = Users() result = users.CheckValidate(user,pwd) if not result: print ‘你输入的用户名或密码有误‘ else: print ‘欢迎登陆后台管理页面‘ server.StartSocket() if __name__ == ‘__main__‘: main()
执行测试:
1)服务端执行
2)客户端执行
3)服务端执行
4)客户端执行
5)服务端执行
6)客户端查看聊天记录
7)查看数据库表
时间: 2024-12-28 12:42:35