Creating dynamic images with WSGI, no files involved

后端 未结 4 1826

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.

    <img src="url_to_my_application">
    

    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.

    0 讨论(0)
  • 2021-01-21 06:23

    It is not related to WSGI or php or any other specific web technology. consider

    <img src="someScript.php?param1=xyz">
    

    in general for url someScript.php?param1=xyz server should return data of image type and it would work

    Consider this example:

    from wsgiref.simple_server import make_server
    
    def serveImage(environ, start_response):
        status = '200 OK'
        headers = [('Content-type', 'image/png')]
        start_response(status, headers)
    
        return open("about.png", "rb").read()
    
    httpd = make_server('', 8000, serveImage)
    httpd.serve_forever()
    

    here any url pointing to serveImage will return a valid image and you can use it in any img tag or any other tag place where a image can be used e.g. css or background images

    Image data can be generated on the fly using many third party libraries e.g. PIL etc e.g see examples of generating images dynamically using python imaging library http://lost-theory.org/python/dynamicimg.html

    0 讨论(0)
  • 2021-01-21 06:24

    For a fancy example that uses this technique, please see the BNF railroad diagram WHIFF mini-demo. You can get the source from the WHIFF wsgi toolkit download.

    0 讨论(0)
  • 2021-01-21 06:30

    You should consider using and paying attention to ETag headers. It's a CGI script, not WSGI, but the ideas are translatable: sparklines source -- it happens to always return the same image for the same parameters, so it practices extreme caching.

    0 讨论(0)
提交回复
热议问题