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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3328926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:28:49  来源:igfitidea点击:

How to make Django's devserver public ? Is it generally possible?

pythondjangodevserver

提问by V-Light

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.

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

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

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

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

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

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

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

采纳答案by Alex Martelli

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 notdesigned for security and scalability).

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

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 dyndnsor 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).

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

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.

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

回答by Davor Lucic

python manage.py runserver 0.0.0.0:8181

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

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

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

请注意,截至2011 年 6 月 17 日,Django 开发服务器默认是线程化的(票号 #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.

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

回答by J. Random Geek

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

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

回答by trez

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

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

gem install localtunnel

then start your python development server with:

然后使用以下命令启动您的 python 开发服务器:

python manage.py runserver 0.0.0.0:8000

in another shell, start localtunnel:

在另一个 shell 中,启动 localtunnel:

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

That will output an url to access your local server.

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

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

That's it.

就是这样。

回答by RezA

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 ifconfigyou are able to see your IP address like 192.168.*.*. The last step is runserver

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

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

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

Cheers!

干杯!

回答by Yash

Already answered but adding npm alternate of same localtunnel

已回答但添加相同本地隧道的 npm 替代

sudo npm install -g localtunnel

lt --port 8000 --subdomain yash

回答by Bonfix Ngetich

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)

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

ALLOWED_HOSTS = ['*']

then ran the server with:

然后运行服务器:

python manage.py runserver 0.0.0.0:9595

回答by alexdlaird

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

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

pip install pyngrok

and using it:

并使用它:

from pyngrok import ngrok

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).

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

回答by 400ZP

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.

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

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.

您的 Django 服务器可以收听 127.0.0.1 或 0.0.0.0(我更喜欢 0.0.0.0),这对 cotunnel 无关紧要。