Advertisement
Guest User

Untitled

a guest
Jul 14th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import time
  5. import urllib2
  6. import BaseHTTPServer
  7. import numpy, Image, StringIO
  8.  
  9. if not os.path.exists('jquery.min.js'):
  10.     response = urllib2.urlopen("http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js")
  11.     open('jquery.min.js','w').write(response.read())
  12.     print 'need download'
  13.  
  14. class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  15.     def do_HEAD(s):
  16.         s.send_response(200)
  17.         s.send_header("Content-type", "text/html")
  18.         s.end_headers()
  19.  
  20.     def getIndex(s):
  21.         s.send_response(200)
  22.         s.send_header("Content-type", "text/html")
  23.         s.end_headers()
  24.         s.wfile.write('''<html><head><title>Title goes here.</title></head>
  25. <body style="margin:0; padding:0">
  26. <div id = "video" style = "width:100%; height:100%">
  27.    <img style = "width:100%; height:100%"/>
  28. </div>
  29. <script src="jquery.min.js"></script>
  30. <script type="text/javascript">
  31.    setInterval(function(){
  32.        $('#video > img').attr('src', 'jpg?random=' + Math.random())
  33.    }, 300);
  34. </script>
  35. </body></html>
  36. ''')
  37.     def getJQuery(s):
  38.         s.send_response(200)
  39.         s.send_header("Content-type", "application/x-javascript")
  40.         s.end_headers()
  41.         s.wfile.write(open('jquery.min.js','r').read())
  42.  
  43.     def getJpg(s):
  44.         s.send_response(200)
  45.         s.end_headers()
  46.         a = numpy.random.rand(720,1280,3) * 255
  47.         image = Image.fromarray(a.astype('uint8')).convert('RGBA')
  48.         f = StringIO.StringIO()
  49.         image.save(f, "JPEG")
  50.         s.wfile.write(f.getvalue())
  51.  
  52.     getDict = {
  53.         '/': getIndex,
  54.         '/jquery.min.js': getJQuery,
  55.     }
  56.  
  57.     def do_GET(s):
  58.         if s.path in s.getDict:
  59.             s.getDict[s.path](s)
  60.         elif '/jpg?random=' in s.path:
  61.             s.getJpg()
  62.         else:
  63.             print 's.path: >>'+s.path+'<<'
  64.  
  65. HOST_NAME = '127.0.0.1'
  66. PORT_NUMBER = 8000
  67.  
  68. if __name__ == '__main__':
  69.     server_class = BaseHTTPServer.HTTPServer
  70.     httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
  71.     print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  72.     try:
  73.         httpd.serve_forever()
  74.     except KeyboardInterrupt:
  75.         pass
  76.     httpd.server_close()
  77.     print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement