【问题标题】:TypeError: coercing to Unicode: need string or buffer, ZipFile foundTypeError:强制转换为 Unicode:需要字符串或缓冲区,找到 ZipFile
【发布时间】:2015-08-03 19:19:28
【问题描述】:

我有一个服务器,它应该向客户端询问文件,压缩它并将其发送给客户端。我在将 zip 文件发送到服务器时遇到了一些麻烦。

这是我收到的错误:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/Users/Alcantara/Desktop/Final/Server.py", line 9, in RetrFile
for dirname, subdirs, files in os.walk(Zip):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 278, in walk
names = listdir(top)
TypeError: coercing to Unicode: need string or buffer, ZipFile found

这是我的服务器:

import socket
import threading
import os
import zipfile

def RetrFile(name, sock):
    Zip = sock.recv(1024)
    Zip = zipfile.ZipFile("new_" + Zip +".zip", "w")
    for dirname, subdirs, files in os.walk(Zip):
        Zip.write(dirname)
        for filename in files:
            Zip.write(os.path.join(dirname, filename))
    Zip.close()
    with open(filename, 'rb') as f:
        bytesToSend = f.read(1024)
        sock.send(bytesToSend)
        while bytesToSend != "":
            bytesToSend = f.read(1024)
            sock.send(bytesToSend)

    sock.close()

def Main():
    host = '127.0.0.1'
    port = 5000


    s = socket.socket()
    s.bind((host,port))

    s.listen(5)

    print "Server Started."
    while True:
        c, addr = s.accept()
        print "client connedted ip:<" + str(addr) + ">"
        t = threading.Thread(target=RetrFile, args=("RetrThread", c))
        t.start()

    s.close()

if __name__ == '__main__':
    Main()

客户:

import socket

def Main():
host = '127.0.0.1'
port = 5000

s = socket.socket()
s.connect((host, port))

filename = raw_input("Filename? -> ")
if filename != 'q':
    s.send(filename)
    f = open('new_'+filename, 'wb')
    data = s.recv(1024)
    totalRecv = len(data)
    f.write(data)
    while totalRecv < filesize:
        data = s.recv(1024)
        totalRecv += len(data)
        f.write(data)
        print "{0:.2f}".format((totalRecv/float(filesize))*100)+ "% Done"
        print "Download Complete!"
    f.close()
else:
    print "File Does Not Exist!"

s.close()


if __name__ == '__main__':
    Main()

【问题讨论】:

  • 您发送到服务器的内容是什么?文件路径?还是文件目录的路径?
  • 我将路径发送到带有文件的目录。我想压缩目录(包括其中的文件)并将其作为 zip 文件发送给客户端。

标签: python unicode tcp network-programming zip


【解决方案1】:

考虑以下几行:

Zip = sock.recv(1024)
Zip = zipfile.ZipFile("new_" + Zip +".zip", "w")
for dirname, subdirs, files in os.walk(Zip):

您使用相同的变量名称来引用接收到的数据(第 1 行和第 3 行)以及 ZipFile 对象(第 2 行)。

试试这个:

#UNTESTED
zname = sock.recv(1024)
zfile = zipfile.ZipFile("new_" + Zip +".zip", "w")
for dirname, subdirs, files in os.walk(zname):
    zfile.write(dirname)
    for filename in files:
        zfile.write(os.path.join(dirname, filename))
zfile.close()
with open("new_" + zname + ".zip", 'rb') as f:
    # ... the rest is unchanged

【讨论】:

  • 通过使用您的更正我得到了这个错误:AttributeError: '_socketobject' object has no attribute 'write'
  • 我的错。你不能那样使用shutil.copyfileobj
  • 我有一个可以压缩文件的工作脚本和一个可以传输文件的工作服务器/客户端。我只需要知道如何将它们组合成一个脚本。
猜你喜欢
  • 2021-03-28
  • 2015-02-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-04
  • 2011-05-13
相关资源
最近更新 更多