python实现简单聊天应用 python群聊和点对点均实现
发布时间 - 2026-01-11 03:14:53 点击率:次后续代码更新和功能添加会提交到个人github主页,有兴趣可以一起来完善!

如果只是拿过去运行看结果,请注意平台相关性以及python版本号,本示例开发运行平台为win7x86_64 pycharm community,python版本号为3.5!!!
TALK IS CHEAP, SHOW YOU MY CODE:
客户端
#coding:utf-8
'''
file:client.py.py
date:2017/9/11 11:01
author:lockey
email:lockey@123.com
platform:win7.x86_64 pycharm python3
desc:p2p communication clientside
'''
from socket import *
import threading,sys,json,re
#引入json模块主要是为了数据的封装传输,re的话是做一些合法性的验证
HOST = '192.168.1.7'
PORT=8022
BUFSIZE = 1024 ##缓冲区大小 1K
ADDR = (HOST,PORT)
myre = r"^[_a-zA-Z]\w{0,}"
tcpCliSock = socket(AF_INET,SOCK_STREAM)
#创建一个socket连接
userAccount = None
#用户登录标志,也用来记录登录的用户名称
def register():
#用户注册函数
print("""
Glad to have you a member of us!
""")
accout = input('Please input your account: ')
if not re.findall(myre, accout):
print('Account illegal!')
return None
password1 = input('Please input your password: ')
password2 = input('Please confirm your password: ')
if not (password1 and password1 == password2):
print('Password not illegal!')
return None
global userAccount
userAccount = accout
regInfo = [accout,password1,'register']
datastr = json.dumps(regInfo)
tcpCliSock.send(datastr.encode('utf-8'))
data = tcpCliSock.recv(BUFSIZE)
data = data.decode('utf-8')
if data == '0':
print('Success to register!')
return True
elif data == '1':
print('Failed to register, account existed!')
return False
else:
print('Failed for exceptions!')
return False
def login():
#用户登录函数
print("""
Welcome to login in!
""")
accout = input('Account: ')
if not re.findall(myre, accout):
print('Account illegal!')
return None
password = input('Password: ')
if not password:
print('Password illegal!')
return None
global userAccount
userAccount = accout
loginInfo = [accout, password,'login']
datastr = json.dumps(loginInfo)
tcpCliSock.send(datastr.encode('utf-8'))
data = tcpCliSock.recv(BUFSIZE)
if data == '0':
print('Success to login!')
return True
else:
print('Failed to login in(user not exist or username not match the password)!')
return False
def addGroup():
#群组添加
groupname = input('Please input group name: ')
if not re.findall(myre, groupname):
print('group name illegal!')
return None
return groupname
def chat(target):
#进入聊天(群聊和点对点聊天可以选择)
while True:
print('{} -> {}: '.format(userAccount,target))
msg = input()
if len(msg) > 0 and not msg in 'qQ':
if 'group' in target:
optype = 'cg'
else:
optype = 'cp'
dataObj = {'type': optype, 'to': target, 'msg': msg, 'froms': userAccount}
datastr = json.dumps(dataObj)
tcpCliSock.send(datastr.encode('utf-8'))
continue
elif msg in 'qQ':
break
else:
print('Send data illegal!')
class inputdata(threading.Thread):
#用户输入选择然后执行不同的功能程序
def run(self):
menu = """
(CP): Chat with individual
(CG): Chat with group member
(AG): Add a group
(EG): Enter a group
(H): For help menu
(Q): Quit the system
"""
print(menu)
while True:
operation = input('Please input your operation("h" for help): ')
if operation in 'cPCPCpcp':
#进入个人聊天
target = input('Who would you like to chat with: ')
chat(target)
continue
if operation in 'cgCGCgcG':
#进入群聊
target = input('Which group would you like to chat with: ')
chat('group'+target)
continue
if operation in 'agAGAgaG':
#添加群组
groupName = addGroup()
if groupName:
dataObj = {'type': 'ag', 'groupName': groupName}
dataObj = json.dumps(dataObj)
tcpCliSock.send(dataObj.encode('utf-8'))
continue
if operation in 'egEGEgeG':
#入群
groupname = input('Please input group name fro entering: ')
if not re.findall(myre, groupname):
print('group name illegal!')
return None
dataObj = {'type': 'eg', 'groupName': 'group'+groupname}
dataObj = json.dumps(dataObj)
tcpCliSock.send(dataObj.encode('utf-8'))
continue
if operation in 'hH':
print(menu)
continue
if operation in 'qQ':
sys.exit(1)
else:
print('No such operation!')
class getdata(threading.Thread):
#接收数据线程
def run(self):
while True:
data = tcpCliSock.recv(BUFSIZE).decode('utf-8')
if data == '-1':
print('can not connect to target!')
continue
if data == 'ag0':
print('Group added!')
continue
if data == 'eg0':
print('Entered group!')
continue
if data == 'eg1':
print('Failed to enter group!')
continue
dataObj = json.loads(data)
if dataObj['type'] == 'cg':
#群组消息的格式定义
print('{}(from {})-> : {}'.format(dataObj['froms'], dataObj['to'], dataObj['msg']))
else:
#个人消息的格式定义
print('{} ->{} : {}'.format(dataObj['froms'], userAccount, dataObj['msg']))
def main():
try:
tcpCliSock.connect(ADDR)
print('Connected with server')
while True:
loginorReg = input('(l)ogin or (r)egister a new account: ')
if loginorReg in 'lL':
log = login()
if log:
break
if loginorReg in 'rR':
reg = register()
if reg:
break
myinputd = inputdata()
mygetdata = getdata()
myinputd.start()
mygetdata.start()
myinputd.join()
mygetdata.join()
except Exception:
print('error')
tcpCliSock.close()
sys.exit()
if __name__ == '__main__':
main()
服务端
#coding:utf-8
'''
file:server.py
date:2017/9/11 14:43
author:lockey
email:lockey@123.com
platform:win7.x86_64 pycharm python3
desc:p2p communication serverside
'''
import socketserver,json,time
import subprocess
connLst = []
groupLst = []
## 代号 地址和端口 连接对象
#optype = {'ag':'group adding','cp':'chat with individual','cg':'chat with group'}
class Connector(object): ##连接对象类
def __init__(self,account,password,addrPort,conObj):
self.account = account
self.password = password
self.addrPort = addrPort
self.conObj = conObj
class Group(object):#群组类
def __init__(self,groupname,groupOwner):
self.groupId = 'group'+str(len(groupLst)+1)
self.groupName = 'group'+groupname
self.groupOwner = groupOwner
self.createTime = time.time()
self.members=[groupOwner]
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
print("got connection from",self.client_address)
userIn = False
global connLst
global groupLst
while not userIn:
conn = self.request
data = conn.recv(1024)
if not data:
continue
dataobj = json.loads(data.decode('utf-8'))
#如果连接客户端发送过来的信息格式是一个列表且注册标识为False时进行用户注册或者登陆
ret = '0'
if type(dataobj) == list and not userIn:
account = dataobj[0]
password = dataobj[1]
optype = dataobj[2]
existuser = False
if len(connLst) > 0:
for obj in connLst:
if obj.account == account:
existuser = True
if obj.password == password:
userIn = True
print('{} has logged in system({})'.format(account,self.client_address))
break
if optype == 'login' and (not userIn or not existuser):
ret = '1'
print('{} failed to logged in system({})'.format(account, self.client_address))
else:
if existuser:
ret = '1'
print('{} failed to register({}),account existed!'.format(account, self.client_address))
else:
try:
conObj = Connector(account,password,self.client_address,self.request)
connLst.append(conObj)
print('{} has registered to system({})'.format(account,self.client_address))
userIn = True
except:
print('%s failed to register for exception!'%account)
ret = '99'
conn.sendall(ret.encode('utf-8'))
if ret == '0':
break
while True:
#除登陆注册之外的请求的监听
conn = self.request
data = conn.recv(1024)
if not data:
continue
print(data)
dataobj = data.decode('utf-8')
dataobj = json.loads(dataobj)
if dataobj['type'] == 'ag' and userIn:
#如果判断用户操作请求类型为添加群组则进行以下操作
groupName = dataobj['groupName']
groupObj = Group(groupName,self.request)
groupLst.append(groupObj)
conn.sendall('ag0'.encode('utf-8'))
print('%s added'%groupName)
continue
if dataobj['type'] == 'eg' and userIn:
#入群操作
groupName = dataobj['groupName']
ret = 'eg1'
for group in groupLst:
if groupName == group.groupName:
group.members.append(self.request)
print('{} added into {}'.format(self.client_address,groupName))
ret = 'eg0'
break
conn.sendall(ret.encode('utf-8'))
continue
#客户端将数据发给服务器端然后由服务器转发给目标客户端
print('connLst',connLst)
print('grouplst',groupLst)
if len(connLst) > 1:
sendok = False
if dataobj['type'] == 'cg':
#群内广播(除发消息的人)
print('group',data)
for obj in groupLst:
if obj.groupName == dataobj['to']:
for user in obj.members:
if user != self.request:
user.sendall(data)
else:
#个人信息发送
for obj in connLst:
if dataobj['to'] == obj.account:
obj.conObj.sendall(data)
sendok = True
if sendok == False:
print('no target valid!')
else:
conn.sendall('-1'.encode('utf-8'))
continue
if __name__ == '__main__':
server = socketserver.ThreadingTCPServer(('192.168.1.7',8022),MyServer)
print('waiting for connection...')
server.serve_forever()
运行结果示例
服务端(记录着各客户端的操作):
客户端1:
有注册、建群、群聊、点对点聊天
客户端2:
客户端3:
要拷贝代码运行的话请注意平台(win7.x86_64)和python版本号(python3.5)!!!
# python
# 聊天应用
# 点对点
# python实现简单点对点(p2p)聊天
# 客户端
# 群组
# 请注意
# 新和
# 用户登录
# 用户注册
# 服务端
# 的人
# 是一个
# 运行平台
# 有兴趣
# 可以选择
# 个人信息
# 创建一个
# 转发给
# 发消息
# 是为了
# return
# illegal
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel请求验证怎么写_Laravel Validator自定义表单验证规则教程
javascript事件捕获机制【深入分析IE和DOM中的事件模型】
JS中页面与页面之间超链接跳转中文乱码问题的解决办法
如何快速登录WAP自助建站平台?
Laravel如何集成Inertia.js与Vue/React?(安装配置)
Laravel安装步骤详细教程_Laravel环境搭建指南
如何用y主机助手快速搭建网站?
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
Laravel路由Route怎么设置_Laravel基础路由定义与参数传递规则【详解】
html5如何设置样式_HTML5样式设置方法与CSS应用技巧【教程】
Laravel如何使用Blade模板引擎?(完整语法和示例)
如何快速生成高效建站系统源代码?
Laravel如何实现模型的全局作用域?(Global Scope示例)
Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理
详解jQuery中基本的动画方法
Android okhttputils现在进度显示实例代码
如何在万网自助建站中设置域名及备案?
如何快速建站并高效导出源代码?
Laravel怎么配置不同环境的数据库_Laravel本地测试与生产环境动态切换【方法】
Laravel如何处理和验证JSON类型的数据库字段
网站建设整体流程解析,建站其实很容易!
七夕网站制作视频,七夕大促活动怎么报名?
laravel服务容器和依赖注入怎么理解_laravel服务容器与依赖注入解析
如何获取免费开源的自助建站系统源码?
Laravel Blade模板引擎语法_Laravel Blade布局继承用法
潮流网站制作头像软件下载,适合母子的网名有哪些?
音乐网站服务器如何优化API响应速度?
Laravel如何自定义分页视图?(Pagination示例)
Laravel如何与Docker(Sail)协同开发?(环境搭建教程)
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
轻松掌握MySQL函数中的last_insert_id()
Laravel怎么配置.env环境变量_Laravel生产环境敏感数据保护与读取【方法】
公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?
laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法
香港服务器选型指南:免备案配置与高效建站方案解析
Bootstrap整体框架之JavaScript插件架构
网站制作免费,什么网站能看正片电影?
在centOS 7安装mysql 5.7的详细教程
Laravel怎么使用Collection集合方法_Laravel数组操作高级函数pluck与map【手册】
Laravel怎么实现模型属性的自动加密
Python函数文档自动校验_规范解析【教程】
消息称 OpenAI 正研发的神秘硬件设备或为智能笔,富士康代工
动图在线制作网站有哪些,滑动动图图集怎么做?
php485函数参数是什么意思_php485各参数详细说明【介绍】
车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?
Android 常见的图片加载框架详细介绍
Laravel如何使用模型观察者?(Observer代码示例)
Laravel如何创建自定义Artisan命令?(代码示例)
如何挑选高效建站主机与优质域名?
Python文件操作最佳实践_稳定性说明【指导】
上一篇:nginx共享内存机制实例分析
上一篇:nginx共享内存机制实例分析

