Python 3:服务器错误:TypeError:“str”不支持中的缓冲区

2024-10-01 19:23:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了一个在python2中执行类似操作时没有的错误。在

代码如下:

def computeSha(self,reqFile):
    filesize_bytes = os.path.getsize(reqFile)

    s = sha1()
    s.update(("blob %u\0" % filesize_bytes).encode('utf-8'))

    with open(reqFile, 'rb') as f:
        s.update(f.read())

    s = s.hexdigest()
    print ("here is the sha: " + s)
    return s

def _sendSha(self, component_id):
    component_path = db_connector.get_design_path(component_id)
    sha = self.computeSha(component_path)

    self.connection.send(self._adjustLength(len(sha)))
    self.connection.sendall(data)

错误出现在这里:self.connection.send(自行调整长度(长度(sha))

以下是调整长度的代码:

^{pr2}$

Tags: path代码selfsendidbytesdef错误
1条回答
网友
1楼 · 发布于 2024-10-01 19:23:43

替换此项:

self.connection.send(self._adjustLength(len(sha)))

有了这个:

^{pr2}$

在python3中,unicode字符串现在是默认值。套接字需要字节字符串,因此必须将unicode字符串转换为字节字符串。您可以使用.encode()来执行此操作。在

我掩盖了一点(很多)与问题没有直接关系的东西。Ned Batchelder's Unipain talk是一个很好的资源。在

相关问题 更多 >

    热门问题