【问题标题】:TypeError: a bytes-like object is required, not '_io.BufferedReader'TypeError:需要一个类似字节的对象,而不是'_io.BufferedReader'
【发布时间】:2017-08-11 11:38:51
【问题描述】:

我运行这个脚本

# -*- 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(s)。我把它藏起来了。

【问题讨论】:

  • sock.send(file).read() 能解决问题吗?
  • 不,这行不通
  • 哎呀,抱歉,当然应该是sock.send(file.read())...
  • 这行不通...

标签: python-3.x


【解决方案1】:

this question

您需要从文件中获取字节才能通过套接字发送它。试试这个:

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

【讨论】:

    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多