Python SimpleHTTPRequestHandler 无法将 yaml 作为内容类型进行服务器。
我尝试运行 python Web 服务器作为 helm 图表存储库。我需要将 yaml 文件设置为内容类型(text/yaml 或 text/x-yaml)。
这是我的代码:
import http.server
import socketserver
PORT = 8000
DIRECTORY = "web"
class Handler(http.server.SimpleHTTPRequestHandler):
extensions_map={
'.yaml': 'text/x-yaml',
'.manifest': 'text/cache-manifest',
'.html': 'text/html',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.js': 'application/x-javascript',
'': 'application/octet-stream', # Default
}
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
self.extensions_map={
'.yaml': 'text/x-yaml',
'.manifest': 'text/cache-manifest',
'.html': 'text/html',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.js': 'application/x-javascript',
'': 'application/octet-stream', # Default
}
Handler.extensions_map={
'.yaml': 'text/x-yaml',
'.manifest': 'text/cache-manifest',
'.html': 'text/html',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.js': 'application/x-javascript',
'': 'application/octet-stream', # Default
}
# h=Handler
# print(h.extensions_map)
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("serving at port", PORT)
httpd.serve_forever()
当我在网站中单击yaml文件时,网络服务器不显示其内容,但显示下载选项。
浏览器有缓存。如果我使用新端口,浏览器会直接显示内容。
import http.server
import socketserver
PORT = 8001
DIRECTORY = "web"
class YamlHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
self.extensions_map={'.yaml': 'text/x-yaml',}
super().__init__(*args, directory=DIRECTORY, **kwargs)
YamlHandler.extensions_map={
'.yaml': 'text/x-yaml',
'.manifest': 'text/cache-manifest',
'.html': 'text/html',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.svg': 'image/svg+xml',
'.css': 'text/css',
'.js': 'application/x-javascript',
'': 'application/octet-stream', # Default
}
with socketserver.TCPServer(("", PORT), YamlHandler) as httpd:
print("serving at port", PORT)
httpd.allow_reuse_address=1
httpd.serve_forever()