Guest User

Untitled

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