我正在尝试建立一个博客并维持下去,在此博客上,内容只会以html / css或某些图像形式出现,因此我不需要任何复杂的Web服务器,我试图编写一个脚本来保持博客活跃,但是有时它确实很频繁,它下降了,我尝试获取它的日志,起初它是关于broken pipe
的事情,但是我尝试使用signal handling
进行修复,但我没有不知道如何一直保持服务器正常运行。这是代码:
#!/usr/bin/python
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
from os import curdir, sep, path
from signal import signal, SIGPIPE, SIG_DFL
import logging
HOST = #ip_address
PORT = 8089
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
try:
print(self.path)
path = self.path
if ('?' in path):
path = path.split('?')[0]
self.path = path
print(self.path)
if (self.path == '/'):
self.path = '/index.html'
self.path = 'output' + self.path
if(os.path.isdir(self.path)):
if(not os.path.exists(self.path + 'index.html')):
self.send_response(403)
self.wfile.write(str.encode("Listing of directories not permited on this server"))
else:
self.path = self.path + 'index.html'
f = open(curdir + sep + self.path, 'rb')
self.wfile.write(f.read())
f.close()
except IOError:
print("File "+self.path+" not found")
self.send_response(404)
httpd = HTTPServer((HOST, PORT), SimpleHTTPRequestHandler)
httpd.serve_forever()
signal(SIGPIPE, SIG_DFL)
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
...
def handle(self):
try:
BaseHTTPRequestHandler.handle(self)
except socket.error:
pass
但是,由于Socket Server中存在一个错误,这也许可能是一个问题,这意味着它无法处理多个请求at the same time
此外,如果您希望HTTP服务器一次处理多个请求,则需要服务器类使用SocketServer.ForkingMixIn和SocketServer.ThreadingMixIn类之一。有关详细信息,请查看SocketServer模块的文档。这里是更具体的question,列出了有关BaseHTTPServer的大多数情况