I'm looking to pipe the content of a HTTP POST or PUT to STDIN of a process. I'm using the Klein library and have the following code:
from klein import run, route
from twisted.internet import reactor, defer, protocol
import os
class CurlProcProtocol(protocol.ProcessProtocol):
def __init__(self, data):
self.data = data
def connectionMade(self):
self.transport.write(self.data)
self.transport.closeStdin()
def outReceived(self, data):
return 'got ' + str(data)
@route('/')
def home(request):
d = defer.Deferred()
reactor.spawnProcess(CurlProcProtocol(request.channel),
'/usr/bin/curl',
args=['curl', '-T', '-', 'ftp://localhost/test.txt'],
env={'HOME': os.environ['HOME']},
usePTY=False)
d.addCallback(reactor.run)
return d
run("localhost", 8080)
The problem I'm struggling with is what part of Request
do I pass into my CurlProcProtocol
, and how do then in turn pass it to self.transport
?