python - 在 Python 3 中提供目录

标签 python python-3.x server

我有这个基本的 python3 服务器,但不知道如何提供目录。

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
        def do_GET(self):
            print(self.path)
            if self.path == '/up':
                self.send_response(200)
                self.end_headers()
                self.wfile.write(b'Going Up')
            if self.path == '/down':
                self.send_response(200)
                self.end_headers()
                self.wfile.write(B'Going Down')

httpd = socketserver.TCPServer(("", PORT), SimpleHTTPRequestHandler)
print("Server started on ", PORT)
httpd.serve_forever()

如果不是上面的自定义类,我只是将 Handler = http.server.SimpleHTTPRequestHandler 传递给 TCPServer():,默认功能是提供一个目录,但我想提供那个目录并在我上面的两个 GET 上具有功能。

例如,如果有人要访问 localhost:8080/index.html,我希望将该文件提供给他们

最佳答案

如果您使用的是 3.7,您可以简单地提供一个存放 html 文件的目录,例如。 index.html 还在

python -m http.server 8080 --bind 127.0.0.1 --directory /path/to/dir

对于 docs

关于python - 在 Python 3 中提供目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55052811/

相关文章:

linux - 递归查找不可公开读取的文件

python - 在 python 2.7 中扩展类,使用 super()

python - 将每月数据分组到数据框中以显示行小计

python - 为什么即使不执行某些代码也会出现错误?

python - 合并未知字符串的匹配切片

java - 使用 java NIO 选择器发送文件

python - Tensorflow 无效参数 : Assertation Failed [Label IDs must < n_classes]

linux - 如何通过套接字将 `eof` 信号发送到在远程 shell 中运行的命令?

python - 在反向代理后面运行花

node.js - 为什么nodejs中app.js中console.log出现两次