from http.server import BaseHTTPRequestHandler, HTTPServer
class S(BaseHTTPRequestHandler):
def do_GET(self):
#path = os.path.join(os.getcwd(), self.path) --> Not work !
with open(self.path, 'r', encoding='utf8') as File:
content = File.read()
def run(server_class=HTTPServer, handler_class=S, port=8085):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print ('Starting httpd...')
httpd.serve_forever()
if __name__ == "__main__":
run()
您好,我正在尝试使用 BaseHTTPRequestHandler 和本地 HTTP 服务器来操作文件。我无法获得绝对路径,真的很奇怪。我正在使用os.path.join
with os.getcwd
,它总是会返回这种目录:c:\\path.ext
而不是c:\\user\\name\\blabla\\path.ext
我在 windows 上工作。
希望有人可以提供帮助,似乎服务器目录始终位于“C:”的基本根目录中。
谢谢