Python 如何从安卓平板电脑访问我的 127.0.0.1:8000
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17116718/
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
how to access my 127.0.0.1:8000 from android tablet
提问by doniyor
I am developing a webpage in django (on my pc with windows 7) and now i need to test some pages in tablet pcs. suddenly the thought came if i can have an access to my localhost in windows from android tablet. is that possible? I am in the same wifi connection in both devices at home.
我正在 django 中开发一个网页(在我的带有 Windows 7 的电脑上),现在我需要在平板电脑中测试一些页面。突然想到,如果我可以在 Windows 中从 android 平板电脑访问我的本地主机。那可能吗?我在家里的两台设备上都处于相同的 wifi 连接中。
i read a lot questions and answers regarding this issue in stackoverflow and other places, but none of them gave me some concrete solutions.
我在stackoverflow和其他地方阅读了很多关于这个问题的问题和答案,但没有一个给我一些具体的解决方案。
I have samsung tab 2 10.1 with android 4.0.4.
我有带 android 4.0.4 的三星 tab 2 10.1。
appreciate any help, thanks
感谢任何帮助,谢谢
采纳答案by bojangler
You can find out what the ip address of your PC is with the ipconfigcommand in a Windows command prompt. Since you mentioned them being connected over WiFi look for the IP address of the wireless adapter.
您可以ipconfig使用 Windows 命令提示符中的命令找出 PC 的 IP 地址。由于您提到它们通过 WiFi 连接,因此请查找无线适配器的 IP 地址。
Since the tablet is also in this same WiFi network, you can just type that address into your tablet's browser, with the :8000 appended to it and it should pull up the page.
由于平板电脑也在同一个 WiFi 网络中,您只需在平板电脑的浏览器中输入该地址,并在其后附加 :8000,它就会拉出页面。
回答by smk
If both are connected to the same network, all you need to do is provide the IP address of your server (in your network) in your Android app.
如果两者都连接到同一个网络,您需要做的就是在您的 Android 应用程序中提供您的服务器(在您的网络中)的 IP 地址。
回答by DevOfZot
127.0.0.1 is a loopback address that means, roughly, "this device"; your PC and your android tablet are separate devices, so each of them has its own 127.0.0.1. In other words, if you try to go to 127.0.0.1 on your Android tab, it's trying to connect to a webserver on the Android device, which is not what you want.
127.0.0.1 是一个环回地址,大致意思是“这个设备”;您的 PC 和您的 android 平板电脑是独立的设备,因此它们每个都有自己的 127.0.0.1。换句话说,如果您尝试转到 Android 选项卡上的 127.0.0.1,它会尝试连接到 Android 设备上的网络服务器,这不是您想要的。
However, you should be able to connect over the wifi. On your windows box, open a command prompt and execute "ipconfig". Somewhere in the output should be your windows box's address, probably 192.168.1.100 or something similar. You tablet should be able to see te Django server at that address.
但是,您应该能够通过 wifi 连接。在您的 Windows 框中,打开命令提示符并执行“ipconfig”。输出中的某处应该是您的 windows 框的地址,可能是 192.168.1.100 或类似的地址。您的平板电脑应该能够在该地址看到 te Django 服务器。
回答by Manuj Rastogi
need to know the ip address of your machine .. Make sure both of your machines (tablet and computer) connected to same network
需要知道你机器的ip地址..确保你的两台机器(平板电脑和电脑)连接到同一个网络
192.168.0.22 - say your machine address
192.168.0.22 - 说出你的机器地址
do this :
做这个 :
192.168.0.22:8000 -- from your tablet
192.168.0.22:8000——来自你的平板电脑
this is it !!!
就是这个 !!!
回答by cwallenpoole
So, there are a couple of issues it seems. The question most of the answers are addressing is "how do you connect to another server in your local network?" (or variants). There are two answers, you can use the computer's IP directly, or you can use the computer's name (you may need to append .local). For example, my computer is xavier.local.
所以,似乎有几个问题。大多数答案解决的问题是“如何连接到本地网络中的另一台服务器?” (或变体)。有两个答案,可以直接使用计算机的IP,也可以使用计算机的名称(可能需要附加.local)。例如,我的电脑是xavier.local.
The second issue is that you seem to be addressing is that runserveris not accessible via other computers on the network (this is your actualquestion). The reason is that by default Django's runserver will only acknowledge requests from the machine which is calling them. This means that the default settings would make it so that you would only be able to access the server from Windows (and they did this on purpose for security reasons). In order for it to listen to other requests you have two options:
第二个问题是您似乎要解决的runserver是无法通过网络上的其他计算机访问的问题(这是您的实际问题)。原因是默认情况下 Django 的 runserver 只会确认来自调用它们的机器的请求。这意味着默认设置将使您只能从 Windows 访问服务器(出于安全原因,他们是故意这样做的)。为了让它侦听其他请求,您有两个选择:
runserver 192.168.1.101:8000
# Only handle requests which are made to the IP address 192.168.1.101
Or (and this is easier when dealing with more than one environment):
或者(这在处理多个环境时更容易):
runserver 0.0.0.0:8000 # handle all requests
So, if your IP address is 192.168.1.101:
因此,如果您的 IP 地址是 192.168.1.101:
runserver # only requests made on the machine will be handled
runserver 127.0.0.1 # only requests made on the machine will be handled
runserver 192.168.1.101 # handles all requests (unless IP changes)
runserver 192.168.1.100 # does not handle any requests (wrong IP)
runserver 0.0.0.0 # handles all requests (even if the IP changes)
I do think it important to note that 0.0.0.0is realistically not a security question when dealing with a local, development machine. It only becomes a significant problem when working on a large app with a machine which can be addressed from the outside world. Unless you have port forwarding (I do), or something wonky like that, you should not be too concerned.
我确实认为重要的是要注意,0.0.0.0在处理本地开发机器时,这实际上不是安全问题。只有在使用可以从外部世界解决的机器上处理大型应用程序时,它才会成为一个重大问题。除非你有端口转发(我有)或类似的东西,否则你不应该太担心。
回答by Tower Jimmy
Tested using easy EasyPHP DevServer 14.1. The trick is you must first add your local ip address to the Apache server to listen to it.
使用easy EasyPHP DevServer 14.1 进行测试。诀窍是您必须首先将本地 IP 地址添加到 Apache 服务器以侦听它。
Right click on the tray icon, go to "Configuration" -> "Apache" in the "httpd.config"
右键单击托盘图标,转到“httpd.config”中的“配置”->“Apache”
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 127.0.0.1:80
Listen 192.168.0.201:80 <<-- ADD THIS LINE
Then go to your Control Panel and deactivate your Windows Firewall or add your Smartphone/Tablet Ip Address to the exception.
然后转到您的控制面板并停用您的 Windows 防火墙或将您的智能手机/平板电脑 IP 地址添加到例外中。
That's it. I hope it helps.
就是这样。我希望它有帮助。
The IP Address 192.168.0.201 may be different from yours. 192.168.0.X
IP 地址 192.168.0.201 可能与您的不同。192.168.0.X
回答by pit tanenbaum
I've struggled with this problem myself, and I couldn't figure out what it was, since it worked perfectly on my iPhone, I decided to look into the problem and find a quick solution. My local machine is a Mac OSX 10.10+, one option would have been to start an Apache server, but that's super unhandy - changing the DocumentRoot every time you want to check something quickly on your Android device. Besides that, changing the DocumentRoot is a pain in the a** on Mac OSX 10.10.
我自己一直在努力解决这个问题,但我无法弄清楚它是什么,因为它在我的 iPhone 上运行得很好,我决定调查这个问题并找到一个快速的解决方案。我的本地机器是 Mac OSX 10.10+,一种选择是启动 Apache 服务器,但这非常不方便 - 每次要在 Android 设备上快速检查内容时都更改 DocumentRoot。除此之外,在 Mac OSX 10.10 上更改 DocumentRoot 是一个痛苦的事情。
If you want to use:
如果你想使用:
python -m SimpleHTTPServer
on your Android, do this:
在您的 Android 上,请执行以下操作:
sudo python -m SimpleHTTPServer [ANY PORT YOU WANT, BUT NOT 8000]
I hope this helps.
我希望这有帮助。
cheerz -Pit
欢呼-坑
回答by foysal
Though this thread was active quite a long time ago. This is what worked for me on windows 10. Posting it in details. Might be helpful for the newbies like me.
虽然这个线程很久以前就活跃了。这就是在 Windows 10 上对我有用的方法。详细发布。可能对我这样的新手有帮助。
- Add
ALLOWED_HOSTS = ['*']in djangosettings.pyfile - run django server with
python manage.py 0.0.0.0:YOUR_PORT. I used9595as my port. Make firewall to allow access on that port:
Navigate to control panel-> system and Security-> Windows Defender Firewall
Open Advanced Settings, select Inbound Rulesthen right click on it and then select New Rule
- Select Port, hit next, input the port you used (in my case
9595), hit next, select allow the connections - hit next again then give it a name and hit next for the last time.
Now find the ip address of your PC.
- Open Command Promtas adminstrator and run
ipconfigcommand. - You may find more than one ip addresses. As I'm connected through wifi I took the one under Wireless LAN adapter WiFi. In my case it was
192.168.0.100 - Note that this ip may change when you reconnect to the network. So you need to check it again then.
- Open Command Promtas adminstrator and run
Now from another device (pc, mobile, tablet etc.) connected to the same network go to
ip_address:YOUR_PORT(in my case192.168.0.100:9595)Hopefully you'll be good to go !
ALLOWED_HOSTS = ['*']在 djangosettings.py文件中添加- 运行 django 服务器
python manage.py 0.0.0.0:YOUR_PORT。我用作9595我的端口。 使防火墙允许访问该端口:
导航到控制面板->系统和安全-> Windows Defender 防火墙
打开Advanced Settings,选择Inbound Rules然后右键单击它,然后选择New Rule
- 选择端口,点击下一步,输入你使用的端口(在我的例子中
9595),点击下一步,选择允许连接 - 再次点击下一个,然后给它一个名字,最后一次点击下一个。
现在找到您PC的IP地址。
- 以管理员身份打开命令
ipconfig提示符并运行命令。 - 您可能会发现多个 ip 地址。当我通过 wifi 连接时,我在无线 LAN 适配器 WiFi下拿了一个。就我而言,它是
192.168.0.100 - 请注意,当您重新连接到网络时,此 ip 可能会更改。所以你需要再次检查它。
- 以管理员身份打开命令
现在从连接到同一网络的另一台设备(PC、手机、平板电脑等)转到
ip_address:YOUR_PORT(在我的情况下192.168.0.100:9595)希望你一切顺利!
回答by Jayas P Jacob
Try this python manage.py runserver then connect both tablet and system to same wifi and browse in the address eg: python manage.py runserver 192.168.0.100:8000 In tablet type that url in adress bar
试试这个 python manage.py runserver 然后将平板电脑和系统连接到同一个 wifi 并浏览地址,例如:python manage.py runserver 192.168.0.100:8000 在平板电脑中输入地址栏中的 url

