【问题标题】:ironPython - python socket file transfer, typeErrorironPython - python 套接字文件传输,typeError
【发布时间】:2015-11-30 20:48:38
【问题描述】:

我正在尝试将二进制文件从运行在 ironPython 2.7.4 上的客户端发送到运行在 linuxbox 上的 cpython 2.7.6 上的服务器。我遵循this 示例,但是当服务器开始写入文件时(第一次调用f.write),我得到一个错误:

TypeError: must be string or buffer, not int

这里是我认为相关的代码。

服务器

def recvall(self, count):

    msgparts = []
    while count > 0:
        newbuf = self.conn.recv(count)
        if not newbuf: return None
        msgparts.append(newbuf)
        count -= len(newbuf)

        #print "%i bytes left" % count

    return "".join(msgparts)

#receive file, write out
f = open(fname, 'wb') 
chunk = self.recvall(1024)
while (chunk):
    f.write(1024) #<-- error happens here.
    chunk = self.recvall(1024)

    f.close()

客户

f = open(fname, 'rb') 
chunk = f.read(1024)
while (chunk):
    self.conn.send(chunk)
    chunk = f.read(1024)

f.close()

conn 是套接字连接 - 这行得通,我可以成功传输腌制的字典。

有什么提示吗?

感谢和问候, 多米尼克

【问题讨论】:

    标签: python sockets ironpython typeerror file-transfer


    【解决方案1】:
    f.write(chunk)
    

    应该这样做(而不是f.write(1024)

    正如错误消息所述,f.write 需要一个字符串参数,而 1024 显然是一个 int

    【讨论】:

    • 是的,当然,现在我觉得自己很愚蠢……谢谢你治愈了我的失明:D
    猜你喜欢
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多