python 结束 serve_forever_python-如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?...

python-如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?

我正在一个单独的线程中运行我的BaseHTTPServer(使用无法停止线程的线程模块...),并且想在主线程也关闭时停止提供请求。

Python文档指出BaseHTTPServer是SocketServer.TCPServer的子类,它支持shutdown方法,但在HTTPServer中缺少。

整个BaseHTTPServer模块只有很少的文档:(

8个解决方案

22 votes

我首先要说:“我自己可能不会做,但过去我会做。” serve_forever(来自SocketServer.py)方法如下所示:

def serve_forever(self):

"""Handle one request at a time until doomsday."""

while 1:

self.handle_request()

您可以用while self.should_be_running替换(在子类中)while 1,然后从其他线程修改该值。 就像是:

def stop_serving_forever(self):

"""Stop handling requests"""

self.should_be_running = 0

# Make a fake request to the server, to really force it to stop.

# Otherwise it will just stop on the next request.

# (Exercise for the reader.)

self.make_a_fake_request_to_myself()

编辑:我挖出了我当时使用的实际代码:

class StoppableRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):

stopped = False

allow_reuse_address = True

def __init__(self, *args, **kw):

SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, *args, **kw)

self.register_function(lambda: 'OK', 'ping')

def serve_forever(self):

while not self.stopped:

self.handle_request()

def force_stop(self):

self.server_close()

self.stopped = True

self.create_dummy_request()

def create_dummy_request(self):

server = xmlrpclib.Server('http://%s:%s' % self.server_address)

server.ping()

Ali Afshar answered 2020-08-11T07:21:09Z

21 votes

在我的python 2.6安装中,我可以在基础TCPServer上调用它-它仍然在HTTPServer内部:

TCPServer.shutdown

>>> import BaseHTTPServer

>>> h=BaseHTTPServer.HTTPServer(('',5555), BaseHTTPServer.BaseHTTPRequestHandler)

>>> h.shutdown

>

>>>

gimel answered 2020-08-11T07:21:31Z

16 votes

我认为您可以使用[serverName].socket.close()

wimerrill answered 2020-08-11T07:21:52Z

16 votes

基于[http://docs.python.org/2/library/basehttpserver.html#more-examples]的另一种实现方法是:只要满足条件,就继续提供服务,而不是serve_forever(), 服务器在每个请求之前和之后检查条件。 例如:

import CGIHTTPServer

import BaseHTTPServer

KEEP_RUNNING = True

def keep_running():

return KEEP_RUNNING

class Handler(CGIHTTPServer.CGIHTTPRequestHandler):

cgi_directories = ["/cgi-bin"]

httpd = BaseHTTPServer.HTTPServer(("", 8000), Handler)

while keep_running():

httpd.handle_request()

user2852263 answered 2020-08-11T07:22:13Z

13 votes

事件循环在SIGTERM,Ctrl + C或调用server_close()时结束。

必须在server_forever()之后调用server_close()以关闭监听套接字。

import http.server

class StoppableHTTPServer(http.server.HTTPServer):

def run(self):

try:

self.serve_forever()

except KeyboardInterrupt:

pass

finally:

# Clean-up server (close socket, etc.)

self.server_close()

可通过用户操作(SIGTERM,Ctrl + C等)停止简单的服务器:

server = StoppableHTTPServer(("127.0.0.1", 8080),

http.server.BaseHTTPRequestHandler)

server.run()

服务器在线程中运行:

import threading

server = StoppableHTTPServer(("127.0.0.1", 8080),

http.server.BaseHTTPRequestHandler)

# Start processing requests

thread = threading.Thread(None, server.run)

thread.start()

# ... do things ...

# Shutdown server

server.shutdown()

thread.join()

Vianney Bajart answered 2020-08-11T07:22:46Z

8 votes

在python 2.7中,仅当您通过serve_forever服务时才调用shutdown(),因为它使用了异步选择和轮询循环。 具有讽刺意味的是,使用handle_request()运行自己的循环会排除此功能,因为这意味着一个愚蠢的阻塞调用。

从SocketServer.py的BaseServer中:

def serve_forever(self, poll_interval=0.5):

"""Handle one request at a time until shutdown.

Polls for shutdown every poll_interval seconds. Ignores

self.timeout. If you need to do periodic tasks, do them in

another thread.

"""

self.__is_shut_down.clear()

try:

while not self.__shutdown_request:

# XXX: Consider using another file descriptor or

# connecting to the socket to wake this up instead of

# polling. Polling reduces our responsiveness to a

# shutdown request and wastes cpu at all other times.

r, w, e = select.select([self], [], [], poll_interval)

if self in r:

self._handle_request_noblock()

finally:

self.__shutdown_request = False

self.__is_shut_down.set()

这是我的代码的一部分,用于使用事件等待完成来阻止其他线程关闭:

class MockWebServerFixture(object):

def start_webserver(self):

"""

start the web server on a new thread

"""

self._webserver_died = threading.Event()

self._webserver_thread = threading.Thread(

target=self._run_webserver_thread)

self._webserver_thread.start()

def _run_webserver_thread(self):

self.webserver.serve_forever()

self._webserver_died.set()

def _kill_webserver(self):

if not self._webserver_thread:

return

self.webserver.shutdown()

# wait for thread to die for a bit, then give up raising an exception.

if not self._webserver_died.wait(5):

raise ValueError("couldn't kill webserver")

jsalter answered 2020-08-11T07:23:16Z

2 votes

我成功使用此方法(Python 3)从Web应用程序本身(Web页面)停止服务器:

import http.server

import os

import re

class PatientHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):

stop_server = False

base_directory = "/static/"

# A file to use as an "server stopped user information" page.

stop_command = "/control/stop.html"

def send_head(self):

self.path = os.path.normpath(self.path)

if self.path == PatientHTTPRequestHandler.stop_command and self.address_string() == "127.0.0.1":

# I wanted that only the local machine could stop the server.

PatientHTTPRequestHandler.stop_server = True

# Allow the stop page to be displayed.

return http.server.SimpleHTTPRequestHandler.send_head(self)

if self.path.startswith(PatientHTTPRequestHandler.base_directory):

return http.server.SimpleHTTPRequestHandler.send_head(self)

else:

return self.send_error(404, "Not allowed", "The path you requested is forbidden.")

if __name__ == "__main__":

httpd = http.server.HTTPServer(("127.0.0.1", 8080), PatientHTTPRequestHandler)

# A timeout is needed for server to check periodically for KeyboardInterrupt

httpd.timeout = 1

while not PatientHTTPRequestHandler.stop_server:

httpd.handle_request()

这样,默认处理程序将为通过基地址http://localhost:8080/static/(示例http://localhost:8080/static/styles/common.css)提供的页面提供服务,从服务器计算机访问http://localhost:8080/control/stop.html将显示stop.html,然后停止服务器,将禁止任何其他选项。

Vlad Tudorache answered 2020-08-11T07:23:42Z

1 votes

我尝试了上述所有可能的解决方案,最终遇到了“有时”问题-某种程度上它并没有真正做到-因此我最终做出了一个肮脏的解决方案,该解决方案一直对我有用:

如果以上所有方法均失败,则蛮力使用如下命令杀死线程:

import subprocess

cmdkill = "kill $(ps aux|grep ' true'|grep -v 'grep'|awk '{print $2}') 2> /dev/null"

subprocess.Popen(cmdkill, stdout=subprocess.PIPE, shell=True)

serup answered 2020-08-11T07:24:10Z

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值