如何在Python Socket聊天室中实现踢人功能?

4
这是一个套接字聊天室,客户可以互相发送消息。我想要一个踢出功能,使服务器可以踢出特定的人。
我已经成功实现了将所需用户踢出,但是它仍然也将我自己踢出,请看下面的代码:
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

但是当我运行这段代码并尝试时,我被踢出了。这是我的完整代码:

Server.py

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()

Client.py

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'))
3个回答

3

clients是一个字典。基本上,踢出一个特定的用户意味着从clients字典中删除他的凭证。所以,而不是使用clients.get(name).pop,应该使用clients.pop(name)

编辑:还要使用for name in list(clients):而不是for name in keys:。因为在迭代期间不能更改字典的大小,否则会抛出异常。然而,这段代码只会从字典中删除用户而不会踢出您。除非您获取用户的值并使用.close(),否则用户不会被踢出。希望这能帮到你。


在试图将用户从聊天中踢出时,您会收到一个异常。这是因为在遍历字典时,您不能改变其大小。请参见编辑后的答案。 - karun
我该如何使用.close() - Hass786123

2

你可以使用del代替.pop

注释掉clients.get(name).pop的代码,并改为del clients[name]

你需要将clients.pop(uname)从except块中删除。就这样。

以下是代码:

server.py

import socket, threading
host = "127.0.0.1"
port = 5000
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:
                        print('Name: '+ name)
                        print('Client: '+ str(clients))
                        # clients.get(name).pop
                        del clients[name]
                        found = True
                if(not found):
                    conn.send('Trying to send message to invalid person.'.encode('ascii'))


        except:
            print(uname + ' has logged out')
            clientConnected = False

while serverRunning:
    conn,addr = s.accept()
    uname = conn.recv(1024).decode('ascii')
    print('User : '+ uname)
    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):
        print("Conn: " + str(conn))
        clients[uname] = conn
        threading.Thread(target = handle_client, args = (conn, uname,)).start()

Client.py

#!/usr/bin/env python3

import socket,threading
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
# uname = raw_input("Enter username: ")
uname = input("Enter username: ")
print('Uname: '+ str(uname))
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'))

5
我在我的机器上测试了你的代码,它正常工作。你的代码和上面的不同吗? - Mayur
这并没有什么不同,因为我复制了上面的代码并按照你的建议进行了更改,但它仍然在踢我 :( - Hass786123
你使用的Python版本是哪个? - Hass786123
我完全复制了你的代码,但它仍然把我踢出去。 - Hass786123
我找到了为什么它一直把我踢出去的原因,现在我的代码可以运行了,感谢你们的帮助 :) - Hass786123
显示剩余2条评论

1

我一直在制作一个聊天应用,对我来说最好的封禁方法是:

  1. 建立一个数据库
  2. 给用户提供用户名、密码和数字ID
  3. 每当有人连接时,要求他们提供凭据(ID可以由程序直接提供,用户可能无需提供)
  4. 如果想要封禁某人,使用数据库中的信息
  5. 如果在数据库中该用户已被封禁,则关闭套接字。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接