打开端口不起作用

Luk*_*s T 1 networking python curl ufw port

我有一个 python 服务器在树莓派的 8000 端口上运行,并想让它在本地网络中访问,但目前无法正常工作。

接口 wlan0 配置了 IP 10.0.0.69 和网络掩码 255.255.255.0。

我无法访问本地网络中的服务器(从不同的主机):

root@DESKTOP-Lukas:~# curl http://10.0.0.69:8000
curl: (7) Failed to connect to 10.0.0.69 port 8000: Connection refused
Run Code Online (Sandbox Code Playgroud)

我也无法从树莓派访问服务器:

lukas@raspberrypi:~ $ curl http://10.0.0.69:8000
curl: (7) Failed to connect to 10.0.0.69 port 8000: Connection refused
Run Code Online (Sandbox Code Playgroud)

服务器正在运行并侦听端口 8000:

lukas@raspberrypi:~ $ sudo netstat -tnlp | ack 8000
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      743/python
Run Code Online (Sandbox Code Playgroud)

服务器在使用 localhost 时正确响应:

lukas@raspberrypi:~ $ curl localhost:8000
<h1>Not Found</h1><p>The requested URL / was not found on this server.</p>
Run Code Online (Sandbox Code Playgroud)

防火墙允许端口 8000 的操作:

lukas@raspberrypi:~ $ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
...
8000                       ALLOW       Anywhere
8000/tcp                   ALLOW       Anywhere
8000 (v6)                  ALLOW       Anywhere (v6)
8000/tcp (v6)              ALLOW       Anywhere (v6)
Run Code Online (Sandbox Code Playgroud)

Dop*_*oti 5

您的netstat输出显示了问题:

tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      743/python
Run Code Online (Sandbox Code Playgroud)

具体来说,127.0.0.1:8000显示您的 Web 服务器仅绑定到环回地址127.0.0.1。您需要将其绑定到实际的网络接口,或者更简单地将其绑定到所有地址(通常通过指定0.0.0.0),然后所有其他主机都可以通过到您的 Pi 的路由访问它。