Как сделать функцию удара в чат-комнате Python? - PullRequest
0 голосов
/ 26 февраля 2019

Это сокет-чат, где клиенты могут отправлять сообщения друг другу.Я хочу использовать функцию «выкинуть», когда сервер может выгнать определенных людей.

Мне удалось сделать так, чтобы он выгонял нужного пользователя, но он все еще выгоняет меня, это код, который делаетэто:

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

Ответы [ 2 ]

0 голосов
/ 26 февраля 2019

Вы можете использовать del вместо .pop

Комментарий clients.get(name).pop код и написать del clients[name] вместо.

Вам необходимо удалить clients.pop(uname) из блока кроме.Вот и все.

Вот код

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'))
0 голосов
/ 26 февраля 2019

клиентов - это словарь.Таким образом, в сущности, удалить конкретного пользователя означает удалить его учетные данные из словаря клиентов.Поэтому вместо clients.get(name).pop
используйте clients.pop(name)

Редактировать: Кроме того, вместо for name in keys: используйте for name in list(clients): Поскольку вы не можете изменить размер словаря при его повторении.Это бросит исключение.Тем не менее, этот код только удалит пользователя из словаря, не выгоняя вас.Пользователь не будет выгнан, если вы не выберете значение пользователя из словаря и не используете .close ().Надеюсь, это поможет

...