Python中文
首页
教程
问答
标签
搜索
登录
注册
twisted未使用twisted客户端和服务器tcp传输发送整个文件
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>编辑:由于我是通过文本附加文件,文件没有正确保存,所以我决定重写我最初希望的方式,并将文件保存为流: Twisted服务器:</p> <pre><code>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() </code></pre> <hr/> <h2>原文如下</h2> <p>Twisted发送了99.9%的文件,然后似乎就是这样,我认为我写的文件不正确。在</p> <p>Twisted服务器:</p> ^{pr2}$ <p>扭曲客户端:</p> <pre><code>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() </code></pre> <p>基本上,服务器正在运行和监听,客户端连接并立即发送json,服务器接收数据包并告诉send client“ok”,然后客户端发送文件;然后服务器接收文件并将其写入磁盘。我注意到,这个文件的大小,并不是很重要,只是在测试后,我写了300个字节的文件。 我是不是把文件发错了?或者只是写错了?是的,我在同一台电脑上测试服务器和客户机。在</p> <p>最终,我计划在两台本地计算机之间发送1GB大小的文件以备备份,并希望这些文件以数据流的形式写入,我不喜欢我使用的<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>方法,但我不知道如何引用file对象而不实际打开文件,这是我第一次收到json对象时才想做的事情。在</p> <p>谢谢!在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>你在开门”测试.png“用于附加文本。这是故意的吗?在</p> <p>您还有一个空的<code>except</code>,这是一个坏主意,因为它捕获<em>所有</em>异常。只捕获您期望的异常。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
使用Python创建一个非常大的二进制频率矩阵来运行协作过滤
1 回答
使用Python创建一张HTML网页,其中在不同颜色中重复n遍显示“Hello World”的方法
6 回答
使用Python创建一组唯一的值length L
2 回答
使用python创建不同表格的透视表
1 回答
使用python创建不和谐频道
9 回答
使用python创建不存在的多个文件夹
10 回答
使用python创建串行远程文件
2 回答
使用python创建交互式仪表板时出现问题
6 回答
使用python创建交互式绘图
9 回答
使用python创建交互式自动电子邮件
7 回答
使用Python创建价格列表
6 回答
使用python创建修改的txt文件
9 回答
使用Python创建全局变量,初始化后更改值
1 回答
使用Python创建关键字搜索词数组
6 回答
使用Python创建具有不均匀块大小/堆叠条形图的热图
7 回答
使用Python创建具有依赖于另一列的值的列
9 回答
使用Python创建具有多列的HTML表
3 回答
使用Python创建具有时间范围数据的等距数据帧
3 回答
使用Python创建具有特定顺序或属性的XML文件
6 回答
使用Python创建具有级联功能的搜索栏
6 回答