【发布时间】:2017-08-02 01:58:57
【问题描述】:
考虑一个简单的 server.py
from http.server import CGIHTTPRequestHandler, HTTPServer
PORT = 5005
handler = CGIHTTPRequestHandler
handler.cgi_directories = ['/'] # this is the current directory
server = HTTPServer(('localhost', PORT), handler)
print ('Server is ready at', PORT, 'PORT')
server.serve_forever()
如果我在命令行中使用python.exe -u server.py 调用此文件,我将能够在'/' 主目录中提供python 脚本。
我也可以直接使用python -m http.server --bind localhost --cgi 5005。
在这两种情况下,我都需要在浏览器中打开一个特定的 python 文件,比如说:http://localhost:5005/myfunction.py
是否有替代方法可以直接在命令行中使用 http.server 来包含 file.py?
例如,根据micro npm 模块,相当于micro -p 5005 file.js 的东西?
【问题讨论】:
-
Algo for doing it ,,, let x = base url of the server and y = x+pythonfile .. 只需从 y 中删除 x 部分即可获得文件名 e..
-
您的意思是仅将
localhost:5005/(根目录)重定向到文件吗?我不熟悉micro是什么,所以我不知道你到底要什么...... -
@TadhgMcDonald-Jensen 是的。使用命令行将
localhost:5005/重定向到特定文件。 -
嗯,我会挖掘相关的源代码,我知道如果有
index.html它会自动重定向到那个。
标签: python python-3.x localhost httpserver