m3u8 proxy for shortcut.lv streams

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. '''
  2. WSGI-compliant HTTP server. Dispatches requests to a pool of threads.
  3. https://github.com/RonRothman/mtwsgi
  4. '''
  5. from wsgiref.simple_server import WSGIServer, WSGIRequestHandler
  6. import multiprocessing.pool
  7. __all__ = ['ThreadPoolWSGIServer', 'make_server']
  8. import bottle
  9. class ThreadPoolWSGIServer(WSGIServer):
  10. '''WSGI-compliant HTTP server. Dispatches requests to a pool of threads.'''
  11. def __init__(self, thread_count=None, *args, **kwargs):
  12. '''If 'thread_count' == None, we'll use multiprocessing.cpu_count() threads.'''
  13. WSGIServer.__init__(self, *args, **kwargs)
  14. self.thread_count = thread_count
  15. self.pool = multiprocessing.pool.ThreadPool(self.thread_count)
  16. # Inspired by SocketServer.ThreadingMixIn.
  17. def process_request_thread(self, request, client_address):
  18. try:
  19. self.finish_request(request, client_address)
  20. self.shutdown_request(request)
  21. except:
  22. self.handle_error(request, client_address)
  23. self.shutdown_request(request)
  24. def process_request(self, request, client_address):
  25. self.pool.apply_async(self.process_request_thread, args=(request, client_address))
  26. def make_server(host, port, app, thread_count=None, handler_class=WSGIRequestHandler):
  27. '''Create a new WSGI server listening on `host` and `port` for `app`'''
  28. httpd = ThreadPoolWSGIServer(thread_count, (host, port), handler_class)
  29. httpd.set_app(app)
  30. return httpd
  31. class MTServer(bottle.ServerAdapter):
  32. def run(self, handler):
  33. thread_count = self.options.pop('thread_count', None)
  34. server = make_server(self.host, self.port, handler, thread_count, **self.options)
  35. try:
  36. server.serve_forever()
  37. except KeyboardInterrupt:
  38. server.server_close() # Prevent ResourceWarning: unclosed socket
  39. raise
  40. if __name__ == '__main__':
  41. from wsgiref.simple_server import demo_app
  42. httpd = make_server('', 8000, demo_app)
  43. sa = httpd.socket.getsockname()
  44. print "Serving HTTP on", sa[0], "port", sa[1], "..."
  45. import webbrowser
  46. webbrowser.open('http://localhost:8000/xyz?abc')
  47. httpd.serve_forever()