Saturday, January 10, 2015

Raspberry Pi CGI Python Serve JPEG Image Howto

Note that lighttpd doesn't care about the interpreter your specify in the shebang (#!/usr/bin...) You have to ensure (default is usually good enough, supports .py) cgi.assign in the /etc/lighttpd/lighttpd.conf has ".py" => "/path/to/python"..

That done, this will do it for you (you'll also have to add "mod_cgi" to your server.modules in the .conf, and put this one in the cgi-bin directory in /var/www if you're using lighttpd that is) :

#!/usr/bin/python
print "Content-Type: text/html"
print
print """\
<html>

<body>
<h2>Hello World!</h2>
"""
data_uri = open('/home/pi/Desktop/snapshot.jpg', 'rb').read().encode('base64').replace('\n','')
img_tag = '<img src="data:image/jpeg;base64,{0}">'.format(data_uri)
print(img_tag)
print """\
</body>
</html>
"""

No comments: