简体   繁体   English

为什么我可以连接到 http://127.0.0.1:8000/ 而不能连接到 http://192.168.1.6/

[英]Why can I connect to http://127.0.0.1:8000/ but not to http://192.168.1.6/

I'm running OS X Mountain Lion on a machine with local IP address 192.168.1.6 (as reported by both the Network utility and ifconfig) and am running a local (Django) development web server on port 8000 that I would like to connect to from a virtual machine running a guest OS on the same machine.我在本地 IP 地址为 192.168.1.6 的机器上运行 OS X Mountain Lion(由 Network 实用程序和 ifconfig 报告),并且在我想连接的端口 8000 上运行本地(Django)开发 Web 服务器从在同一台机器上运行来宾操作系统的虚拟机。

On the host OS (ie, OS X running on the metal of the machine w/ address 192.168.1.6) I can connect to my test web server through the browser by navigating to 127.0.0.1:8000;在主机操作系统(即运行在地址为 192.168.1.6 的机器上的 OS X)上,我可以通过浏览器导航到 127.0.0.1:8000 连接到我的测试 Web 服务器; or localhost:8000;或本地主机:8000; but not when using the machine's local IP address.但在使用机器的本地 IP 地址时则不然。 Here's what makes this extra confusing:这是什么让这个额外的混乱:

  • The router is not filtering the ports;路由器没有过滤端口; and, just to be sure, I've set it to explicitly forward ports 8000 and 22 to 192.168.1.6;而且,可以肯定的是,我已将其设置为明确地将端口 8000 和 22 转发到 192.168.1.6; And speaking of port 22,说到端口 22,
  • When I start the SSH service, I can connect (from the command line) via ssh 192.168.1.6当我启动 SSH 服务时,我可以通过 ssh 192.168.1.6 连接(从命令行)
  • It's not a browser issue, because I also can't telnet to 192.168.1.6 port 8000 (connection refused) while I can telnet to 127.0.0.1 port 8000, and I can also telnet to 192.168.1.6 port 22这不是浏览器问题,因为我也不能 telnet 到 192.168.1.6 端口 8000(连接被拒绝),而我可以telnet 到 127.0.0.1 端口 8000,我也可以 telnet 到 192.168.1.6 端口 22
  • The firewall is set to off (as reported in System Preferences) but to be extra safe, I've also set an ipfw rule to allow everything through防火墙设置为关闭(如系统偏好设置中所述)但为了更加安全,我还设置了一个 ipfw 规则以允许所有内容通过

Here are the ipfw rules:以下是 ipfw 规则:

00100 allow tcp from any to any dst-port 8000
65535 allow ip from any to any

Here is additional confirmation that the port is, indeed, being listened to by my test server:这是我的测试服务器确实正在侦听端口的额外确认:

netstat -an | grep 8000
tcp4       0      0  127.0.0.1.8000         *.*                    LISTEN 

so what's going on here?那么这里发生了什么? Somehow port 22 is being treated differently than port 8000, but every place I can think to look for those differences I can't find any.不知何故,端口 22 的处理方式与端口 8000 不同,但我能想到的每个地方都找不到这些差异。 Why can't I get into this machine's port 8000 using its local ip address?为什么我不能使用它的本地IP地址进入这台机器的8000端口?

When you start Django development server you need to give the address explicitly:当你启动 Django 开发服务器时,你需要明确给出地址:

python manage.py runserver 192.168.1.6:8000

Or if you want the server to run on all interfaces you can use:或者,如果您希望服务器在所有接口上运行,您可以使用:

python manage.py runserver 0.0.0.0:8000

In other case Django development server defaults to running on the local interface only.在其他情况下,Django 开发服务器默认仅在本地界面上运行。

The problem for me was I accidentally quit the server whenever trying to copy the server address.对我来说,问题是我在尝试复制服务器地址时不小心退出了服务器。 So instead of using ctrl+C just write down the address into your browser.因此,不要使用ctrl+C只需在浏览器中记下地址即可。

I solved the issue.There are a few things you might be missing.Listing them below-我解决了这个问题。你可能会遗漏一些东西。在下面列出它们-

1.Once it starts the server, do not press Ctrl+C anyhow .u might be pressing it to copy to url and that accidently closes the server due to which it might be happening. 1.一旦它启动服务器,无论如何都不要按Ctrl + C。你可能正在按它来复制到url,并且由于它可能会发生意外关闭服务器。

2.instead of http://127.0.0.1:8000/ ...change the port number to http://127.0.0.1:8080/ ...That would work. 2.代替http://127.0.0.1:8000/ ...将端口号更改为http://127.0.0.1:8080/ ...那会起作用。

3.Try changing the firewall setting and allow the app. 3.尝试更改防火墙设置并允许该应用程序。

4.Try opening it with different browsers and incognito too. 4.尝试使用不同的浏览器和隐身模式打开它。

The above steps helped solve my issue.Hope they help u too...:)以上步骤帮助解决了我的问题。希望它们也能帮助你...:)

请不要通过ctr + C快捷键复制地址,因为它会关闭服务器。请右键单击复制相同的地址,或者自己写一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM