Python socket实现简单聊天室
作者:萧-十一 时间:2022-06-20 02:13:27
本文实例为大家分享了Python socket实现简单聊天室的具体代码,供大家参考,具体内容如下
服务端使用了select模块,实现了对多个socket的监控。客户端由于select在Windows下只能对socket使用,所以使用了多线程来实现对客户端输入和socket连接的同时监控。注意这里的socket设置为了非阻塞。这样就实现了在一个线程中同时进行socket的接收和发送。
服务器代码:
# -*- coding: utf-8 -*-
import socket,select
connection_list = []
host = ''
port = 10001
def board_cast(sock,message):
for socket in connection_list:
if socket != server_sock and socket != sock:
try:
socket.send(message)
except:
socket.close()
print str(socket.getpeername())+' is offline'
connection_list.remove(socket)
server_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server_sock.setblocking(0)
server_sock.bind((host,port))
server_sock.listen(10)
connection_list.append(server_sock)
while 1:
readable,writable,error = select.select(connection_list,[],[])
for sock in readable:
if sock == server_sock:
connection,connection_add = sock.accept()
message = str(connection_add)+'enter room'
board_cast(connection,message)
print connection_add,'%s connect'
connection_list.append(connection)
else:
try:
date = sock.recv(1024)
print date
board_cast(sock,'('+str(sock.getpeername())+') :'+date)
except:
message2 = str(sock.getpeername())+ 'is offline'
board_cast(sock,message2)
print str(sock.getpeername())+ ' is offline'
sock.close()
connection_list.remove(sock)
continue
客户端代码:
# -*- coding: utf-8 -*-
import socket,threading,time
flag = 0
date = ''
lock = threading.Lock()
host = 'localhost'
port = 10001
client_sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client_sock.setblocking(0)
class Mythread1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global flag, date
while 1:
date = raw_input()
if len(date):
lock.acquire()
flag = 1
lock.release()
class Mythread2(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global flag
global date
while 1:
try:
buf = client_sock.recv(1024)
if len(buf):
print buf
except:
pass
if flag:
try:
client_sock.send(date)
except socket.error, e:
print e
lock.acquire()
flag = 0
lock.release()
try:
client_sock.connect((host,port))
print"连接成功"
except socket.error,e:
print e
t1 = Mythread1()
t2 = Mythread2()
t1.start()
t2.start()
来源:https://blog.csdn.net/wait_nothing_alone/article/details/53946597
标签:Python,socket,聊天室
0
投稿
猜你喜欢
TensorFlow中tf.batch_matmul()的用法
2022-06-06 10:33:37
django 2.2和mysql使用的常见问题
2024-01-27 17:40:02
为你的有序列表添加个性样式
2009-02-23 13:12:00
MySQL深分页问题解决的实战记录
2024-01-18 21:32:36
MySQL按照汉字的拼音排序简单实例
2024-01-22 00:32:41
pycharm sql语句警告的处理
2022-01-03 18:48:15
ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)
2011-04-19 11:15:00
并行查询让SQL Server加速运行
2009-03-16 16:31:00
plt.subplot()参数及使用介绍
2022-03-23 09:17:03
分享4个Python中高效省时的技巧
2021-02-14 22:32:09
python保存图片的四个常用方法
2023-10-03 15:06:37
基于jupyter代码无法在pycharm中运行的解决方法
2023-09-15 06:46:38
如何使用Python处理HDF格式数据及可视化问题
2023-11-21 00:17:01
在页面中动态载入外部javascript
2007-09-30 12:52:00
使用Title提升可访问性二
2009-11-16 12:53:00
Python+Pygame实战之英文版猜字游戏的实现
2021-01-18 01:40:32
5款实用的python 工具推荐
2021-08-09 20:13:28
MySQL 存储过程的基本用法介绍
2024-01-20 15:56:33
python 协程并发数控制
2023-09-20 01:07:10
win7下mysql6.x出现中文乱码的完美解决方法
2024-01-17 03:36:45