Python基于Socket实现简单聊天室
作者:Coder_py 时间:2022-06-13 11:56:51
本文实例为大家分享了Python基于Socket实现简单聊天室,供大家参考,具体内容如下
服务端
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/7/27 15:34
# @File : Server.py
"""
聊天室服务端
"""
import socket,select
host=''
port=7799
addr=(host,port)
inputs=[]
members={}
#配置服务端
def start_Server():
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(addr)
server.listen(5)
return server
#返回聊天室成员列表
def show_members(w):
name_list=[]
for i in w:
name_list.append(w[i])
return name_list
#处理新的成员
def new_member(server):
client,addr=server.accept()
print('欢迎进入聊天室 ,%s,%s' % (client,addr))
try:
client.send('请输入名字')
name=client.recv(1024)
inputs.append(client)
members[client]=name
names='其它用户%s'% (show_members(members))
client.send(names)
except Exception,e:
print(e)
#正式启动服务端
def server_run():
server=start_Server()
print 'start'
inputs.append(server)
while True:
rs,ws,es=select.select(inputs,[],[])
for temp in rs:
if temp is server:
new_member(temp)
else:
disConnect=False
try:
data=temp.recv(1024)
data=members[temp]+'说'+data
except socket.error:
data=members[temp]+'离开'
disConnect=True
if disConnect:
inputs.remove(temp)
print data
for other in inputs:
if other!=server and other!=temp:
try:
other.send(data)
except Exception,e:
print(e)
del members[temp]
else:
print data
for other in inputs:
if other!=server and other!=temp:
try:
other.send(data)
except Exception,e:
print(e)
if __name__ == '__main__':
server_run()
客户端
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/7/27 15:34
# @File : Client.py
"""
聊天室客户端
"""
import socket
import select
import threading
host='127.0.0.1'
port=7799
addr=(host,port)
#配置连接
def connect():
client=socket.socket()
client.connect(addr)
return client
#显示用户列表
def show_member(s):
my=[s]
while True:
rs,ws,es=select.select(my,[],[])
if s in rs:
try:
print s.recv(1024)
except socket.error:
print 'socket is error'
exit()
#进行对话
def talk(s):
while True:
try:
info=raw_input('>>>')
except Exception:
print 'can\'t input'
exit()
try:
s.send(info)
except Exception,e:
print e
exit()
#主函数
def main():
client=connect()
t=threading.Thread(target=show_member,args=(client,))
t.start()
t1=threading.Thread(target=talk,args=(client,))
t1.start()
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/Coder_py/article/details/76218423
标签:Python,Socket,聊天室
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django如何实现网站注册用户邮箱验证功能
2023-05-14 08:03:08
![](https://img.aspxhome.com/file/2023/9/94049_0s.png)
python-pymysql获取字段名称-获取内容方式
2024-01-21 05:19:44
golang中cache组件的使用及groupcache源码解析
2024-02-07 11:12:25
PyQt5实现将Matplotlib图像嵌入到Scoll Area中显示滚动条效果
2021-12-21 05:40:39
![](https://img.aspxhome.com/file/2023/8/112118_0s.gif)
PyCharm配置mongo插件的方法
2023-08-16 19:37:05
![](https://img.aspxhome.com/file/2023/6/85026_0s.jpg)
CSS应用的必要步骤:样式重设
2008-06-11 13:29:00
python GUI库图形界面开发之PyQt5滚动条控件QScrollBar详细使用方法与实例
2021-04-26 09:11:26
![](https://img.aspxhome.com/file/2023/0/92500_0s.png)
python反扒机制的5种解决方法
2023-10-14 12:39:10
![](https://img.aspxhome.com/file/2023/4/63514_0s.jpg)
更新升级python和pip版本后不生效的问题解决
2022-09-30 18:23:14
![](https://img.aspxhome.com/file/2023/5/109905_0s.png)
利用Python实现简单的相似图片搜索的教程
2023-10-24 18:45:36
![](https://img.aspxhome.com/file/2023/6/79466_0s.jpg)
非常不错的SQL语句学习手册实例版第1/3页
2024-01-25 23:31:01
![](https://img.aspxhome.com/file/2023/6/113576_0s.gif)
设置python3为默认python的方法
2023-07-11 19:24:49
![](https://img.aspxhome.com/file/2023/4/110074_0s.png)
mysql慢查询优化之从理论和实践说明limit的优点
2024-01-26 11:59:51
实例讲解Python中函数的调用与定义
2022-10-25 02:47:55
小看了setTimeout()
2009-12-04 12:44:00
Python过滤列表用法实例分析
2021-03-21 03:47:49
Python numpy实现二维数组和一维数组拼接的方法
2022-02-24 09:40:53
pycharm 批量修改变量名称的方法
2021-09-07 22:48:29
![](https://img.aspxhome.com/file/2023/9/75169_0s.jpg)
XMLTextReader和XmlDocument读取XML文件的比较
2008-09-05 16:04:00
python ftp 按目录结构上传下载的实现代码
2021-01-28 00:38:33