Skip to content

Instantly share code, notes, and snippets.

@mark-jay
Last active July 22, 2017 19:24
Show Gist options
  • Select an option

  • Save mark-jay/925f7d87c7447239f5cbb1ee485bb792 to your computer and use it in GitHub Desktop.

Select an option

Save mark-jay/925f7d87c7447239f5cbb1ee485bb792 to your computer and use it in GitHub Desktop.
Simple http server that provides camera pictures on request
### installation:
# sudo apt-get install fswebcam autossh
#
### test camera:
# fswebcam -r 640x480 -S 25 --jpeg 85 -D 1 /tmp/web-cam-shot.jpg && opn /tmp/web-cam-shot.jpg
#
### running:
# PORT=10007
# autossh -M 20000 -f -N <YOUR_VPS> -R 8080:localhost:"$PORT" -C
# python run-surveillance-server.py "$PORT" "/tmp/webcam.jpg"
### check
# check in browser http://YOUR_VPS:8080
#
import SimpleHTTPServer
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import sys
def load(file):
with open(file, 'rb') as file:
return file.read()
# credits go to:
# https://gist.github.com/fxsjy/5465353
class Handler(BaseHTTPRequestHandler):
''' Main class to present webpages and authentication. '''
def do_HEAD(self):
print "send header"
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_AUTHHEAD(self):
print "send header"
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"Test\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
''' Present frontpage with user authentication. '''
if self.headers.getheader('Authorization') == None:
self.do_AUTHHEAD()
self.wfile.write('no auth header received')
pass
elif self.headers.getheader('Authorization') == 'Basic dXNlcjpVbmdlc3NhYmxlUGFzc3dvcmQ=': # user:UngessablePassword
self.send_response(200)
self.send_header("Content-type", "image/jpeg")
self.end_headers()
import os
import datetime
file = sys.argv[2]
os.system("fswebcam -r 640x480 -S 25 --jpeg 85 -D 1 " + file)
self.wfile.write(load(file))
pass
else:
self.do_AUTHHEAD()
self.wfile.write(self.headers.getheader('Authorization'))
self.wfile.write('not authenticated')
pass
httpd = SocketServer.TCPServer(("", int(sys.argv[1])), Handler)
httpd.serve_forever()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment