python实现多人聊天室
程序员文章站
2022-05-06 12:47:42
本文实例为大家分享了python实现多人聊天室的具体代码,供大家参考,具体内容如下
一、目的
以实现小项目的方式,来巩固之前学过的python基本语法以及相关的知识。&...
本文实例为大家分享了python实现多人聊天室的具体代码,供大家参考,具体内容如下
一、目的
以实现小项目的方式,来巩固之前学过的python基本语法以及相关的知识。
二、相关技术
1.wxpython gui编程
2.网络编程
3.多线程编程
4.数据库编程
5.简单的将数据导出到excel表
三、存在的漏洞以及不足
1.由于数据库编码的问题,无法使用中文。
2.在客户端关闭后,其相关的线程仍然存在于服务器的用户线程队列中,所以服务器会错误地往已关闭的客户端传送信息。
3.客户端初始登录并加载历史记录时,会出现每条历史消息后面的回车键丢失的现象,解决的方法是:在加载相邻两条消息之间加个时间间隔,但效果不佳。
四、源码
服务器server:
# -*- coding: utf-8 -*- from socket import * import time import threading import wx import mysqldb import xlwt from clientthread import clientthread class server(wx.frame): def __init__(self,parent=none,id=-1,title='服务器',pos=wx.defaultposition,size=(500,300)): '''窗口''' wx.frame.__init__(self,parent,id,title,pos,size=(400,470)) pl = wx.panel(self) con = wx.boxsizer(wx.vertical) subcon = wx.flexgridsizer(wx.horizontal) sta = wx.button(pl , size=(133, 40),label='启动服务器') end = wx.button(pl, size=(133, 40), label='关闭服务器') hist = wx.button(pl,size=(133,40),label='导出聊天记录') subcon.add(sta, 1, wx.bottom) subcon.add(hist, 1, wx.bottom) subcon.add(end, 1, wx.bottom) con.add(subcon,1,wx.align_centre|wx.bottom) self.text = wx.textctrl(pl, size=(400,250),style = wx.te_multiline|wx.te_readonly) con.add(self.text, 1, wx.align_centre) self.ttex = wx.textctrl(pl, size=(400,100),style=wx.te_multiline) con.add(self.ttex, 1, wx.align_centre) sub2 = wx.flexgridsizer(wx.horizontal) clear = wx.button(pl, size=(200, 40), label='清空') send = wx.button(pl, size=(200, 40), label='发送') sub2.add(clear, 1, wx.top | wx.left) sub2.add(send, 1, wx.top | wx.right) con.add(sub2, 1, wx.align_centre) pl.setsizer(con) '''窗口''' '''绑定''' self.bind(wx.evt_button, self.editclear, clear) self.bind(wx.evt_button, self.sendmessage, send) self.bind(wx.evt_button, self.start, sta) self.bind(wx.evt_button, self.break, end) self.bind(wx.evt_button, self.writetoexcel, hist) '''绑定''' '''服务器准备工作''' self.userthreadlist = [] self.onserve = false addr = ('', 21567) self.servesock = socket(af_inet, sock_stream) self.servesock.bind(addr) self.servesock.listen(10) '''服务器准备工作''' '''数据库准备工作,用于存储聊天记录''' self.db = mysqldb.connect('localhost', 'root', '123456', 'user_info') self.cursor = self.db.cursor() self.cursor.execute("select * from history order by time") self.text.setvalue('') for data in self.cursor.fetchall(): #加载历史聊天记录 self.text.appendtext('%s said:\n%s\nwhen %s\n\n' % (data[0], data[2], data[1])) '''数据库准备工作,用于存储聊天记录''' #将聊天记录导出到excel表中 def writetoexcel(self,event): wbk = xlwt.workbook() sheet = wbk.add_sheet('sheet 1') self.cursor.execute("select * from history order by time") sheet.write(0, 0, "user") sheet.write(0, 1, "datetime") sheet.write(0, 5, "message") index = 0 for data in self.cursor.fetchall(): index = index + 1 time = '%s'%data[1] #将datetime转成字符形式,否则直接写入excel会变成时间戳 sheet.write(index,0,data[0]) sheet.write(index,1,time) #写进excel会变成时间戳 sheet.write(index,5,data[2]) wbk.save(r'd:\history_dialog.xls') #启动服务器的服务线程 def start(self,event): if not self.onserve: '''启动服务线程''' self.onserve = true mainthread = threading.thread(target=self.on_serving, args=()) mainthread.setdaemon(true) # 解决父线程结束,子线程还继续运行的问题 mainthread.start() '''启动服务线程''' #关闭服务器 def break(self,event): self.onserve = false #服务器主循环 def on_serving(self): print '...on serving...' while self.onserve: usersocket, useraddr = self.servesock.accept() username = usersocket.recv(1024).decode(encoding='utf-8') #接收用户名 userthread = clientthread(usersocket, username,self) self.userthreadlist.append(userthread) #将用户线程加到队列中 userthread.start() self.servesock.close() #绑定发送按钮 def sendmessage(self,event): if self.onserve and cmp(self.ttex.getvalue(),''): data = self.ttex.getvalue() self.addtext('server',data,time.strftime("%y-%m-%d %h:%m:%s", time.localtime())) self.ttex.setvalue('') # 向所有客户端(包括自己)发送信息,同时更新到数据库 def addtext(self, source, data,time): self.cursor.execute("insert into history values(\"%s\",\"%s\",\"%s\")" % (source,time,data)) #双引号里面有双引号,bug:句子不能有双引号、以及中文 self.db.commit() senddata = '%s said:\n%s\nwhen %s\n' % (source,data,time) self.text.appendtext('%s\n'%senddata) for user in self.userthreadlist: #bug:客户端关闭了仍然在队列中。如果客户端关闭了,那怎么在服务器判断是否已经关闭了?客户端在关闭之前发一条信息给服务器? user.usersocket.send(senddata.encode(encoding='utf-8')) #绑定清空按钮 def editclear(self,event): self.ttex.clear() def main(): app = wx.app(false) server().show() app.mainloop() if __name__ == '__main__': main()
服务器的客户线程clientthread:
# -*- coding: utf-8 -*- import threading import time class clientthread(threading.thread): def __init__(self,usersocket, username,server): threading.thread.__init__(self) self.usersocket = usersocket self.username = username self.server = server self.loadhist() # 加载历史聊天记录 def loadhist(self): self.server.cursor.execute("select * from history order by time") for data in self.server.cursor.fetchall(): time.sleep(0.6) #几条信息同时发,会造成末尾回车键的丢失,所以要有时间间隔 senddata = '%s said:\n%s\nwhen %s\n'%(data[0], data[2], data[1]) self.usersocket.send(senddata.encode(encoding='utf-8')) #方法重写,线程的入口 def run(self): size = 1024 while true: data = self.usersocket.recv(size) #未解决:客户端断开连接后这里会报错 self.server.addtext(self.username,data.decode(encoding='utf-8'),time.strftime("%y-%m-%d %h:%m:%s", time.localtime())) self.usersocket.close() #这里都执行不到
客户登录界面logframe:
# -*- coding: utf-8 -*- from socket import * import wx import mysqldb from client import client class logframe(wx.frame): def __init__(self,parent=none,id=-1,title='登录窗口',pos=wx.defaultposition,size=(500,300)): '''窗口''' wx.frame.__init__(self,parent,id,title,pos,size=(400,280)) self.pl = wx.panel(self) con = wx.boxsizer(wx.vertical) subcon = wx.flexgridsizer(2,2,10,10) username = wx.statictext(self.pl, label="username:",style=wx.align_left) password = wx.statictext(self.pl, label="password:",style=wx.align_left) self.tc1 = wx.textctrl(self.pl,size=(180,20)) self.tc2 = wx.textctrl(self.pl,size=(180,20),style=wx.te_password) subcon.add(username,wx.te_left) subcon.add(self.tc1,1,wx.expand) subcon.add(password) subcon.add(self.tc2,1,wx.expand) con.add(subcon,1,wx.align_center) subcon2 = wx.flexgridsizer(1,2,10,10) register = wx.button(self.pl,label='register') login = wx.button(self.pl,label='login') subcon2.add(register,1, wx.top) subcon2.add(login,1, wx.top) con.add(subcon2,1,wx.align_centre) self.pl.setsizer(con) self.bind(wx.evt_button,self.register,register) self.bind(wx.evt_button,self.login,login) '''窗口''' self.isconnected = false self.usersocket = none #连接到服务器 def connecttoserver(self): if not self.isconnected: addr = ('localhost', 21567) self.usersocket = socket(af_inet, sock_stream) try: self.usersocket.connect(addr) self.usersocket.send(self.tc1.getvalue().encode(encoding='utf-8')) self.isconnected = true return true except exception: return false else: return true #登录 def login(self,event): if not self.connecttoserver(): err = wx.messagedialog(none, '服务器未启动', 'error!', wx.ok) err.showmodal() err.destroy() else: username = self.tc1.getvalue() password = self.tc2.getvalue() db = mysqldb.connect('localhost', 'root', '123456', 'user_info') cursor = db.cursor() cursor.execute("select * from user_list where username='%s' and password='%s'"%(username,password)) if not cursor.fetchone(): err = wx.messagedialog(none,'用户不存在或密码错误','error!',wx.ok) err.showmodal() else: self.close() client(opsock=self.usersocket, username=username).show() db.commit() db.close() #注册 def register(self,event): if not self.connecttoserver(): err = wx.messagedialog(none, '服务器未启动', 'error!', wx.ok) err.showmodal() err.destroy() else: username = self.tc1.getvalue() password = self.tc2.getvalue() db = mysqldb.connect('localhost', 'root', '123456', 'user_info') cursor = db.cursor() cursor.execute("select * from user_list where username='%s'"%username) if not cursor.fetchone(): cursor.execute("insert into user_list(username,password) values('%s','%s')"%(username,password)) else: err = wx.messagedialog(none, '用户已存在', 'error!', wx.ok) err.showmodal() db.commit() db.close() def main(): app = wx.app(false) logframe().show() app.mainloop() if __name__ == '__main__': main()
客户端client:
#/usr/bin/env python # -*- coding: utf-8 -*- import wx import threading from time import ctime class client(wx.frame): def __init__(self,opsock,username,parent=none,id=-1,title='客户端',pos=wx.defaultposition,size=(500,300)): '''窗口''' wx.frame.__init__(self,parent,id,title,pos,size=(400,470)) self.opsock = opsock self.username = username pl = wx.panel(self) con = wx.boxsizer(wx.vertical) subcon = wx.flexgridsizer(wx.horizontal) sta = wx.button(pl, size=(200, 40),label='连接') end = wx.button(pl, size=(200, 40),label='断开') subcon.add(sta, 1, wx.top|wx.left) subcon.add(end, 1, wx.top|wx.right) con.add(subcon,1,wx.align_centre) self.text = wx.textctrl(pl, size=(400,250),style = wx.te_multiline|wx.te_readonly) con.add(self.text, 1, wx.align_centre) self.ttex = wx.textctrl(pl, size=(400,100),style=wx.te_multiline) con.add(self.ttex, 1, wx.align_centre) sub2 = wx.flexgridsizer(wx.horizontal) clear = wx.button(pl, size=(200, 40), label='清空') send = wx.button(pl, size=(200, 40), label='发送') sub2.add(clear, 1, wx.top | wx.left) sub2.add(send, 1, wx.top | wx.right) con.add(sub2, 1, wx.align_centre) pl.setsizer(con) '''窗口''' '''绑定''' self.bind(wx.evt_button, self.editclear, clear) self.bind(wx.evt_button, self.send, send) self.bind(wx.evt_button, self.login, sta) self.bind(wx.evt_button, self.logout, end) '''绑定''' self.isconnected = false #登录 def login(self,event): '''客户端准备工作''' self.isconnected = true t = threading.thread(target=self.receive, args=()) t.setdaemon(true) t.start() '''客户端准备工作''' #退出 def logout(self,event): self.isconnected = false #绑定发送按钮 def send(self,event): if self.isconnected and cmp(self.ttex.getvalue(),''): self.opsock.send(self.ttex.getvalue().encode(encoding='utf-8')) self.ttex.setvalue('') #绑定清空按钮 def editclear(self,event): self.ttex.clear() #接收客户端的信息(独立一个线程) def receive(self): while self.isconnected: data = self.opsock.recv(1024).decode(encoding='utf-8') self.text.appendtext('%s\n'%data)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 学生够幽默老师无语了
下一篇: 忽然感觉说的挺对