【发布时间】:2016-05-07 02:12:33
【问题描述】:
您好,我已经使用 Python 中的套接字创建了一个客户端和服务器架构来传输文件。它在 Windows 中工作得非常好,但在 ubuntu 中它不起作用。在 Ubuntu 中没有错误,但没有发送整个文件。如果我尝试发送 4mb 的音乐文件,则只有 50-60kb 被传输,而在 Windows 中,即使是 300mb 的文件也可以完美发送。这是我的代码。
客户-
def sendFile(self):
# ''' Print a language constructed from
# the selections made by the user. '''
# print('%s!' % (self.recipient.displayText()))
client_socket.send("upload")
time.sleep(1)
client_socket.send(self.virtual_os[self.os_box.currentIndex()].title())
time.sleep(1)
path = self.recipient.displayText() #inputbox which contains the path of the file
self.recipient.setText('')
name = path.split('/')
name = name[len(name)-1]
print "Opening file - ",name
client_socket.send(name)
time.sleep(1)
fp = open(path,'rb')
data = fp.read()
fp.close()
size = os.path.getsize(path)
size = str(size)
client_socket.send(size)
time.sleep(1)
client_socket.send(data)
print "Data sent successfully"
服务器-
choice = client_socket.recv(1024)
if(choice == "upload"):
virtual_os = client_socket.recv(1024)
print virtual_os
fname = client_socket.recv(1024)
print "recieved file "+fname
size = client_socket.recv(1024)
size = int(size)
print "The file size is - ",size," bytes"
size = size*2
strng = client_socket.recv(size)
fp = open(fname,'wb')
fp.write(strng)
fp.close()
print "Data Received successfully"
我的代码有问题还是我必须更改某些内容才能使其在 Ubuntu 上运行?
【问题讨论】:
-
为什么不直接使用
len(data)而不是os.path.getsize()? -
os.path.getsize() 工作正常,因为我打印了大小并且它打印了正确的大小,但是我尝试使用 len(data) 仍然不起作用。问题只在 Ubuntu 中。不知何故,只发送了一部分数据。
标签: python python-2.7 sockets ubuntu file-transfer