【问题标题】:Python server for streaming request body content用于流式传输请求正文内容的 Python 服务器
【发布时间】:2014-02-09 00:18:09
【问题描述】:

我正在尝试创建 python 智能代理服务器,它应该能够将大型请求正文内容从客户端流式传输到一些内部存储(可能是 amazon s3、swift、ftp 或类似的东西)。在流式传输服务器之前,应该请求一些内部 API 服务器来确定上传到内部存储的参数。主要限制是它应该使用方法 PUT 在一个 HTTP 操作中完成。它也应该异步工作,因为会有很多文件上传。

什么解决方案允许我从上传内容中读取块并开始将这些块流式传输到内部存储,因为用户将上传整个文件?我知道的所有 python web 应用程序都会等待整个内容,然后再将管理权交给 wsgi 应用程序/python web 服务器。

我找到的解决方案之一是 tornado fork https://github.com/nephics/tornado 。但它是非官方的,tornado 开发人员并不急于将其包含在主分支中。 那么您可能知道我的问题的一些现有解决方案吗?龙卷风?扭曲?事件?

【问题讨论】:

  • 如果使用urllib 和一堆线程/greenlets 有什么具体问题?或者这是一个普遍的“什么是最好的解决方案”问题?
  • @Carpetsmoker 看来我问错了问题。我已经澄清了描述:“什么解决方案允许我从上传内容中读取块并开始将这些块流式传输到内部存储,因为用户将上传整个文件。我知道的所有解决方案都会等待整个内容,然后再对 wsgi 应用程序进行管理/ python网络服务器"

标签: python asynchronous twisted tornado gevent


【解决方案1】:

这是一个使用 Twisted 编写的进行流式上传处理的服务器示例:

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),但可能还需要很长时间才能完成。

【讨论】:

  • 谢谢!我注意到这个解决方案是扭曲的。如您所愿,您可能会在下面看到我自己的 gevents 解决方案
  • 感谢分享您的解决方案!
【解决方案2】:

看来我有一个使用 gevent 库和猴子补丁的解决方案:

from gevent.monkey import patch_all
patch_all()
from gevent.pywsgi import WSGIServer


def stream_to_internal_storage(data):
    pass


def simple_app(environ, start_response):
    bytes_to_read = 1024

    while True:
        readbuffer = environ["wsgi.input"].read(bytes_to_read)
        if not len(readbuffer) > 0:
            break
        stream_to_internal_storage(readbuffer)

    start_response("200 OK", [("Content-type", "text/html")])
    return ["hello world"]


def run():
    config = {'host': '127.0.0.1', 'port': 45000}

    server = WSGIServer((config['host'], config['port']), application=simple_app)
    server.serve_forever()


if __name__ == '__main__':
    run()

当我尝试上传大文件时效果很好:

curl -i -X PUT --progress-bar --verbose --data-binary @/path/to/huge/file "http://127.0.0.1:45000"

【讨论】:

    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多