如何公开 Django 的开发服务器?一般情况下可以吗?

发布于 2024-09-11 11:12:28 字数 824 浏览 13 评论 0原文

我目前正在尝试 Django 框架,我会向我的同事/朋友分享/展示/展示我所做的一些东西。我通过 VMware 在 Win7 下的 Ubuntu 中工作。所以我的愿望/愿望是发送我当前的 pub-IP 和端口(例如 http://123.123.123.123 :8181/django-app/) 给我的朋友,以便他们可以测试它。

问题是 - 我使用 django 的开发服务器(python /path-to-django-app/manage.py runserver $IP:$P​​ORT)。

如何公开开发服务器?

编辑:

哦,有件事我忘记说了。遗憾的是我在 Ubuntu 上使用了 VMware。我有一个 shellscript,它返回我当前的 int-IP 192.168.xx.xx 并将其保存在环境变量 ($CUR_IP) 中 所以,每次我想运行django的devserver时,我只需执行

python /path-to-django-site/manage.py runserver $CUR_IP:8080

这样我就成为一个http地址(例如 http://192.168.40.145:8080/app-name/)我可以在虚拟机之外使用。我可以在我的主机(win7)机器上测试它。这实际上就是我问这个问题的原因。我认为有一种方法可以使用 ext-IP 并使 runserver 也可以在外部使用

I'm currently trying out the Django framework and I would share/present/show some stuff I've made to my workmate/friends. I work in Ubuntu under Win7 via VMware. So my wish/desire is to send my current pub-IP with port (e.g http://123.123.123.123:8181/django-app/) to my friends so they could test it.

the Problem is - I use django's Dev server (python /path-to-django-app/manage.py runserver $IP:$PORT).

How do I make the devserver public?

EDIT:

Oh, there's something I forgot to mention. As I sad I use VMware with Ubuntu. I have a shellscript that returns me my current int-IP 192.168.xx.xx and saves it in a environment-variable ($CUR_IP)
So, each time I want to run django's devserver I simply execute

python /path-to-django-site/manage.py runserver $CUR_IP:8080

At this way I become an http-adress (e.g.http://192.168.40.145:8080/app-name/) which I CAN USE OUTSIDE my virtual machine. I could test it on my host (win7) machine. That's actually the reason why I asked the question. I thought there's a way to use the ext-IP and make runserver usable outside too

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

淡淡的优雅 2024-09-18 11:12:29

或者,您可以使用 cotunnel,只需在 ubuntu 中运行 cotunnel(在 VMware 中),在 cotunnel 仪表板中更改隧道端口您在本地使用哪个端口。它提供了公共网址,您可以与您的朋友分享该网址。

你的Django服务器可以监听127.0.0.1或0.0.0.0(我更喜欢0.0.0.0),这对于cotunnel来说并不重要。

Alternatively, you can use cotunnel, Just run cotunnel in your ubuntu (in VMware) change your tunnel port in cotunnel dashboard which port you are using in local side. It gives public url and you can share the url with your friends.

Your Django server can listen to 127.0.0.1 or 0.0.0.0 (I prefer 0.0.0.0) it does not matter for cotunnel.

墨离汐 2024-09-18 11:12:29

我是否可以建议尝试像 pyngrok 这样的东西以编程方式为您管理 ngrok 隧道?完全公开,我是它的开发者。 Django 示例此处,但它就像安装 pyngrok 一样简单

pip install pyngrok

并使用它:

from pyngrok import ngrok

# <NgrokTunnel: "http://<public_sub>.ngrok.io" -> "http://localhost:8000">
http_url = ngrok.connect(8000)

不会弄乱端口、防火墙或 IP 地址,现在您还可以检查流量(这很有用,因为您在这里所做的是持续开发,而不是运行 prod< /code>-就绪服务器)。

Might I suggest trying something like pyngrok to programmatically manage an ngrok tunnel for you? Full disclosure, I am the developer of it. Django example here, but it's as easy as installing pyngrok:

pip install pyngrok

and using it:

from pyngrok import ngrok

# <NgrokTunnel: "http://<public_sub>.ngrok.io" -> "http://localhost:8000">
http_url = ngrok.connect(8000)

No messing with ports or firewalls or IP addresses, and now you can also inspect the traffic (which is useful since what you're doing here is ongoing development, not running a prod-ready server).

望喜 2024-09-18 11:12:28
python manage.py runserver 0.0.0.0:8181

这将 运行开发服务器应该侦听端口8181上的所有IP。

请注意,截至2011年6月17日Django 开发服务器默认是线程化的(ticket #1609)

来自文档:

请注意默认 IP 地址,
127.0.0.1,无法从网络上的其他计算机访问。到
使您的开发服务器可见
对于网络上的其他机器,使用
它自己的IP地址(例如192.168.2.1)
或 0.0.0.0。

python manage.py runserver 0.0.0.0:8181

This will run development server that should listen on all IP's on port 8181.

Note that as of Jun 17, 2011 Django development server is threaded by default (ticket #1609).

From docs:

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.

一身骄傲 2024-09-18 11:12:28

假设你已经安装了 ruby​​,你只需要获取 localtunnel:

gem install localtunnel

然后启动你的 python 开发服务器:

python manage.py runserver 0.0.0.0:8000

在另一个 shell 中,启动 localtunnel:

localtunnel -k ~/.ssh/id_rsa.pub 8000 

这将输出一个 url 来访问你的本地服务器。

Port 8000 is now publicly accessible from http://xxxx.localtunnel.com

就是这样。

Assuming you have ruby installed, you just have to get localtunnel:

gem install localtunnel

then start your python development server with:

python manage.py runserver 0.0.0.0:8000

in another shell, start localtunnel:

localtunnel -k ~/.ssh/id_rsa.pub 8000 

That will output an url to access your local server.

Port 8000 is now publicly accessible from http://xxxx.localtunnel.com

That's it.

始终不够 2024-09-18 11:12:28

我必须将此行添加到 settings.py 才能使其工作(否则从另一台计算机访问时会显示错误),

ALLOWED_HOSTS = ['*']

然后运行服务器:

python manage.py runserver 0.0.0.0:9595

另外,请确保您的防火墙允许与所选端口(9595本案)

I had to add this line to settings.py in order to make it work (otherwise it shows an error when accessed from another computer)

ALLOWED_HOSTS = ['*']

then ran the server with:

python manage.py runserver 0.0.0.0:9595

Also, make sure that your firewall allows communication to the chosen port (9595 in this case)

暖风昔人 2024-09-18 11:12:28

192.168.*.* 是 LAN 专用地址 - 一旦您完成了正确的 VMWare(或其他 VM 管理器)和防火墙咒语以使其可以从 LAN 访问,它仍然无法访问可以从 LAN 外部访问,即从整个互联网访问(这也是一件好事,因为这样的开发服务器并不是为安全性和可扩展性而设计的)。

为了使具有 LAN 私有 IP 的机器的某些端口对整个互联网可见,您需要一个具有“虚拟服务器”功能的路由器(许多路由器,甚至是便宜的路由器,都提供它,但不可能具体启用因为每个品牌都有自己独特的方式)。我还建议使用 dyndns 或其他类似服务将稳定的 DNS 名称与您不断变化的公共 IP 关联起来(除非当然,您需要从连接提供商那里购买静态 IP,但后一种选择一直变得越来越昂贵)。

superuser.com 或 serverfault.com 可能会提供更好的答案和详细信息(一旦您在问题中提供了配置的每一个小细节),因为该问题与软件开发没有太大关系,而与服务器管理和配置有关。

192.168.*.* is a LAN-private address -- once you've done the proper VMWare (or other VM manager) and firewall incantations to make it accessible from the LAN, it still won't be accessible from outside the LAN, i.e., from the internet at large (a good thing too, because such development servers are not designed for security and scalability).

To make some port of a machine with a LAN-private IP visible to the internet at large, you need a router with a "virtual servers" ability (many routers, even cheap ones, offer it, but it's impossible to be specific about enabling it since each brand has its own idiosyncratic way). I would also recommend dyndns or other similar service to associate a stable DNS name to your always-varying public IP (unless you're splurging for a static IP from your connectivity provider, of course, but the latter option is becoming costlier all the time).

superuser.com or serverfault.com may provide better answers and details (once you give every single little detail of your configuration in a question) since the question has nothing much to do with software development and everything to do with server administration and configuration.

帥小哥 2024-09-18 11:12:28

已经回答,但添加相同本地隧道的 npm 备用

sudo npm install -g localtunnel

lt --port 8000 --subdomain yash

Already answered but adding npm alternate of same localtunnel

sudo npm install -g localtunnel

lt --port 8000 --subdomain yash
吲‖鸣 2024-09-18 11:12:28

如果您使用 Virtualbox,则需要将 VB 中的网络设置从“NAT”更改为“桥接适配器”。然后重新启动linux。现在,如果您运行 sudo ifconfig 您可以看到您的 IP 地址,例如 192.168.*.* 。最后一步是 runserver

python manage.py runserver 192.168.*.*:8000

干杯!

If you are using Virtualbox, You need to change the network setting in VB from "NAT" to "Bridged Adaptor". Then restart the linux. Now if you run sudo ifconfig you are able to see your IP address like 192.168.*.* . The last step is runserver

python manage.py runserver 192.168.*.*:8000

Cheers!

蘸点软妹酱 2024-09-18 11:12:28

您需要在 VMWare 中配置桥接网络,并在 Ubuntu 防火墙中授予对目标端口的访问权限。

You need to configure bridged networking in VMWare and also grant access to the target port in Ubuntu firewall.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文