0

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?

wspeirs
  • 1,321
  • 2
  • 11
  • 22

1 Answers1

0

I ended up dumping Klein and going with just Twisted

from twisted.web import server, resource
from twisted.internet import reactor, protocol

import os


class CurlProcProtocol(protocol.ProcessProtocol):
    def __init__(self, request):
        self.request = request

    def connectionMade(self):
        self.transport.write(self.request.content.read())
        self.transport.closeStdin()

        self.request.write("Done!\n")
        self.request.finish()

    def outReceived(self, data):
        print 'GOT: ' + str(data)


class Simple(resource.Resource):
    isLeaf = True

    def render_POST(self, request):

        reactor.spawnProcess(CurlProcProtocol(request),
                             '/usr/bin/curl',
                             args=['curl', '-T', '-', 'ftp://localhost/test.txt'],
                             env={'HOME': os.environ['HOME']},
                             usePTY=False)

        return server.NOT_DONE_YET


site = server.Site(Simple(), logPath='access.log')
reactor.listenTCP(8080, site)
reactor.run()
wspeirs
  • 1,321
  • 2
  • 11
  • 22