這裏的,它流與扭曲的編寫上傳處理服務器的例子:
from twisted.internet import reactor
from twisted.internet.endpoints import serverFromString
from twisted.web.server import Request, Site
from twisted.web.resource import Resource
from twisted.application.service import Application
from twisted.application.internet import StreamServerEndpointService
# Define a Resource class that doesn't really care what requests are made of it.
# This simplifies things since it lets us mostly ignore Twisted Web's resource
# traversal features.
class StubResource(Resource):
isLeaf = True
def render(self, request):
return b""
class StreamingRequestHandler(Request):
def handleContentChunk(self, chunk):
# `chunk` is part of the request body.
# This method is called as the chunks are received.
Request.handleContentChunk(self, chunk)
# Unfortunately you have to use a private attribute to learn where
# the content is being sent.
path = self.channel._path
print "Server received %d more bytes for %s" % (len(chunk), path)
class StreamingSite(Site):
requestFactory = StreamingRequestHandler
application = Application("Streaming Upload Server")
factory = StreamingSite(StubResource())
endpoint = serverFromString(reactor, b"tcp:8080")
StreamServerEndpointService(endpoint, factory).setServiceParent(application)
這是一個TAC文件(把它放在streamingserver.tac
和運行twistd -ny streamingserver.tac
)。
由於需要使用self.channel._path
這不是一個完全支持的方法。 API的整體也相當笨拙,所以這是一個例子,它可能是比它好。長期以來,人們一直致力於使這種事情變得更加容易(http://tm.tl/288),但在此之前可能還需要很長時間。
如果使用'urllib的任何具體問題'和一堆線程/ greenlets?或者這是一個普遍的「什麼是最好的解決方案」問題? – Carpetsmoker
@Carpetsmoker看來我問錯了問題。我已澄清說明: 「什麼解決方案允許我從上載內容中讀取塊,並開始將此塊流式傳輸到內部存儲器,因爲用戶將上傳整個文件。我知道的所有解決方案都等待整個內容,然後再對wsgi應用程序進行管理/ python web server「 – Dmitry