Creating dynamic images with WSGI, no files involved

后端 未结 4 1859

I would like to send dynamically created images to my users, such as charts, graphs etc. These images are \"throw-away\" images, they will be only sent to one user and then dest

4条回答
  •  执念已碎
    2021-01-21 06:21

    YES. It is as simple as putting the url in the page.

    
    

    And your application just have to return it with the correct mimetype, just like on PHP or anything else. Simplest example possible:

    def application(environ, start_response):
        data = open('test.jpg', 'rb').read() # simulate entire image on memory
        start_response('200 OK', [('content-type': 'image/jpeg'), 
                                  ('content-length', str(len(data)))])
        return [data]
    

    Of course, if you use a framework/helper library, it might have helper functions that will make it easier for you.

    I'd like to add as a side comment that multi-threading capabilities are not quintessential on a web server. If correctly done, you don't need threads to have a good performance.

    If you have a well-developed event loop that switches between different requests, and write your request-handling code in a threadless-friendly manner (by returning control to the server as often as possible), you can get even better performance than using threads, since they don't make anything run faster and add overhead.

    See twisted.web for a good python web server implementation that doesn't use threads.

提交回复
热议问题