python - 无法从本地服务器外部连接到 Django

标签 python django centos7 telnet firewall

我在Centos7上搭建了一个基本的Django项目和应用程序,但我无法从外部访问它。检查下面的配置。

[root@pytoncentos7 myproject]# cat settings.py | grep ALLOWED
ALLOWED_HOSTS = ['*']

然后我按如下方式运行我的服务器。

[root@pytoncentos7 myproject]#  python3.6 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 24, 2018 - 15:03:04
Django version 2.0.5, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

另外,防火墙没有运行

[root@pytoncentos7 myproject]# firewall-cmd --state
not running

下面是我的端口列表。

[root@pytoncentos7 myproject]# netstat -lptun
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
PID/Program name
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      
1643/python3.6
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      
876/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      
982/master
tcp6       0      0 :::3306                 :::*                    LISTEN      
986/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      
876/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      
982/master
udp        0      0 127.0.0.1:323           0.0.0.0:*                           
636/chronyd
udp6       0      0 ::1:323                 :::*                                
636/chronyd

此外,当我尝试从外部连接到端口 8000 时,它失败了!!但是我当然可以在本地远程登录。同时,我让 Apache 在 80 上运行并且可以从内部/外部访问,但是当我禁用 Apache 并在端口 80 上运行 Django 时,我仍然可以在本地连接但不能连接外部

Django 服务器只是一个运行在我本地计算机上的虚拟机

最佳答案

来自 https://docs.djangoproject.com/en/2.0/ref/django-admin/#runserver

Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0 or :: (with IPv6 enabled).

您必须使用命令 python3.6 manage.py runserver 0.0.0.0:8000 运行

然后找出虚拟机的本地 IP 并导航到它

http://192.168.0.XXX:8000   <--- Use local IP of VM

关于python - 无法从本地服务器外部连接到 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50512716/

相关文章:

python - Psycopg copy_expert 方法 - 如何正确使用

python - 如何将 tkinter Canvas 滚动到绝对位置?

python - Python 中的 PyObject 是什么?

python - crontab不会执行python

python - 需要其他方式来停止我的游戏(pygame)!

python - Elasticsearch-dsl 嵌套查询

Django 模型继承,过滤模型

python - django打印模板中的表单错误

NGinX 无法连接到 CentOS 7 上的 Jenkins

docker - kubectl 无法连接到 docker 注册表来下载镜像