[英]How do I kill SimpleHTTPServer from within a Python script?
I am trying to use http.server
to test all the links in a Python project.我正在尝试使用
http.server
来测试 Python 项目中的所有链接。 I can get my script to work if I start the server before running my script, and then the server stops when I close the terminal window.如果我在运行我的脚本之前启动服务器,我可以让我的脚本工作,然后当我关闭终端窗口时服务器停止。 But I'd really like the script itself to start and stop the server.
但我真的很喜欢脚本本身来启动和停止服务器。
I made a test script to simply start the server, get a page and prove the server is running, and then stop the server.我做了一个测试脚本来简单地启动服务器,获取一个页面并证明服务器正在运行,然后停止服务器。 I can't seem to get the pid of the server.
我似乎无法获得服务器的 pid。 When I try to kill the pid that this script reports after the script runs, I get a message that there is no such process;
当我在脚本运行后试图杀死这个脚本报告的pid时,我收到一条消息,说没有这样的进程; but the server is still running.
但服务器仍在运行。
How do I get the correct pid for the server, or more generally how do I stop the server from the script?如何获得服务器的正确 pid,或者更一般地说,我如何从脚本中停止服务器?
import os
import requests
from time import sleep
# Start server, in background.
print("Starting server...")
os.system('python -m http.server &')
# Make sure server has a chance to start before making request.
sleep(1)
print "Server pid: "
os.system('echo $$')
url = 'http://localhost:8000/index.html'
print("Testing request: ", url)
r = requests.get(url)
print("Status code: ", r.status_code)
Here is what I am doing:这是我在做什么:
import threading
try:
from http.server import HTTPServer, SimpleHTTPRequestHandler # Python 3
except ImportError:
from SimpleHTTPServer import BaseHTTPServer
HTTPServer = BaseHTTPServer.HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler # Python 2
server = HTTPServer(('localhost', 0), SimpleHTTPRequestHandler)
thread = threading.Thread(target = server.serve_forever)
thread.daemon = True
thread.start()
def fin():
server.shutdown()
print('server running on port {}'.format(server.server_port))
# here is your program
If you call fin
in your program, then the server shuts down.如果您在程序中调用
fin
,则服务器将关闭。
A slight modification to User's code above:对上面用户的代码稍作修改:
import threading
try:
from http.server import HTTPServer, BaseHTTPRequestHandler # Python 3
except ImportError:
import SimpleHTTPServer
from BaseHTTPServer import HTTPServer # Python 2
from SimpleHTTPServer import SimpleHTTPRequestHandler as BaseHTTPRequestHandler
server = HTTPServer(('localhost', 0), BaseHTTPRequestHandler)
thread = threading.Thread(target = server.serve_forever)
thread.deamon = True
def up():
thread.start()
print('starting server on port {}'.format(server.server_port))
def down():
server.shutdown()
print('stopping server on port {}'.format(server.server_port))
This is a closure solution to the problem.这是问题的闭包解决方案。 Works on python 3.
适用于python 3。
import os
import threading
import webbrowser
from http.server import HTTPServer, SimpleHTTPRequestHandler
def simple_http_server(host='localhost', port=4001, path='.'):
server = HTTPServer((host, port), SimpleHTTPRequestHandler)
thread = threading.Thread(target=server.serve_forever)
thread.deamon = True
cwd = os.getcwd()
def start():
os.chdir(path)
thread.start()
webbrowser.open_new_tab('http://{}:{}'.format(host, port))
print('starting server on port {}'.format(server.server_port))
def stop():
os.chdir(cwd)
server.shutdown()
server.socket.close()
print('stopping server on port {}'.format(server.server_port))
return start, stop
simple_http_server
which will return start
and stop
functions simple_http_server
将返回start
和stop
函数
>>> start, stop = simple_http_server(port=4005, path='/path/to/folder')
which you can use as您可以将其用作
>>> start()
starting server on port 4005
127.0.0.1 - - [14/Aug/2016 17:49:31] "GET / HTTP/1.1" 200 -
>>> stop()
stopping server on port 4005
I got this to run, but I'm curious to hear how this compares to User's answer above.我让它运行,但我很想知道这与上面用户的答案相比如何。 I came up with this after looking at the accepted answer here .
在查看此处接受的答案后,我想出了这个。
import subprocess
import requests
import os
import signal
from time import sleep
print "Starting server..."
cmd = 'python -m SimpleHTTPServer'
pro = subprocess.Popen(cmd, shell=True, preexec_fn=os.setsid)
# Make sure server has a chance to start before making request.
sleep(1)
url = 'http://localhost:8000/index.html'
print "Testing request: ", url
r = requests.get(url)
print "Status code: ", r.status_code
os.killpg(pro.pid, signal.SIGTERM)
Here's a context-flavored version which I prefer because it cleans up automatically and you can specify the directory to serve:这是我更喜欢的上下文风格版本,因为它会自动清理并且您可以指定要提供的目录:
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)
My solution with browser opening:我打开浏览器的解决方案:
File: http.py文件:http.py
import SimpleHTTPServer
import SocketServer
import threading
import webbrowser
import platform
from socket import SOL_SOCKET,SO_REUSEADDR
class HTTPServer():
def __init__(self,port=8000,url='http://localhost'):
self.port = port
self.thread = None
self.httpd = None
self.run = False
self.url = url
os = platform.system()
if os=='Linux':
self.browser_path = "/usr/bin/google-chrome %s"
elif os == 'Windows':
self.browser_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
else:
print("Chrome not found!")
def start(self):
self.run = True
self.httpd = SocketServer.TCPServer(("", self.port), SimpleHTTPServer.SimpleHTTPRequestHandler)
self.httpd.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.thread = threading.Thread(target = self._serve)
self.thread.start()
webbrowser.get(str(self.browser_path)).open(self.url+":"+str(self.port)+"/")
def _serve(self):
while self.run:
self.httpd.handle_request()
def stop(self):
self.run = False
self.httpd.server_close()
After, just run:之后,只需运行:
from http import HTTPServer
server = HTTPServer()
server.start()
raw_input("Enter to close")
server.stop()
In my opinion, what I did is:在我看来,我所做的是:
(only if you ran the cmd/terminal command in python, because I think the way to stop it in cmd/terminal is ctrl+c or just quit the cmd/terminal) (仅当您在python中运行cmd/terminal命令时,因为我认为在cmd/terminal中停止它的方法是ctrl+c或直接退出cmd/terminal)
note: the pid is also there注意:pid也在那里
Hope this helps :D!!希望这会有所帮助:D!!
Using Python 3.8使用 Python 3.8
import http.server
import socketserver
import threading
PORT = 8000
Handler = http.server.SimpleHTTPRequestHandler
server = socketserver.TCPServer(("", PORT), Handler)
thread = threading.Thread(target = server.serve_forever)
thread.daemon = True
thread.start()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.