Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #Licence: GPLv3.0
- #Copyright: 2017 Krasimir S. Stefanov (lokster)
- #Website: http://lokspace.eu
- import SocketServer
- from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- import urllib2
- import base64
- import os
- import time
- MJS_AUTH_USER = "the user for your mjpg_streamer stream"
- MJS_AUTH_PASS = "the password for your mjpg_streamer stream"
- MJS_SNAPSHOT_ADDRESS = "the mjpg_streamer snapshot url"
- AUTH_USER = "the user for accessing this snapshot"
- AUTH_PASS = "the password for accessing this snapshot"
- PORT_NUMBER = 8083
- os.chdir(os.path.dirname(os.path.abspath(__file__)))
- authString = "Basic %s" % base64.b64encode('%s:%s' % (AUTH_USER, AUTH_PASS))
- 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', 'image/jpeg')
- #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=\"Snapshot Proxy\"')
- 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') == authString:
- self.do_HEAD()
- if (MJS_AUTH_USER and MJS_AUTH_PASS):
- os.system('wget -O- "%s" --user="%s" --password="%s" 2>/dev/null | convert - /tmp/snapshot.jpg 2>/dev/null' % (MJS_SNAPSHOT_ADDRESS, MJS_AUTH_USER, MJS_AUTH_PASS))
- else:
- os.system('wget -O- "%s" 2>/dev/null | convert - /tmp/snapshot.jpg 2>/dev/null' % SNAPSHOT_ADDRESS)
- self.wfile.write(open('/tmp/snapshot.jpg', 'r').read())
- os.system('rm /tmp/snapshot.jpg')
- pass
- else:
- self.do_AUTHHEAD()
- self.wfile.write(self.headers.getheader('Authorization'))
- self.wfile.write('not authenticated')
- pass
- try:
- #Create a web server and define the handler to manage the
- #incoming request
- server = HTTPServer(('', PORT_NUMBER), Handler)
- print 'Started httpserver on port ' , PORT_NUMBER
- #Wait forever for incoming htto requests
- server.serve_forever()
- except KeyboardInterrupt:
- print '^C received, shutting down the web server'
- server.socket.close()
Add Comment
Please, Sign In to add comment