我希望将HTTP POST或PUT的内容传递给进程的STDIN。我正在使用Klein库并拥有以下代码:
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)
我遇到的问题是Request
我将CurlProcProtocol
的哪一部分传递到self.transport
,然后如何将其传递给int i = 0; // ...
char fn[16];
sprintf(fn, "file%02d.txt", i);
?
答案 0 :(得分:0)
我最终倾销了Klein并且只使用了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()