如何从外界访问本地的Django网络服务器

编程入门 行业动态 更新时间:2024-10-23 08:34:00
本文介绍了如何从外界访问本地的Django网络服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我按照说明这里使用内置的Web服务器运行Django,并且可以使用 python manage.py runserver 成功运行它。如果我从网络服务器本地访问127.0.0.1:port,我得到的Django页面指示它的工作。

我意识到Django网络服务器不是一个生产服务器,但它是对我来说重要的是测试目的,以便能够从外部世界访问它 - 即不是来自服务器上的Web浏览器,而是来自不同的计算机。

我尝试过:

http:// mywebserver:port_django_runs_on

但它没有工作。我也尝试使用IP(基于ifconfig)来访问:

http:// myipaddress:port_django_runs_on

哪些不起作用。

Web服务器是运行,所以它必须从外面访问,我只是不知道如何。我正在使用Apache运行Linux,尽管我还没有配置Django与Apache。

有关如何做到这一点的任何想法?

解决方案

您必须运行开发服务器,使其侦听界面到您的网络。

例如

python manage.py runserver 0.0.0.0:8000

侦听端口8000上的每个界面。

无论您是使用IP还是主机名访问Web服务器。我想你还在自己的局域网中。 如果你真的想从外部访问服务器,你还必须配置你的路由器转发端口,例如

检查您的服务器上的防火墙是否允许与使用端口的传入连接!

假设您可以从外部成功访问Apache服务器,还可以尝试以下方式:

  • 停止Apache服务器,以便端口 80 是免费的。
  • 使用启动开发服务器sudo python manage.py runserver 0.0.0.0:80

I followed the instructions here to run Django using the built-in webserver and was able to successfully run it using python manage.py runserver. If I access 127.0.0.1:port locally from the webserver, I get the Django page indicating it worked.

I realize the Django webserver is not a production server, but it's important for me for testing purposes to be able to access it from the outside world -- i.e. not from a web browser on the server, but from a different computer.

I tried:

mywebserver:port_django_runs_on

but it did not work. I also tried using the IP instead (based on ifconfig) to access:

myipaddress:port_django_runs_on

which did not work either.

The web server is running so it must be accessible from the outside, I'm just not sure how. I am running Linux with Apache, though I have not configured Django with Apache.

Any ideas on how to do this?

解决方案

You have to run the development server such that it listens on the interface to your network.

E.g.

python manage.py runserver 0.0.0.0:8000

listens on every interface on port 8000.

It doesn't matter whether you access the webserver with the IP or the hostname. I guess you are still in your own LAN. If you really want to access the server from outside, you also have to configure your router to forward port e.g. 8000 to your server.

Check your firewall on your server whether incoming connections to the port in use are allowed!

Assuming you can access your Apache server from the outside successfully, you can also try this:

  • Stop the Apache server, so that port 80 is free.
  • Start the development server with sudo python manage.py runserver 0.0.0.0:80

更多推荐

如何从外界访问本地的Django网络服务器

本文发布于:2023-11-23 18:44:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:外界   服务器   网络   Django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!