我已经被这个错误困住了一个星期。 OSError: [Errno 9] 错误的文件描述符

时间:2021-01-30 17:58:51

标签: python multithreading sockets

根据一些 Python 教程和使用 JavaScript 的记忆,我使用套接字创建了一个可用的 Python 聊天室。接下来我想给它添加加密,所以我决定使用 Base64 加密所有数据。它奏效了,我能够接收来自其他客户端的所有加密消息。但是当我尝试在客户端解码来自 Base64 的数据时,我得到 OSError: [Errno 9] Bad file descriptor 我该如何解决?

这是我的客户端代码:

import socket
import threading
import base64

# Choosing Nickname
nickname = input("Choose your nickname: ")

# Connecting To Server
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 55554))

# Listening to Server and Sending Nickname

def write():
    while True:
        message = '{}: {}'.format(nickname, input(''))
        message = message.encode('ascii')
        message = base64.b64encode(message)
        client.send(message)

def receive():
    while True:
        try:
            # Receive Message From Server
            # If 'NICK' Send Nickname
            message1 = client.recv(1024).decode('ascii')
            if message1 == 'NICK':
                client.send(nickname.encode('ascii'))
            else:
                print(base64.b64decode(message1))
        except:
            # Close Connection When Error
            print("An error occured!")
            client.close()
            break



# Starting Threads For Listening And Writing
receive_thread = threading.Thread(target=receive)
receive_thread.start()

write_thread = threading.Thread(target=write)
write_thread.start()

和服务器:

import socket
import threading
import base64

# Connection Data
host = '127.0.0.1'
port = 55554

# Starting Server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen()

# Lists For Clients and Their Nicknames
clients = []
nicknames = []

# Sending Messages To All Connected Clients
def broadcast(message):
    for client in clients:
        client.send(message)

# Handling Messages From Clients
def handle(client):
    while True:
        try:
            # Broadcasting Messages
            message = client.recv(1024)
            broadcast(message)
        except:
            # Removing And Closing Clients
            index = clients.index(client)
            clients.remove(client)
            client.close()
            nickname = nicknames[index]
            broadcast('{} left!'.format(nickname).encode('ascii'))
            nicknames.remove(nickname)
            break

# Receiving / Listening Function
def receive():
    while True:
        # Accept Connection
        client, address = server.accept()
        print("Connected with {}".format(str(address)))

        # Request And Store Nickname
        client.send('NICK'.encode('ascii'))
        nickname = client.recv(1024).decode('ascii')
        nicknames.append(nickname)
        clients.append(client)

        # Print And Broadcast Nickname
        print("Nickname is {}".format(nickname))
        joinmessage = "{} joined!".format(nickname).encode('ascii')
        broadcast(base64.b64encode(joinmessage))
        client.send('Connected to server!'.encode('ascii'))

        # Start Handling Thread For Client
        thread = threading.Thread(target=handle, args=(client,))
        thread.start()

receive()

1 个答案:

答案 0 :(得分:0)

看起来问题是我在用 Base64 解码之前没有解码消息。

else:
    message1 = base64.b64decode(message1)
    print(message1.decode('ascii'))