最简单的临时web服务器
前言
有的时候需要做个单页的网页显示,这个直接用python启动即可,但是存在一个问题,停止进程以后,如果网页正在被访问,socket会不释放,然后再启动就会提示端口占用,实际上是没有端口占用的
这个增加一个配置项就行
相关代码
#!/usr/bin/python3
from __future__ import print_function
import SimpleHTTPServer
import SocketServer
import os
import sys
import signal
def quit(signal_num,frame):
print("you stop the threading")
sys.exit()
path = os.path.dirname(sys.argv[0])
os.chdir('/home/cephuse/output/')
class ReusingTCPServer(SimpleHTTPServer.SimpleHTTPRequestHandler):
allow_reuse_address = True
def send_head(self):
# horrible kludge because SimpleHTTPServer is buggy wrt
# slash-redirecting of requests with query arguments, and will
# redirect to /foo?q=bar/ -- wrong slash placement
self.path = self.path.split('?', 1)[0]
return SimpleHTTPServer.SimpleHTTPRequestHandler.send_head(self)
SocketServer.TCPServer.allow_reuse_address = True
httpd = SocketServer.TCPServer(
("", 9090),
ReusingTCPServer,
)
try:
print("Serving doc at port: http://localhost:9090")
httpd.serve_forever()
except KeyboardInterrupt:
pass
上面是参考的ceph doc的服务脚本,区别就是增加的那句
SocketServer.TCPServer.allow_reuse_address = True