- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
这是一个套接字聊天室,客户端可以在其中相互发送消息。我想要一个踢球功能,服务器可以踢出某些人。
我已经成功地把想要的用户踢出去了,但它仍然把我踢出去,这是执行此操作的代码:
for name in keys:
if('**kick '+name) in data:
clients[name].close()
del clients[name]
found = True
我试过这个:
for name in keys:
if('**kick '+name) in data:
data = data.replace('**kick '+name,'')
clients.get(name).pop
found = True
但是当我运行这段代码并尝试它时,我反而被踢了。
这是我的完整代码:
import socket, threading
host = "127.0.0.1"
port = 4000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen()
clients = {}
print("Server is ready...")
serverRunning = True
def handle_client(conn, uname):
clientConnected = True
keys = clients.keys()
help = 'There are 3 commands in Messenger\n1**chatlist > gives you the list of the people currently online\n2**quit > To end your session and quit the server\n3**(username) sends a private message to any user you want'
while clientConnected:
try:
response = 'Number of People Online\n'
data = conn.recv(1024).decode('ascii')
found = False
if '**' not in data:
for k,v in clients.items():
if v != conn:
v.send(data.encode('ascii'))
found = True
elif '**chatlist' in data:
clientNo = 0
for name in keys:
clientNo += 1
response = response + str(clientNo) +'::' + name+'\n'
conn.send(response.encode('ascii'))
found = True
elif '**help' in data:
conn.send(help.encode('ascii'))
found = True
else:
for name in keys:
if('**'+name) in data:
data = data.replace('**'+name,'')
clients.get(name).send(data.encode('ascii'))
found = True
if('**kick '+name) in data:
clients.get(name).pop
found = True
if(not found):
conn.send('Trying to send message to invalid person.'.encode('ascii'))
except:
clients.pop(uname)
print(uname + ' has logged out')
clientConnected = False
while serverRunning:
conn,addr = s.accept()
uname = conn.recv(1024).decode('ascii')
print('%s connected to the server'%str(uname))
conn.send('Welcome to Messenger. Type **help to know all the commands'.encode('ascii'))
if(conn not in clients):
clients[uname] = conn
threading.Thread(target = handle_client, args = (conn, uname,)).start()
import socket,threading
host = "127.0.0.1"
port = 4000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
uname = input("Enter username: ")
s.send(uname.encode('ascii'))
clientRunning = True
def echo_data(sock):
serverDown = False
while clientRunning and (not serverDown):
try:
data = sock.recv(1024).decode('ascii')
print(data)
except:
print('Server is Down. You are now Disconnected. Press enter to exit...')
serverDown = True
threading.Thread(target=echo_data, args = (s,)).start()
while clientRunning:
tempMsg = input()
data = uname + '>> ' + tempMsg
s.send(data.encode('ascii'))
最佳答案
clients 是一个字典。所以基本上,踢一个特定的用户意味着从客户字典中删除他的凭据。所以而不是 clients.get(name).pop
利用 clients.pop(名称)
编辑:也不要使用 for name in keys:
使用 作为列表中的名称(客户):
因为,您不能在遍历字典时更改字典的大小。它会抛出异常。但是,此代码只会将用户从字典中删除,而不会将您踢出。除非您从字典中获取用户的值并使用 .close(),否则用户不会被踢出。希望这有帮助
关于python - 如何在 Python 套接字聊天室中创建踢功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54891425/
我有聊天应用程序,可以一对一发送消息(fromId/toId)。我想升级它的聊天室。我怎样才能做到这一点? ChatingRoom 需要什么数据库结构?我还需要做什么? 我的 User.swift 模
本文实例讲述了python socket多线程通讯方法。分享给大家供大家参考,具体如下: ?
我正在使用简单的套接字连接创建一个聊天室。我有一个服务器和客户端程序。服务器在端口 225 上运行,然后当我在端口 225 上运行客户端以便它们可以读/写套接字时,客户端立即停止并显示错误消息 jav
我可以创建一个粘液室。但是如果我将房间名称指定为数据库中现有的房间名称,服务器会拒绝它。那么是否可以在 ejabberd 服务器(MYSQl 后端)中创建两个具有相同名称的房间? 如果可能的话,后果是
我正在尝试使用 PHP 创建一个聊天室(顺便说一句,它正在工作),但只显示消息,而不显示他们的用户名。我已经为他们创建了数据库、用户名和消息。我不知道为什么他们的用户名没有显示 ".$extract[
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我想在我的应用程序中实现群聊,为此我正在使用 MUC 聊天室来实现它。 在这里,我想向房间添加一个成员列表(我有 JID)。我想在内部将它们添加到列表中的所有成员。我如何在不邀请他们的情况下添加他们。
我正在尝试在 Rails 中创建类似 Whatsapp 的消息应用程序服务器端,并进行私有(private)对话。现在,我正在尝试实现应用程序的实时部分 - 我正在使用 websocket-rails
我正在尝试为我的网站制作一个 PHP/JavaScript 聊天系统。如何在不刷新页面的情况下做到这一点? JavaScript 可以:--动态添加文本到文本框。 PHP 可以:--将聊天内容保存到聊
我已经为多个客户端完全开发了一个聊天室,其多线程服务器可以完成这项工作,但仅限于我的本地计算机。我想超越这个,让这个聊天室通过互联网工作。到目前为止,我已经在路由器上对 TCP 协议(protocol
我是 Angular 的初学者,我正在尝试构建一个聊天室应用程序,以此自学如何在该框架中进行开发。 我在后端使用 PHP 和 Yii 将 RESTful 数据发送回 Angular 应用程序。我知道我
我正在寻找好的建议来制作这个聊天室: 使用 Java Swing。我已经尝试了所有 java swing 组件 3 天,但我仍然不知道该选择什么。 容器还必须插入 JprogressBars 以进行文
嗨,我正在努力让两件事同时工作...... channels2 chat room例子可以开始,但我想添加一个功能,知道房间里有多少人。我通过更新房间模型来做到这一点。 然后我想要一个仪表板来显示当前
我正在编写一个需要持久存储对话的多聊天室应用程序(即新用户应该能够看到旧消息)。如果有帮助,我正在使用 socket.io。 目前,当用户登录到一个房间时,我的 Node 应用程序会检查是否有人去过那
我试图扩展doc中关于TCP的代码来制作一个简单的聊天室,简单地说,几个客户端连接到服务器,一个客户端发送一个字符串,服务器将字符串广播给所有客户端......我写的代码显示下面,它不起作用,有人可以
如何在 Microsoft-Teams 中自动向聊天室发布消息?这是用于单向消息传递:即发布消息,而不是阅读消息。 这里的大局是我们正在评估不同的群聊解决方案,其中一项要求是从各种服务和程序向聊天室发
我创建了一个小型的 jquery 和 php 聊天室,其中包含一些 .get 和 .post 函数以及用于将数据读写到 sql server 的 php 文档。它工作正常,但小问题是当有人发布一些东西
我想实现的是自动下载附件功能,无需进入 ChatRoom,使用 QuickBlox SDK iOS 版本 2.0.12, 为此,我想在登录后启动用户所属的所有对话框(聊天室)。 使用当前的 API,用
所以我有聊天室工作所以如果收件人是 All 或 ALL 它广播我有这个工作所以如果你的名字是'Sam'它发送它与“私有(private)消息:”文本我不知道如何检查用户名的花名册并将其私下发送给该用户
我正在尝试创建基于移动设备的聊天应用。 基本上,我希望用户使用他们的 Facebook 帐户连接到 Facebook(使用 XMPPFramework) 我想要预先存在的房间供用户加入。我注意到 XM
我是一名优秀的程序员,十分优秀!