1

I have seen simple wsgi applications that display either Hello World or a PNG image (but not both) on a web page.

The first page uses 'Content-type', 'text/html; charset=utf-8' and the second uses 'content-type', 'image/png'.

How do I write a simple application (using say wsgiref.simple_server.make_server) that sends BOTH text/html and image/png on the SAME web page?

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150

2 Answers2

0

Basically, if I understand it all correctly, you can not. Your HTML code will need to have an <img src="path/url/to/image.png"> in it and that path will need to be either served as a static image or be a second request to the same WSGI server that will give you a png raw with a proper Content-type.

So, let me elaborate.

Say, you have a request path of http://server.com/mypage that will return you an HTML with Content-type set to text/html and within that HTML you will have:

<img src="http://server.com/myimage">

Then, in your WSGI app you implement two routes:

  1. /mypage that gives you an HTML back
  2. /myimage that gives you back a PNG image
favoretti
  • 29,299
  • 4
  • 48
  • 61
  • Just to play devil's advocate for a second... while you can't have a web page that has both text/html and image/png on the same web page, you could send a text/html response that has an inline css style that has a base64 encoded representation of a PNG within the HTML using the Data URI Scheme. – Jonathan Vanasco Oct 23 '12 at 01:52
  • Oh my :) I suppose that's a very creative implementation that will work, I wouldn't think of that off-hand. But I'm not sure anyone would want that as a standard implementation and not as an academic exercise . – favoretti Oct 23 '12 at 01:56
  • Actually, it's very useful in production. Many people i know have build/deploy scripts that stick smaller, often used, images into the CSS using Data URI to streamline content delivery and caching. It can save a few requests that wouldn't necessarily be on the same server to benefit from a keepalive connection, and add up to additional billing on CDNs. BUT... It's totally an academic hack in the context of this use case scenario. – Jonathan Vanasco Oct 23 '12 at 02:05
  • I stand corrected. Thanks for the information, didn't know that! – favoretti Oct 23 '12 at 02:06
  • Look into it! It can be a great strategy. Here's a good primer of positives - http://stackoverflow.com/questions/6819314/why-use-data-uri-scheme – Jonathan Vanasco Oct 23 '12 at 02:23
  • Thank you all, I need a little bit more study and time to understand these suggestions. – user1766964 Oct 23 '12 at 04:06
0
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server

def simple_app(environ, start_response):
  setup_testing_defaults(environ)
  path    = str( environ['PATH_INFO']
  headers = [('Server', 'Apache'),('Content-type', 'text/html')]
  rsp     = 'oops'

  if '.html' in path:
  rsp  = some_html

  if '.png' in path:
     headers = [('Server', 'Apache'),('Content-type', 'image/png')]
     rsp = some_png

  start_response(status, headers)
  return rsp

httpd = make_server('', 8008, simple_app)
print "Serving on port 8000..."

httpd.serve_forever()

  • 3
    Please note that the question specifically says `How to write` and not `provide me with a` so this is not very helpful. Please add more description about _why_ or _how_ it works instead of just providing it. – Tadhg McDonald-Jensen Mar 25 '16 at 06:03