Python 3.x TypeError:需要类似字节的对象,而不是'_io.BufferedReader';

Python 3.x TypeError:需要类似字节的对象,而不是'_io.BufferedReader';,python-3.x,Python 3.x,我运行这个脚本 # -*- coding: utf-8 -*- import socket def snd(): a = ent.get() sock.send(a.encode()) print("Отправка текста") def sndfle(): b = ent2.get() file = open(b, "rb") sock.send(file) print("Отправка файла") sock = sock

我运行这个脚本

# -*- coding: utf-8 -*-
import socket

def snd():
    a = ent.get()
    sock.send(a.encode())
    print("Отправка текста")
def sndfle():
    b = ent2.get()
    file = open(b, "rb")
    sock.send(file)
    print("Отправка файла")


sock = socket.socket()
sock.connect(('localhost', 25565))
print ("Соединение с сервером установлено")
我看到了这个错误

    sock.send(file)
TypeError: a bytes-like object is required, not '_io.BufferedReader'
请帮帮我 我使用按钮运行def。我把它藏起来。

您需要从文件中获取字节,以便通过套接字发送。试试这个:

def sndfle():
    b = ent2.get()
    with open(b, "rb") as file:
        for data in file:
            sock.sendall(data)
    print("Отправка файла")

sock.send(file.read()
能做到这一点吗?不,这不是worksoops,对不起,应该读
sock.send(file.read())
当然了……它不起作用。。。