Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os
- import time
- import urllib2
- import BaseHTTPServer
- import numpy, Image, StringIO
- if not os.path.exists('jquery.min.js'):
- response = urllib2.urlopen("http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js")
- open('jquery.min.js','w').write(response.read())
- print 'need download'
- class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
- def do_HEAD(s):
- s.send_response(200)
- s.send_header("Content-type", "text/html")
- s.end_headers()
- def getIndex(s):
- s.send_response(200)
- s.send_header("Content-type", "text/html")
- s.end_headers()
- s.wfile.write('''<html><head><title>Title goes here.</title></head>
- <body style="margin:0; padding:0">
- <div id = "video" style = "width:100%; height:100%">
- <img style = "width:100%; height:100%"/>
- </div>
- <script src="jquery.min.js"></script>
- <script type="text/javascript">
- setInterval(function(){
- $('#video > img').attr('src', 'jpg?random=' + Math.random())
- }, 300);
- </script>
- </body></html>
- ''')
- def getJQuery(s):
- s.send_response(200)
- s.send_header("Content-type", "application/x-javascript")
- s.end_headers()
- s.wfile.write(open('jquery.min.js','r').read())
- def getJpg(s):
- s.send_response(200)
- s.end_headers()
- a = numpy.random.rand(720,1280,3) * 255
- image = Image.fromarray(a.astype('uint8')).convert('RGBA')
- f = StringIO.StringIO()
- image.save(f, "JPEG")
- s.wfile.write(f.getvalue())
- getDict = {
- '/': getIndex,
- '/jquery.min.js': getJQuery,
- }
- def do_GET(s):
- if s.path in s.getDict:
- s.getDict[s.path](s)
- elif '/jpg?random=' in s.path:
- s.getJpg()
- else:
- print 's.path: >>'+s.path+'<<'
- HOST_NAME = '127.0.0.1'
- PORT_NUMBER = 8000
- if __name__ == '__main__':
- server_class = BaseHTTPServer.HTTPServer
- httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
- print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
- try:
- httpd.serve_forever()
- except KeyboardInterrupt:
- pass
- httpd.server_close()
- print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement