Create dynamic images using WSGI, no files

I would like to send dynamically generated images to my users, such as charts, graphs, etc. These images are "discarded" images, they will be sent to only one user and then destroyed, therefore, "no files are used."

I would like to send the image directly to the user without first saving it to the file system. With PHP, this can be achieved by linking the image in your HTML files to a PHP script, for example:

edit: SO swallowed my image tag:

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

Then the script sent the correct headers (filetype => jpeg, etc.) to the browser and directly wrote the image back to the client, without temporarily saving it to the file system.

How can I do something like this using a WSGI application. I am currently using the internal Python SimpleWSGI server. I know that this server was mainly intended for demonstration purposes, and not for actual use, since it does not have multithreading capabilities, so please do not tell me this, I know about it, and at the moment it satisfies my requirements :)

Is it really that simple how to put the URL in image tags and process the WSGI request, or is there a better practice?

Does anyone have any experience with this and can give me some pointers (no 32bit, please)

Thanks,

Tom

+3
source share
4 answers

This does not apply to WSGI or php, or to any other specific web technology. Consider

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

generally for url someScript.php?param1=xyz server should return image type data and it will work

Consider the following 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 anywhere else in the tag where the image can be used, for example. css or background images

Image data can be generated on the fly using many third-party libraries, for example. PIL etc. For example, examples of dynamically generating images using the python image library http://lost-theory.org/python/dynamicimg.html

+8
source

YES. It is as simple as putting a URL on a page.

 <img src="url_to_my_application"> 

And your application just needs to return it with the correct mimetype type, like in PHP or something else. The simplest example:

 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 the framework / helper library, it may have helper functions that make your work easier.

I would like to add, as a side comment, that multithreading capabilities are not the quintessence of a web server. If done correctly, you do not need threads to have good performance.

If you have a well-developed event loop that switches between different requests, and write the request processing code in trouble-free mode (returning control to the server as often as possible), you can get even better performance than using threads, since they do nothing faster and add additional overhead.

See twisted.web for a good python web server implementation that does not use threads.

+2
source

For an example that uses this method, see the BNF Railway WHIFF mini-demo diagram . You can get the source code from the WHIFF wsgi toolkit download.

0
source

You should consider using and referring to ETag headers. This is a CGI script, not WSGI, but ideas can be translated: to spark the source - the same image always returns for the same parameters, therefore it uses extreme caching.

0
source

Source: https://habr.com/ru/post/905652/


All Articles