本文介绍了如何关闭python服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码运行python服务器:

导入操作系统从 http.server 导入 SimpleHTTPRequestHandler, HTTPServeros.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')server_address = ('', 8000)httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)httpd.serve_forever()

如何让它停止?

解决方案

您的问题不明确 - 如果您通过 shell 运行服务器,即 python myscript.py,只需按 crtl + C.>

如果你想用代码优雅地关闭它,你必须决定某些条件、点或异常来调用它关闭.您可以添加一个块并调用 httpd.shutdown() - 作为 HttpServer 本身就是一个 SocketServer.TCPSServer 子类:

第一个类 HTTPServer 是一个 SocketServer.TCPServer 子类,并且因此实现了 SocketServer.BaseServer 接口.它创建并侦听 HTTP 套接字,将请求分派给处理程序.

所以 BaseServer 有一个方法 shutdown(),因此作为 HttpServer 的子类也有.

例如:

导入操作系统从 http.server 导入 SimpleHTTPRequestHandler, HTTPServeros.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')server_address = ('', 8000)尝试:httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)httpd.serve_forever()除了例外:httpd.shutdown()

有用的相关问题 -

Used this code to run a python server:

import os
from http.server import SimpleHTTPRequestHandler, HTTPServer                                                                                                                                   

os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')                                                                                                                                                                                      
server_address = ('', 8000)                                                                                                                                                                    
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)                                                                                                                                   
httpd.serve_forever()

How to make it stop?

解决方案

Your question is ambiguous - if your running the server via shell i.e. python myscript.py, simply press crtl + C.

If you want to close it elegantly using code, you must decide on some condition, or point, or exception to call it shutdown. You can add a block and call httpd.shutdown() - as HttpServer itself is a SocketServer.TCPSServer subclass:

So the BaseServer has a method shutdown(), hence being a subclass HttpServer has it too.

for example:

import os
from http.server import SimpleHTTPRequestHandler, HTTPServer                                                                                                                                   

os.chdir('c:/users/owner/desktop/tom/tomsEnyo2.5-May27')                                                                                                                                                                                      
server_address = ('', 8000)   
try:
    httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)                                                                                                                                   
    httpd.serve_forever()
except Exception:
    httpd.shutdown()


Helpful relevant question -

这篇关于如何关闭python服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 08:07