Python BaseHTTPServer和Tornado [英] Python BaseHTTPServer and Tornado

查看:148
本文介绍了Python BaseHTTPServer和Tornado的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行通过ThreadedHTTPServer传递的BaseHTTPServer,这样我就可以进行线程化了.

I'm running a BaseHTTPServer, passed through ThreadedHTTPServer so I get threading.

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

接下来,我根据此处的信息进行分叉:守护Python的BaseHTTPServer

Next I fork according to the info here: Daemonizing python's BaseHTTPServer

然后我这样做:

server.serve_forever()

我想做的是让相同的Python脚本也运行Tornado WebSocket服务器,我尝试创建第二个处理程序,并在我的主目录中创建与上面类似的第二个服务器,但是然后serve_forever()块(我假设),并且我无法启动Tornado WebSocket服务器.

What I am trying to do is have the same Python script run a Tornado WebSocket server as well, I tried creating the second handler and in my main creating the second server similar to above, but then the serve_forever() blocks (I assume) and I can't start the Tornado WebSocket server.

我曾经考虑过使用Tornado来提供一般的Web服务,但性能太差且无法使用,因此我宁愿将其与其他产品一起运行,除非有将WebSockets添加到BaseHTTPServer的更简单的选择.

I had considered using Tornado to serve my general web stuff too but performance was aweful and unusable, so I'd prefer to run it alongside, unless there is a simpler alternative to adding WebSockets to the BaseHTTPServer.

任何人都可以提供解决方案吗?

Can anyone offer a solution please?

推荐答案

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

Yes, serve_forever() blocks it all. You can use handle_request to serve one request at a time. To assure it won't block you have to set timeout. To run it periodically you can use tornado.ioloop.PeriodicCallback. Example:

#!/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/

I used here timeout attribute to set timeout. I'm not sure if it's proper way to do it. Other method: http://code.activestate.com/recipes/499376/

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

Another solution: running every server in its own thread:

#!/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()

这篇关于Python BaseHTTPServer和Tornado的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆