Python twisted未使用twisted客户端和服务器tcp传输发送整个文件
编辑:由于我是通过文本追加文件,文件保存不正确,我决定重写我最初希望的方式,并将文件保存为流: Twisted服务器:Python twisted未使用twisted客户端和服务器tcp传输发送整个文件,python,tcp,twisted,file-transfer,Python,Tcp,Twisted,File Transfer,编辑:由于我是通过文本追加文件,文件保存不正确,我决定重写我最初希望的方式,并将文件保存为流: Twisted服务器: from twisted.internet import reactor, protocol import os,json class Echo(protocol.Protocol): f = file def dataReceived(self, data): try: try: pri
from twisted.internet import reactor, protocol
import os,json
class Echo(protocol.Protocol):
f = file
def dataReceived(self, data):
try:
try:
print format(json.loads(data))
print "got jason"
self.f=open("test.png","wb")
self.transport.write("ready")
except:
print "filedata incoming!"
self.f.write(data)
except:
print "unknown error" #happens if we don't receive json first
def connectionLost(self, reason):
if self.f!=file:self.f.close()
def main():
"""This runs the protocol on port 8000"""
factory = protocol.ServerFactory()
factory.protocol = Echo
reactor.listenTCP(8000,factory)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
原文如下 Twisted发送了99.9%的文件,似乎就是这样,我认为我写的文件不正确 Twisted服务器:
from twisted.internet import reactor, protocol
import os,json
class Echo(protocol.Protocol):
def dataReceived(self, data):
try:
print format(json.loads(data))
print "got jason"
self.transport.write("ready")
except:
print "filedata incoming!"
f = open("test.png","a")
f.write(data)
f.close()
def main():
"""This runs the protocol on port 8000"""
factory = protocol.ServerFactory()
factory.protocol = Echo
reactor.listenTCP(8000,factory)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
Twisted客户端:
from twisted.internet import reactor, protocol
import os,json
fname="pic.png"
class EchoClient(protocol.Protocol):
"""Once connected, send a message, then print the result."""
def connectionMade(self):
fsize = os.path.getsize(fname)
self.transport.write(json.dumps({"file":{"size":fsize}}))
def sendFile(self):
print "sending file"
f = open(fname,"rb")
self.transport.write(f.read())
f.close()
print "closing conn"
self.transport.loseConnection()
def dataReceived(self, data):
"As soon as any data is receive"
print "Server said: ", data
self.sendFile()
def connectionLost(self, reason):
print "connection lost"
class EchoFactory(protocol.ClientFactory):
protocol = EchoClient
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop()
# this connects the protocol to a server runing on port 8000
def main():
f = EchoFactory()
reactor.connectTCP("localhost", 8000, f)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
基本上,服务器正在运行和侦听,客户端连接并立即发送json,服务器接收数据包并告诉发送客户端“ok”,然后客户端发送文件;然后服务器接收文件并将其写入磁盘。我只是在测试一些东西,所以这个程序可能没有多大意义,特别是使用file append——但我注意到在传输和最终写入之后,文件的大小与原始文件差不多,但不完全相同,并且大约小300字节,因此几乎没有用。
我发送的文件有误吗?或者只是写错了?哦,是的,我正在同一台计算机上测试服务器和客户端
最终,我计划在两台本地计算机之间发送1GB大小的文件进行备份,并希望文件以数据流的形式写入,我不喜欢我使用的append方法,但我不知道如何引用文件对象,而不首先实际打开文件,这是我第一次收到json对象时才想做的事情
谢谢 您正在打开“test.png”以添加文本。这是故意的吗
除了,您还有一个空的
,这是个坏主意,因为它捕获所有异常。只捕获您期望的异常。问题是您期望所有数据都能被dataReceived
一次接收到。互联网不是这样工作的:。我想不出一个好办法,在第一条json消息之后打开文件,并且在随后发送文件数据时仍然正确地引用文件,所以我选择了append——尽管我没有意识到这是用于文本的;我需要使用r+b和seek命令吗?我真的只想将内容流式传输到文件中,然后关闭它,问题是流以几个数据包的形式出现,因此函数被多次调用。我还意识到,除非是开放的失败,我计划清理,一旦我有文件保存正确。谢谢你的意见!要以二进制模式进行追加,请使用“ab”。我现在可以正确地编写文件,谢谢:)现在可以找到一种更有效的方法来执行此操作,而不是追加文件。我完全不希望发生这种情况,我希望函数可以多次运行。我告诉你的是,你的dataReceived
不正确。它有时会失败。你不能期望每次调用它时它都是一个完整的JSON对象。很好,我必须构建一个更好的系统来处理零碎的JSON对象,因为我的JSON对象太小了,我还没有经历过