python聊天室支持私聊和群聊

问题描述 投票:0回答:0

我是学计算机的 我需要有关 python 客户端/服务器代码的帮助 这段代码的想法应该支持私人聊天和群聊,服务器文件中也应该有 3 个列表 别名 , 客户 , 集团 这是我的代码 我使用 Python 3.0.1,因为它是我旧电脑上唯一可用的 我的问题是第二条消息没有发送到服务器。

谢谢你的时间。

Server.py

import threading
import socket
import time
ser_lock = threading.Lock()

host = '127.0.0.1'
port = 52000
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(7)
#print('WELCOME TO CHAT ROOM ...')
clients = []
aliases = []
groups= []
group_id=0
def broadcast(message):
    for client in clients:
        client.send(message)


def send_def(destination,message):
    while True:
        i = aliases.index(destination)
        print(i)
        client1=clients[i]
        client1.send(message)
       #except:
          #print("error")

def broadcastForgroup(client, group_id, distination,message):  
        for distination in groups[group_id]:  
            if distination != client:
                try:
                    distination.send(message.encode())  
                    time.sleep(0.1)
                    
                except:
                    distination.close()
# Function to handle clients'connections
def handle_client(message):
        while True:
            #message = client.recv(1024)
            list=str(message).split(":")
            distination=list[0]
            msg=list[1]
            send_def(msg,distination)
        
# Main function to receive the clients connection
  
def receive():
        while True:
            print('Server is running and listening ...')
            client, address = server.accept()
            print('connection is established with')
            print(address)
            client.send('alias?'.encode('utf-8'))
            alias = client.recv(1024)
            aliases.append(alias)
            clients.append(client)
            print(' The alias of this client is '.encode('utf-8'))
            broadcast(alias+' has connected to the chat room\n'.encode('utf-8')) 
            client.send('you are now connected!\n'.encode('utf-8'))
            client.send('write message and destination !\n'.encode('utf-8'))
            message=client.recv(1024)
            thread = threading.Thread(target=handle_client, args=(message,))
            thread.start()


if __name__ == "__main__":
    receive()

client.py

import threading
import socket

alias = input('Choose an alias >>> ')
distination= input('Choose an client or groupe >>> ')
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 52000))

def client_receive():
    while True:
        try: 
            message = client.recv(1024).decode('utf-8')
            if message == "alias?":
                client.send(alias.encode('utf-8'))
            else:
                print(message)
                msg = distination +":"+input()
                client.send(msg.encode('utf-8')) 
        except:
            print('Error!')
            client.close()
            break
#def client_send():
    #while True:
           

receive_thread = threading.Thread(target=client_receive)
receive_thread.start()


#send_thread = threading.Thread(target=client_send)
#send_thread.start()
python sockets tcp client-server chatroom
© www.soinside.com 2019 - 2024. All rights reserved.