57

HTTPServer在一个单独的线程中运行我的(使用无法停止线程的线程模块......)并且希望在主线程也关闭时停止服务请求。

Python 文档指出它BaseHTTPServer.HTTPServer是 的子类SocketServer.TCPServer,它支持一种shutdown方法,但在HTTPServer.

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

4

11 回答 11

29

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

def serve_forever(self):
    """Handle one request at a time until doomsday."""
    while 1:
        self.handle_request()

您可以用 替换(在子类中)while 1while self.should_be_running并从不同的线程修改该值。就像是:

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()
于 2008-11-06T13:21:35.237 回答
29

另一种基于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()
于 2013-10-06T17:32:17.843 回答
25

事件循环在 SIGTERM、Ctrl+C或何时shutdown()被调用时结束。

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

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()
于 2016-02-23T11:18:35.847 回答
24

在我的 python 2.6 安装中,我可以在底层 TCPServer 上调用它——它仍然在你的内部HTTPServer

TCPServer.shutdown


>>> import BaseHTTPServer
>>> h=BaseHTTPServer.HTTPServer(('',5555), BaseHTTPServer.BaseHTTPRequestHandler)
>>> h.shutdown
<bound method HTTPServer.shutdown of <BaseHTTPServer.HTTPServer instance at 0x0100D800>>
>>> 
于 2008-11-06T13:30:25.033 回答
15

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

于 2010-10-26T01:38:35.223 回答
9

在 python 2.7 中,调用 shutdown() 有效,但前提是您通过 serve_forever 提供服务,因为它使用异步选择和轮询循环。具有讽刺意味的是,使用 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")
于 2014-03-18T23:38:21.927 回答
2

我成功使用此方法(Python 3)从 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/(example http://localhost:8080/static/styles/common.css)提供的页面将由默认处理程序提供,http://localhost:8080/control/stop.html从服务器计算机访问将显示stop.html然后停止服务器,任何其他选项都将被禁止。

于 2018-06-07T20:23:46.437 回答
2

这是Helgi对 python 3.7 的回答的简化版本:

import threading
import time
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler


class MyServer(threading.Thread):
    def run(self):
        self.server = ThreadingHTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
        self.server.serve_forever()
    def stop(self):
        self.server.shutdown()


if __name__ == '__main__':
    s = MyServer()
    s.start()
    print('thread alive:', s.is_alive())  # True
    time.sleep(2)
    s.stop()
    print('thread alive:', s.is_alive())  # False
于 2020-10-20T03:42:11.087 回答
1

我尝试了上述所有可能的解决方案,最终遇到了“有时”的问题——不知何故,它并没有真正做到——所以我最终制定了一个对我来说一直有效的肮脏解决方案:

如果以上所有方法都失败了,那么使用以下方法蛮力杀死你的线程:

import subprocess
cmdkill = "kill $(ps aux|grep '<name of your thread> true'|grep -v 'grep'|awk '{print $2}') 2> /dev/null"
subprocess.Popen(cmdkill, stdout=subprocess.PIPE, shell=True)
于 2016-02-12T08:41:21.557 回答
1
import http.server
import socketserver
import socket as sck
import os
import threading


class myserver:
    def __init__(self, PORT, LOCATION):
        self.thrd = threading.Thread(None, self.run)
        self.Directory = LOCATION
        self.Port = PORT
        hostname = sck.gethostname()
        ip_address = sck.gethostbyname(hostname)
        self.url = 'http://' + ip_address + ':' + str(self.Port)
        Handler = http.server.SimpleHTTPRequestHandler
        self.httpd = socketserver.TCPServer(("", PORT), Handler)
        print('Object created, use the start() method to launch the server')
    def run(self):
        print('listening on: ' + self.url )
        os.chdir(self.Directory)
        print('myserver object started')        
        print('Use the objects stop() method to stop the server')
        self.httpd.serve_forever()
        print('Quit handling')

        print('Sever stopped')
        print('Port ' + str(self.Port) + ' should be available again.')


    def stop(self):
        print('Stopping server')
        self.httpd.shutdown()
        self.httpd.server_close()
        print('Need just one more request before shutting down'


    def start(self):
        self.thrd.start()

def help():
    helpmsg = '''Create a new server-object by initialising
NewServer = webserver3.myserver(Port_number, Directory_String)
Then start it using NewServer.start() function
Stop it using NewServer.stop()'''
    print(helpmsg)

不是有经验的python程序员,只是想分享一下我的综合解决方案。主要基于这里和那里的片段。我通常在我的控制台中导入这个脚本,它允许我使用它们的特定端口为不同的位置设置多个服务器,与网络上的其他设备共享我的内容。

于 2020-05-29T14:00:41.340 回答
0

这是 Python 3.7+ 的上下文风格版本,我更喜欢它,因为它会自动清理您可以指定要服务的目录:

from contextlib import contextmanager
from functools import partial
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
from threading import Thread


@contextmanager
def http_server(host: str, port: int, directory: str):
    server = ThreadingHTTPServer(
        (host, port), partial(SimpleHTTPRequestHandler, directory=directory)
    )
    server_thread = Thread(target=server.serve_forever, name="http_server")
    server_thread.start()

    try:
        yield
    finally:
        server.shutdown()
        server_thread.join()


def usage_example():
    import time

    with http_server("127.0.0.1", 8087, "."):
        # now you can use the web server
        time.sleep(100)
于 2021-05-06T06:47:13.187 回答