我正在尝试使 GET 方法显示网络服务器目录中的所有文件

I am trying to make the GET method to show all files in webserver directory

你好,我正在尝试添加一个 GET 函数,它可以显示我的网络服务器分配目录中的所有文件

我完全删除了 GET 函数,并认为它只是通过显示目录中的所有文件来默认该方法,但是服务器给我一个错误,说它不支持方法 GET

from http.server import HTTPServer, BaseHTTPRequestHandler

from io import BytesIO


class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    #def do_GET(self):
     #   self.send_response(200)
      #  self.end_headers()
       # self.wfile.write(b"Page is only for POST")

    def do_POST(self):
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b"POST")
        import json
        list_output = json.loads(body.decode("utf-8"))
        newdata = open(str(list_output[0]) + ".json", "w")
        newdata.write(body.decode("utf-8"))
        response.write(body)
        self.wfile.write(response.getvalue())
        print(body.decode("utf-8"))

httpd = HTTPServer(('192.168.1.107', 8079), SimpleHTTPRequestHandler)
httpd.serve_forever()

我得到的错误是

192.168.1.107 - - [02/Dec/2020 19:48:47] code 501, message Unsupported method ('GET')

实际上我希望它出现在网页中

我做到了!

目前它可以显示请求的文件。但这对我来说已经足够了!

from http.server import HTTPServer, BaseHTTPRequestHandler
import json
from io import BytesIO
import os
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        with open(str(self.path)[1:], 'rb') as file: 
            self.send_response(200)
            self.end_headers()
            self.wfile.write(file.read())
            print(self.path)

    def do_POST(self):  
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()
        response = BytesIO()
        response.write(b"POST")
        list_output = json.loads(body.decode("utf-8"))
        newdata = open(str(list_output[0]) + ".json", "w")
        newdata.write(body.decode("utf-8"))
        response.write(body)
        self.wfile.write(response.getvalue())
        print(body.decode("utf-8"))

httpd = HTTPServer(('127.0.0.1', 8079), SimpleHTTPRequestHandler)
httpd.serve_forever()