Python BaseHTTPServer and Tornado



我正在运行一个BaseHTTPServer,通过threaddhttpserver,所以我得到线程。

server = ThreadedHTTPServer(('', int(port)), MainHandler)

下一步我根据这里的信息分叉:Daemonizing python's BaseHTTPServer

然后我做:

server.serve_forever()

我想做的是让同样的Python脚本运行龙卷风WebSocket服务器,我尝试创建第二个处理程序,并在我的主创建类似于上面的第二个服务器,但然后serve_forever()块(我假设),我不能启动龙卷风WebSocket服务器。

我曾考虑使用龙卷风来服务我的一般web的东西,但性能是可怕的和不可用的,所以我宁愿运行它旁边,除非有一个更简单的替代方案,以添加WebSockets到BaseHTTPServer。

谁能提供一个解决方案?

是的,serve_forever()阻止了这一切。您可以使用handle_request一次处理一个请求。为了确保它不会阻塞,你必须设置超时。要定期运行,可以使用tornado.ioloop.PeriodicCallback。例子:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class Handler(BaseHTTPRequestHandler):  
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        message =  threading.currentThread().getName()
        self.wfile.write(message)
        self.wfile.write('n')
        return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""
class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        greeting = self.get_argument('greeting', 'Hello')
        self.write(greeting + ', friendly user!n')
if __name__ == '__main__':
    # create Tornado Server
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    # create BaseHTTPServer
    server = ThreadedHTTPServer(('localhost', 8080), Handler)
    server.timeout = 0.01 
    tornado.ioloop.PeriodicCallback(server.handle_request, 100).start() # every 100 milliseconds
    tornado.ioloop.IOLoop.instance().start()
运行:

$ curl http://localhost:8080/
Thread-1
$ curl http://localhost:8080/
Thread-2
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8080/
Thread-3
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8080/
Thread-4
$ curl http://localhost:8000/
Hello, friendly user!
$ curl http://localhost:8000/
Hello, friendly user!

我在这里使用timeout属性来设置超时。我不确定这样做是否合适。其他方法:http://code.activestate.com/recipes/499376/

另一个解决方案:让每个服务器在自己的线程中运行:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)
class Handler(BaseHTTPRequestHandler):  
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        message =  threading.currentThread().getName()
        self.wfile.write(message)
        self.wfile.write('n')
        return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
    """Handle requests in a separate thread."""
class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        greeting = self.get_argument('greeting', 'Hello')
        self.write(greeting + ', friendly user!n')
def run_tornado():
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
def run_base_http_server():
    server = ThreadedHTTPServer(('localhost', 8080), Handler)
    server.serve_forever()
if __name__ == '__main__':
    threading.Thread(target=run_tornado).start()
    threading.Thread(target=run_base_http_server).start()

相关内容

  • 没有找到相关文章