Python 如何使用命令行中的http.server作为参数传递file.py?
考虑一个简单的服务器.pyPython 如何使用命令行中的http.server作为参数传递file.py?,python,python-3.x,localhost,httpserver,Python,Python 3.x,Localhost,Httpserver,考虑一个简单的服务器.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 r
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-mhttp.server--bind localhost--cgi 5005
在这两种情况下,我都需要在浏览器中打开一个特定的python文件,比如:http://localhost:5005/myfunction.py
是否有其他方法可以直接使用http.server将file.py包含在命令行中
例如,根据micro npm module,相当于micro-p 5005 file.js
?的东西似乎重定向到了index.html
或index.htm
(或文件列表)之外的任何东西,但以下情况不支持:
#如果:
对于“index.html”、“index.htm”中的索引:
index=os.path.join(路径,索引)
如果os.path.存在(索引):
路径=索引
打破
其他:
返回self.list\u目录(路径)
一种解决方案是简单地修改原始源代码,使元组(“index.html”、“index.htm”)
指向运行时更容易修改的内容。或者,您可以自己定义进行检查的包装器,基本上只需复制整个def send_head
函数并覆盖该循环,将path
分配给要重定向到的文件
但愿我有一个不涉及重写源代码的解决方案,但我想不出另一个(合理的)解决方案。Algo,让x=服务器的基本url,y=x+pythonfile。。只需从y中删除x部分即可获得文件name..您的意思是只将localhost:5005/
(根目录)重定向到文件吗?我对什么是micro
不熟悉,所以我不知道你到底想要什么…@Tadhgcdonald Jensen是的。使用command line.hmm将localhost:5005/
重定向到特定文件,我将挖掘相关的源代码,我知道是否有index.html
它将自动重定向到该文件。我认为这是一个很好的解决方案。我会检查这个替代方案。谢谢你的帮助。
#if <path is a directory>:
for index in "index.html", "index.htm":
index = os.path.join(path, index)
if os.path.exists(index):
path = index
break
else:
return self.list_directory(path)