我遇到了一个在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}$
替换此项:
有了这个:
^{pr2}$在python3中,unicode字符串现在是默认值。套接字需要字节字符串,因此必须将unicode字符串转换为字节字符串。您可以使用
.encode()
来执行此操作。在我掩盖了一点(很多)与问题没有直接关系的东西。Ned Batchelder's Unipain talk是一个很好的资源。在
相关问题 更多 >
编程相关推荐