python -m SimpleHTTPServer - 监听 0.0.0.0:8000 但 http://0.0.0.0:8000/test.html 给出“找不到页面”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4341638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:18:54  来源:igfitidea点击:

python -m SimpleHTTPServer - Listening on 0.0.0.0:8000 but http://0.0.0.0:8000/test.html gives "Page Not Found"

pythonmacosterminalwebserver

提问by Denis Hoctor

After cding to my folder I enter

cding到我的文件夹后,我输入

python -m SimpleHTTPServer

and get

并得到

Serving HTTP on 0.0.0.0 port 8000 ...

in reply. But when I hit http://0.0.0.0:8000/test.htmlI get a page not found error.

回复。但是当我点击时,http://0.0.0.0:8000/test.html我得到一个页面未找到错误。

I've also tried

我也试过

pushd /path/you/want/to/serve; python -m SimpleHTTPServer; popd

taken from this question

取自这个问题

When I hit ls I can see the file and the directory. Anyone know what I'm doing wrong?

当我点击 ls 时,我可以看到文件和目录。有谁知道我做错了什么?

采纳答案by elarson

I think the other two answers are trying to make it clear that 0.0.0.0 is not the URL you should be visiting. When a Python web server (like cherrypy for instance) says it is serving on 0.0.0.0 it means it is listening for all TCP traffic that ends up at that machine no matter the hostname or IP that was requested. But, if you change it such that the socket listens on 127.0.0.1 or 'localhost', then unless the request was specifically to that IP/hostname, it won't respond to the request. For example, many times you can use your machine name instead of localhost (ubuntu allows this for example). If your machine name is 'brian' and you have a server listening on 0.0.0.0:8080, you should be able to reach that server with http://brian:8080. But if that server is listening on 'localhost', even though 'brian' is set to point to 'localhost', the server won't receive the message.

我认为其他两个答案试图表明 0.0.0.0 不是您应该访问的 URL。当 Python 网络服务器(例如cherrypy)说它在 0.0.0.0 上提供服务时,这意味着它正在侦听最终到达该机器的所有 TCP 流量,无论请求的主机名或 IP 是什么。但是,如果您更改它以使套接字侦听 127.0.0.1 或“localhost”,那么除非请求专门针对该 IP/主机名,否则它不会响应请求。例如,很多时候你可以使用你的机器名而不是 localhost(例如 ubuntu 允许这样做)。如果您的机器名称是 'brian' 并且您有一个服务器在 0.0.0.0:8080 上侦听,您应该能够通过http://brian:8080访问该服务器. 但是,如果该服务器正在侦听 'localhost',即使将 'brian' 设置为指向 'localhost',该服务器也不会收到该消息。

You also need to be sure the file really is in the directory you are running the command from. Otherwise, the 404 response is actually correct :)

您还需要确保该文件确实在您运行命令的目录中。否则,404 响应实际上是正确的 :)

Good luck!

祝你好运!

回答by Ryan Ginstrom

Have you tried http://127.0.0.1:8000/?

你试过http://127.0.0.1:8000/吗?

:)

:)

回答by The Maniac

Try browsing to http://localhost:8000/test.htmlor http://127.0.0.1:8000/test.html(those two should be exactly the same thing as long as your hosts file isn't all crazy-like).

尝试浏览到http://localhost:8000/test.htmlhttp://127.0.0.1:8000/test.html(只要你的主机文件不是疯狂的,这两个应该是完全一样的)。

0.0.0.0 is usually used by Windows as the "Not connected" IP, and can also be used as a sort of wildcard for when dealing with IPs. I am a bit confused at why your HTTP server is trying to host on 0.0.0.0, though. You may need to edit some config files and set that to 'localhost' or '127.0.0.1'.

0.0.0.0 通常被 Windows 用作“未连接”IP,也可以用作处理 IP 时的一种通配符。不过,我对为什么您的 HTTP 服务器试图在 0.0.0.0 上托管感到有些困惑。您可能需要编辑一些配置文件并将其设置为“localhost”或“127.0.0.1”。

回答by Rob

You must type in the ip-address of the computer your connecting to for example 192.168.0.2:8000 Change that to the ip-address of your server.

您必须输入您连接的计算机的 ip 地址,例如 192.168.0.2:8000 将其更改为您服务器的 ip 地址。

回答by Giannis Tzagarakis

  1. create a directory e.g. mkdir HTTPServer_dir
  2. move inside the folder cd HTTPServer_dir
  3. typing the command (according to python version) python -m SimpleHTTPSever 8000(or the port you want)
  4. go on a browser and type http://127.0.0.1:8000/
  1. 创建一个目录,例如 mkdir HTTPServer_dir
  2. 在文件夹内移动 cd HTTPServer_dir
  3. 输入命令(根据 python 版本)python -m SimpleHTTPSever 8000(或你想要的端口)
  4. 在浏览器上输入 http://127.0.0.1:8000/

Done !!!

完毕 !!!

You could make a simple index.html page inside the HTTPServer_dir so you can see an html page instead of directory listing

您可以在 HTTPServer_dir 中创建一个简单的 index.html 页面,以便您可以看到一个 html 页面而不是目录列表

回答by Sergey Gerasimenko

You should use this link http://127.0.0.1:8000/and can see all files there

您应该使用此链接http://127.0.0.1:8000/并可以看到那里的所有文件

回答by Gayan Weerakutti

Run ifconfigon Linux or ipconfigon Windows to find the ip address of the server.

ifconfig在 Linux 或ipconfigWindows上运行以查找服务器的 ip 地址。

$ sudo ifconfig
wlan0     Link encap:Ethernet  HWaddr 30:3a:64:b3:be:6a  
          inet addr:192.168.1.103  Bcast:192.168.1.255  Mask:255.255.255.0

Here in case the url would be:

如果网址是:

http://192.168.1.103:8000/test.html

http://192.168.1.103:8000/test.html

回答by Mostafa Sadeghi

try this in python3

在 python3 中试试这个

python -m http.server 8000 --bind 127.0.0.1

and in your browser this url:

在您的浏览器中,这个网址:

http://127.0.0.1:8000/

http://127.0.0.1:8000/

回答by shubhrant chauhan

Sometimes the same port number is used by some other service. So we can try with some other port like

有时,某些其他服务使用相同的端口号。所以我们可以尝试使用其他端口,例如

python -m SimpleHTTPServer 9090

And then simply hit http://{your system IP}:9090/

然后只需点击 http://{您的系统 IP}:9090/

this works for me.

这对我有用。

回答by rahul soni

this worked for me,replacing your machine name with
http://localhost:x000

这对我有用,http://localhost:x000替换你的机器名