Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A basic FileSender example
import sys
from twisted.protocols import basic
from twisted.internet import defer, endpoints, protocol, task
class GetFileProtocol(basic.LineReceiver):
delimiter = '\n'
def lineReceived(self, line):
parts = line.split(None, 1)
if not parts or parts[0] != b'GET':
self.transport.write(b"Invalid command.\n")
self.transport.loseConnection()
return
path = parts[1]
fileobj = open(path, "rb")
sender = basic.FileSender()
def finish(ignored):
fileobj.close()
self.transport.loseConnection()
finished = sender.beginFileTransfer(fileobj, self.transport)
finished.addBoth(finish)
@defer.inlineCallbacks
def main(reactor, description):
server = endpoints.serverFromString(reactor, description)
yield server.listen(protocol.Factory.forProtocol(GetFileProtocol))
yield defer.Deferred()
task.react(main, sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.