Search notes:

Python WSGI: Serve images

The WSGI protocol allows to serve binary content such as images simply bu returning [open('filename', 'rb').read()]:
def application(environ, start_response):

    if environ['PATH_INFO'] == '/file.html':

       start_response(
          '200 OK',
          [('Content-Type', 'text/html; charset=utf-8')]
       )

       return [
         f'<html><body>'       .encode('utf-8'),
         f'<h1>Image</h1>'     .encode('utf-8'),
         f'<img src="img.png">'.encode('utf-8'),
         f'</body></html>'     .encode('utf-8')
       ]

    elif environ['PATH_INFO'] == '/img.png':

       start_response(
          '200 OK',
          [('Content-Type', 'image/png')]
       )

       return [open('img.png', 'rb').read()]

    else:

       start_response(
          '200 OK',
          [('Content-Type', 'text/html; charset=utf-8')]
       )

       return [
         f'<h1>404</h1>'                     .encode('utf-8'),
         f'{environ["PATH_INFO"]} not found.'.encode('utf-8')
       ]

Links

This Stackoverflow answer was helpful for me.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758199276, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/PEP/3333/serve-image(72): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78