【问题标题】:Im learning socket , but cant resolve the error in my client File我正在学习套接字,但无法解决我的客户端文件中的错误
【发布时间】:2020-03-22 22:46:29
【问题描述】:

client.py

while True:
        full_msg= ""
        new_msg = True
        while True:
                msg = c.recv(15)
                if new_msg :
                        msglen = int(msg[:headersize])
                        new_msg = False   
                full_msg += msg.decode("utf-8")
                if len(full_msg)-headersize == msglen:
                                print("full msg recvd, Length = ", msglen)
                                print(full_msg[headersize:])
                                new_msg = True


这显示错误为: ValueError: int() 以 10 为底的无效文字:b''

【问题讨论】:

  • print(msg[:headersize]) 的输出是什么?
  • 在最后一次迭代中:b''。
  • 什么是msgheadersize 是什么?你如何处理msgrest,你只能从中读取headersize
  • 这是一个代码提取,headersize = 10 定义在上面,header被添加到服务器文件中的消息中,在客户端文件中提取header并打印消息

标签: python python-3.x sockets


【解决方案1】:

错误显然出现在这一行(唯一一个 callint int():

msglen = int(msg[:headersize])

如果您检查msg,您会发现它是bytes 对象,而不是字符串。你需要在上面使用.decode

【讨论】:

    【解决方案2】:

    代替:

    msglen = int(msg[:headersize])
    

    尝试:

    msglen = int(msg[:headersize].decode('utf-8'))
    

    【讨论】:

      猜你喜欢
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多