简体   繁体   English

manage.py 运行服务器

[英]manage.py runserver

当我尝试检查机器 B 时,我正在从机器 A 运行 python manage.py runserver 我输入的 url 是http://A:8000/我收到类似系统返回的错误:(111) 连接被拒绝

You can run it for machines in your network by您可以通过以下方式为网络中的机器运行它

./manage.py runserver 0.0.0.0:8000 ./manage.py 运行服务器 0.0.0.0:8000

And than you will be able to reach you server from any machine in your network.并且您将能够从网络中的任何机器访问您的服务器。 Just type on other machine in browser http://192.168.0.1:8000 where 192.168.0.1 is IP of you server... and it ready to go....只需在浏览器中的其他机器上输入http://192.168.0.1:8000 ,其中192.168.0.1是您服务器的 IP ......它准备好了......

or in you case:或者在你的情况下:

  1. On machine A in command line ./manage.py runserver 0.0.0.0:8000在命令行中的机器A ./manage.py runserver 0.0.0.0:8000
  2. Than try in machine B in browser type http://A:8000比在机器B中尝试在浏览器中输入http://A:8000
  3. Make a sip of beer.喝一口啤酒。

Source from django docs 来自 Django 文档

You need to tell manage.py the local ip address and the port to bind to.您需要告诉 manage.py 本地 IP 地址要绑定的端口。 Something like python manage.py runserver 192.168.23.12:8000 .类似于python manage.py runserver 192.168.23.12:8000东西。 Then use that same ip and port from the other machine.然后使用来自另一台机器的相同 ip 和端口。 You can read more about it here in the documentation.您可以在此处的文档中阅读有关它的更多信息。

I was struggling with the same problem and found one solution.我正在努力解决同样的问题,并找到了一个解决方案。 I guess it can help you.我想它可以帮助你。 when you run python manage.py runserver, it will take 127.0.0.1 as default ip address and 8000. 127.0.0.0 is the same as localhost which can be accessed locally.当你运行 python manage.py runserver 时,它会以 127.0.0.1 作为默认 IP 地址和 8000。127.0.0.0 与 localhost 相同,可以在本地访问。 to access it from cross origin you need to run it on your system ip or 0.0.0.0.要从跨源访问它,您需要在系统 IP 或 0.0.0.0 上运行它。 0.0.0.0 can be accessed from any origin in the network. 0.0.0.0 可以从网络中的任何来源访问。 for port number, you need to set inbound and outbound policy of your system if you want to use your own port number not the default one.对于端口号,如果要使用自己的端口号而不是默认端口号,则需要设置系统的入站和出站策略。

To do this you need to run server with command python manage.py runserver 0.0.0.0:<your port> as mentioned above为此,您需要使用命令python manage.py runserver 0.0.0.0:<your port>运行服务器,如上所述

or, set a default ip and port in your python environment.或者,在你的 python 环境中设置一个默认的 ip 和端口。 For this see my answer on django change default runserver port为此,请参阅我对django 更改默认运行服务器端口的回答

Enjoy coding .....享受编码......

Just in case any Windows users are having trouble, I thought I'd add my own experience.以防万一任何 Windows 用户遇到问题,我想我会添加我自己的经验。 When running python manage.py runserver 0.0.0.0:8000 , I could view urls using localhost:8000 , but not my ip address 192.168.1.3:8000 .运行python manage.py runserver 0.0.0.0:8000 ,我可以使用localhost:8000查看网址,但不能查看我的 IP 地址192.168.1.3:8000

I ended up disabling ipv6 on my wireless adapter, and running ipconfig /renew .我最终在我的无线适配器上禁用了 ipv6,并运行ipconfig /renew After this everything worked as expected.在此之后,一切都按预期进行。

在烧瓶中使用flask.ext.script,你可以这样做:

python manage.py runserver -h 127.0.0.1 -p 8000

For people who are using CentOS7, In order to allow access to port 8000, you need to modify firewall rules in a new SSH connection:对于使用 CentOS7 的人,为了允许访问 8000 端口,您需要在新的 SSH 连接中修改防火墙规则:

sudo firewall-cmd --zone=public --permanent --add-port=8000/tcp
sudo firewall-cmd --reload

First, change your directory:首先,更改您的目录:

cd your_project name

Then run:然后运行:

python manage.py runserver

I had the same problem and here was my way to solve it:我遇到了同样的问题,这是我解决它的方法:

First, You must know your IP address.首先,您必须知道您的 IP 地址。 On my Windows PC, in the cmd windows i run ipconfig and select my IP V4 address.在我的 Windows PC 上,在 cmd 窗口中运行 ipconfig 并选择我的 IP V4 地址。 In my case 192.168.0.13在我的情况下 192.168.0.13

Second as mention above: runserver 192.168.0.13:8000第二个如上所述:runserver 192.168.0.13:8000

It worked for me.它对我有用。 The error i did to get the message was the use of the gateway address not my PC address.我为获取消息所做的错误是使用了网关地址而不是我的 PC 地址。

Ok just came across this post this is a little off topic but hopefully explains a few things, The IP 127.0.0.1 points to your network card so any traffic that you cause to go to that IP address will not leave your computer.好的,刚看到这篇文章,这有点离题,但希望能解释一些事情,IP 127.0.0.1 指向您的网卡,因此您导致进入该 IP 地址的任何流量都不会离开您的计算机。

For example modern network cards in laptops for example will not even give you that IP if you are not connected to a wifi or cabled network so you'll need to be connected at least to activate the card.例如,如果您没有连接到 wifi 或有线网络,笔记本电脑中的现代网卡甚至不会为您提供该 IP,因此您至少需要连接才能激活网卡。

If you need to run multiple servers on the same machine but want to access them with a domain then you have a couple of options如果您需要在同一台机器上运行多个服务器,但想使用域访问它们,那么您有几个选择

  1. edit your computers host file to define the domain and what IP it goes to编辑您的计算机主机文件以定义域及其访问的 IP
  2. use a DNS Alias I set up using a cname record years ago *.local.irishado.com will point to 127.0.0.1使用我多年前使用 cname 记录设置的 DNS 别名 *.local.irishado.com 将指向 127.0.0.1

so for example these three domains will point to your local machine例如,这三个域将指向您的本地计算机

will all point to your local machine then in python projects you will need to edit the projects setting file ALLOWED_HOSTS property to hold the domain it will accept将全部指向您的本地机器,然后在 python 项目中,您需要编辑项目设置文件 ALLOWED_HOSTS 属性以保存它将接受的域

ALLOWED_HOSTS = ['site1.local.irishado.com'] ALLOWED_HOSTS = ['site1.local.irishado.com']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM