当我的用户/名称文件夹中拥有所有适当的文件时,我需要帮助理解为什么我的本地主机服务器会出现一个目录。
我正在尝试使用 codepen 中的简单条形图来测试本地主机,但是当我运行服务器时,我只是遇到了一个目录列表,但是 html、css 和 js 的链接都在那里。我的文件夹中是否缺少某些内容,或者可能是某个地方的连接丢失了。我的 Python 知识非常基础,因此我们将不胜感激
HTML
<div class="chart"></div>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
代码
var data = [4, 8, 15, 16, 23, 42];
var x = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, 420]);
d3.select(".chart")
.selectAll("div")
.data(data)
.enter().append("div")
.style("width", function(d) { return x(d) + "px"; })
.text(function(d) { return d; });
CSS
.chart div {
font: 10px sans-serif;
background-color: steelblue;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
命令
C:\Users\name>python -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
xxx.0.0.1 - - [09/Oct/2018 11:57:09] "GET / HTTP/1.1" 200 -
xxx.0.0.1 - - [09/Oct/2018 11:57:27] "GET /Java.js HTTP/1.1" 200 -
xxx.0.0.1 - - [09/Oct/2018 11:57:35] "GET /css.css HTTP/1.1" 304 -
最佳答案
来自Python 3文档;
do_GET() The request is mapped to a local file by interpreting the request as a path relative to the current working directory.
If the request was mapped to a directory, the directory is checked for a file named index.html or index.htm (in that order). If found, the file’s contents are returned; otherwise a directory listing is generated by calling the list_directory() method.
因此,由于您没有 index.html 文件(或配置为充当主页的任何其他文件),因此服务器会列出根目录内容。简单的解决方案是将您的 html 文件命名为 index.html 并将其放置在服务器的根目录中,然后按照分步指南设置一个简单的 Python Web服务器如 this 。希望有帮助。
关于python - 在 python 本地主机服务器上进行测试引导我进入目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52719780/