在 mac 上运行的本地主机.. 我可以在我的 Android 手机上查看吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11005540/
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
Localhost running on mac.. Can I view it on my Android phone?
提问by Mark Steggles
Running a ruby on rails project on my mac. I need to test it on my android phone. Is there a way to view my mac localhost on my android phone?
在我的 Mac 上运行 ruby on rails 项目。我需要在我的安卓手机上测试它。有没有办法在我的安卓手机上查看我的 mac localhost?
回答by Chuck
The name "localhost" is just a loopback to your own computer. To access it on your Android, you need to find the computer's IP address.
名称“localhost”只是到您自己计算机的环回。要在您的 Android 上访问它,您需要找到计算机的 IP 地址。
The most general way of finding this info, which works on both Mac and Linux, is to go into the Terminal and type ifconfig
. Look for either "en0" or "en1", and under that entry look for the "inet" listing. It will be something along the lines of "192.168.1.100". When you find that address, that's what you'll want to put in your browser's address bar.
查找此信息的最通用方法(适用于 Mac 和 Linux)是进入终端并键入ifconfig
. 查找“en0”或“en1”,然后在该条目下查找“inet”列表。它将类似于“192.168.1.100”。当您找到该地址时,这就是您想要放入浏览器地址栏中的内容。
(On a Mac specifically, you can go to the Sharing pane in System Preferences and it'll tell you there.)
(特别是在 Mac 上,您可以转到“系统偏好设置”中的“共享”面板,它会在那里告诉您。)
回答by user1713964
Here's a quick to-do to have your localhost available for tests on other devices :
这是让您的本地主机可用于在其他设备上进行测试的快速操作:
1) identify the IP of your Android : select the Wi-Fi you're connected (the same as the one the Mac is on), you'll have the IP detailed on it). For the example : we suppose your Android IP is : 192.168.0.10
1) 确定您的 Android 的 IP:选择您连接的 Wi-Fi(与 Mac 所在的 Wi-Fi 相同),您将在其上详细了解 IP)。例如:我们假设您的 Android IP 是:192.168.0.10
2) open a Shell on your Mac and edit your host :
2) 在 Mac 上打开 Shell 并编辑您的主机:
sudo nano /etc/hosts
3) edit the file as this :
3)编辑文件如下:
127.0.0.1 192.168.0.10
4) Identify your Mac IP : (as @Chuck perfectly explained) (in your shell) :
4) 识别您的 Mac IP :(正如@Chuck 完美解释的那样)(在您的外壳中):
ifconfig
5) Open your favourite webbrowser you use on your phone and connect to your Mac IP (with the port if needed) with directly something like :
5)打开您在手机上使用的最喜欢的网络浏览器,并直接使用以下内容连接到您的 Mac IP(如果需要,请使用端口):
http://192.168.x.x:8000/
6) Enjoy your test :)
6)享受你的测试:)
Notice you can do that for every support connected on your Wi-Fi.
请注意,您可以为 Wi-Fi 上连接的每个支持执行此操作。
回答by Bjarni Rúnar
Chunk's answer is correct, assuming your mobile device and your computer are on the same network. However, if you want your localhost server to be visible to the wider Internet (e.g. for testing over 3G, developing webhooks or collaboration with a remote colleague/client), more is needed as local addresses (starting with 10.* or 192.168.*) are not visible to the wider Internet.
假设您的移动设备和您的计算机在同一网络上,Chunk 的答案是正确的。但是,如果您希望您的 localhost 服务器对更广泛的 Internet 可见(例如,用于测试 3G、开发 webhooks 或与远程同事/客户端协作),则需要更多本地地址(以 10.* 或 192.168.* 开头) ) 对更广泛的互联网不可见。
The traditional solution to that is port forwarding and dynamic DNS, but lately a few services have popped up which aim to make this process simpler (disclaimer: I am the author of one of them, PageKite).
传统的解决方案是端口转发和动态 DNS,但最近出现了一些旨在使这个过程更简单的服务(免责声明:我是其中之一的作者,PageKite)。
These services provide you with a public DNS name and software which connect your "localhost" with an in-the-cloud relay server (a.k.a. a reverse proxy). For example, if you are using PageKite, you can run the following command in the terminal:
这些服务为您提供公共 DNS 名称和软件,将您的“本地主机”与云端中继服务器(又名反向代理)连接起来。例如,如果您使用的是 PageKite,则可以在终端中运行以下命令:
$ pagekite.py 80 yourname.pagekite.me
$ pagekite.py 80 你的名字.pagekite.me
... to create a mapping from http://yourname.pagekite.me/
to the web server running on http://localhost:80
. While the program is running, your localhost site will be visible to the rest of the Internet. In order to make it private again, you simply turn off the pagekite.py connector program.
... 创建从http://yourname.pagekite.me/
到运行在 上的 Web 服务器的映射http://localhost:80
。当程序运行时,您的本地主机站点将对 Internet 的其余部分可见。为了使其再次私有,您只需关闭 pagekite.py 连接器程序。
For completeness, here are some of the localhost tunneling services I am aware of:
为了完整起见,以下是我所知道的一些本地主机隧道服务:
- PageKiteis Free Software (Python) with a "pay-what-you-want" on-line service. You can create as many long-lived subdomains as you want, a wild-card SSL certificate is included and front-end relays run in multiple geographic locations to provide redundancy and responsiveness.
- Localtunnelis a free-of-charge (sponsored by Twilio) Ruby solution which gives connections temporary names. Note that names are recycled so you may see unexpected traffic while the connection is live.
- Showoff.io and Tunnlr.com are proprietary paid service comparable to Localtunnel, based on the same basic technology (ssh tunnels).
- PageKite是免费软件 (Python),具有“按需付费”的在线服务。您可以根据需要创建任意多个长期存在的子域,包括通配符 SSL 证书,并且前端中继在多个地理位置运行以提供冗余和响应能力。
- Localtunnel是一个免费的(由 Twilio 赞助)Ruby 解决方案,它为连接提供临时名称。请注意,名称会被回收,因此您可能会在连接有效时看到意外流量。
- Showoff.io 和 Tunnlr.com 是可与 Localtunnel 相媲美的专有付费服务,基于相同的基本技术(ssh 隧道)。
(Sorry about not linking to the last two, SO spam protection is preventing me from being fair to my competitors. ;-)
(抱歉没有链接到最后两个,所以垃圾邮件保护阻止我对我的竞争对手公平。;-)
回答by rassom
Found this great, free tool today - reallyeasy to set up and works like a charm! Versions for Mac OS, Linux and Windows also available.
今天发现了这个很棒的免费工具 -真的很容易设置,而且工作起来很有魅力!还提供适用于 Mac OS、Linux 和 Windows 的版本。
(am not associated with it in any way)
(我与它没有任何关联)
回答by Simmi Badhan
This worked for me for accessing rails server with IP over local network:
这对我通过本地网络使用 IP 访问 rails 服务器有用:
- The firewall has to be turned off.
/etc/hosts should have this entry:
127.0.0.1 192.168.100.12
where 192.168.100.12 is the ip address which can be found by ifconfig command in terminal.
Start rails server with this command:
rails server -b 0.0.0.0 -p 8080
- 必须关闭防火墙。
/etc/hosts 应该有这个条目:
127.0.0.1 192.168.100.12
其中 192.168.100.12 是可以通过终端中的 ifconfig 命令找到的 IP 地址。
使用以下命令启动 rails 服务器:
rails server -b 0.0.0.0 -p 8080
I was able to access my localhost through http://192.168.100.12:8080/
我能够通过http://192.168.100.12:8080/访问我的本地主机
回答by yaircarreno
Additionally, if you want to test on Android/iOS device a PWA Appsdeveloped with Angular, you will need to use:
此外,如果您想在 Android/iOS 设备上测试使用Angular开发的PWA 应用程序,您将需要使用:
ng serve --host 0.0.0.0
to start up the server CLI.
ng serve --host 0.0.0.0
启动服务器 CLI。
If you receive "Invalid Host Header", uses:
如果您收到“无效的主机头”,请使用:
ng serve --host 0.0.0.0 --disableHostCheck true
ng serve --host 0.0.0.0 --disableHostCheck true
回答by Sebastian Scholl
after reading this thread (and the suggestions working!) I put together a single guide to solving this issue. This link to that guide that has screenshots for every step and where to look. Or the text is pasted below. Thank you for the help!
阅读此线程后(以及建议有效!),我整理了一份解决此问题的指南。该指南的此链接包含每个步骤和查看位置的屏幕截图。或者文字粘贴在下面。感谢您的帮助!
First off, both your phone and laptop must be connected to the same WiFi network. If you're using your phones mobile hotspot, it will still work. However, make sure to connect both devices to the same network before moving forward.
首先,您的手机和笔记本电脑必须连接到同一个 WiFi 网络。如果您使用的是手机移动热点,它仍然可以工作。但是,请确保在继续之前将两个设备连接到同一网络。
Next, collect your phones IP address. To do so, I use the Network Info II app. There is most definitely another way to accomplish this. However, Network Info II works as needed and is document for this tutorials sake.
接下来,收集您的电话 IP 地址。为此,我使用了 Network Info II 应用程序。绝对有另一种方法可以实现这一点。但是,Network Info II 可以根据需要工作,并且是本教程的文档。
Now open the terminal on your computer (don't worry about which directory you're within) and run the command sudo nano /etc/hosts/. After entering your system password, you'll see near the top of the terminal a string of numbers with the word localhost following. Localhost is in fact just an alias for your computer's own server address so that when you go to localhost in a browser it simply routes the http request to your local machine. In order to tie your phone into this loop, enter its IP address right between the string of numbers and Localhost.
现在在您的计算机上打开终端(不要担心您在哪个目录中)并运行命令 sudo nano /etc/hosts/。输入系统密码后,您将在终端顶部附近看到一串数字,后面跟着 localhost 一词。Localhost 实际上只是您计算机自己服务器地址的别名,因此当您在浏览器中访问 localhost 时,它只是将 http 请求路由到您的本地计算机。为了将您的手机绑定到此循环中,请在数字字符串和本地主机之间输入其 IP 地址。
To save this, hit control + X and then Y when prompted to save. After that, the enter key will bring you back to the standard command line.
要保存它,请在提示保存时点击 control + X 然后点击 Y。之后,回车键会将您带回标准命令行。
Open a second tab in the terminal and launch a local server. I've only tested this using a simple python server, which can be run by running in the terminal python -m SimpleHTTPServer 8000. However, I'm assuming that you can launch any local server you like, being that all you'll need to reference is the port number. If you do use the simple python server, that port number is 8000.
在终端中打开第二个选项卡并启动本地服务器。我只使用一个简单的 python 服务器对此进行了测试,该服务器可以通过在终端 python -m SimpleHTTPServer 8000 中运行来运行。但是,我假设您可以启动任何您喜欢的本地服务器,这就是您所需要的to reference 是端口号。如果您确实使用简单的 python 服务器,则该端口号为 8000。
Run ifconfig in the terminal. This will bring up a slew of information, to which you should scroll about halfway down. What you are looking for is a string of numbers that follow after an inet and before netmask within either the en0: or en1: key.
在终端中运行 ifconfig。这将显示大量信息,您应该向下滚动大约一半。您要查找的是 en0: 或 en1: 键中 inet 之后和网络掩码之前的一串数字。
Done! On your Android, open up a browser and visit the inet number, followed by a colon (:) and the port number.
完毕!在您的 Android 上,打开浏览器并访问 inet 号,后跟一个冒号 (:) 和端口号。
回答by best wishes
Although one answer has been accepted but this worked for me:
虽然一个答案已被接受,但这对我有用:
- Make sure both your computer and android device are on same network.
- get your computer's ip address. (in case of mac type
ifconfig
inside terminal and look foren0/en1 -> inet
it should be something like19*.16*.1.4
.) Open your mobile settings and go to network-> long press your wifi network-> modify network -> scroll down to advanced options -> chose proxy manual and in proxy hostname type your address i.e.
19*.16*.1.4
inside port enter your port. and save it.search for
what is my ip
in google chrome, and get your ip, lets say it is1**.1**.15*.2**
- try accessing
1**.1**.15*.2**:port/
from your mobile device.
- 确保您的计算机和 android 设备在同一网络上。
- 获取你电脑的IP地址。(如果
ifconfig
终端内的 mac 类型并查找en0/en1 -> inet
它应该是这样的19*.16*.1.4
。) 打开您的移动设置并转到网络-> 长按您的wifi 网络-> 修改网络-> 向下滚动到高级选项-> 选择代理手册并在代理主机名中输入您的地址,即
19*.16*.1.4
在端口内输入您的端口。并保存它。what is my ip
在谷歌浏览器中搜索,并获取您的 ip,假设它是1**.1**.15*.2**
- 尝试
1**.1**.15*.2**:port/
从您的移动设备访问 。
It is as simple as this.
就这么简单。
回答by nb2998
As mentioned in other answers, url has to consist of the IP address of computer and not localhost. If that isn't working, before trying anything else, check if the port is correctly mentioned along with IP address when making an api call in the Android app. That is, you should be making an api call to a url which is of the format: http://192.168.X.X:80/api/..
正如其他答案中提到的,url 必须由计算机的 IP 地址组成,而不是本地主机。如果这不起作用,在尝试其他任何操作之前,请检查在 Android 应用程序中进行 api 调用时是否正确提及了端口和 IP 地址。也就是说,您应该对以下格式的 url 进行 api 调用:http://192.168.X.X:80/api/..
回答by vlodko
MacOS Catalina 10.15.4
MacOS Catalina 10.15.4
- Go to Settings -> Security and Privacy.
- 转到设置 -> 安全和隐私。
- Select Firewalltab and unlock the settings using your laptop password:
- 选择防火墙选项卡并使用您的笔记本电脑密码解锁设置:
- Select Firewall Options...and in the dialog uncheck the box for Block all incoming connectionsand hit OK.
- 选择防火墙选项...并在对话框中取消选中阻止所有传入连接框并点击确定。
- Open Terminal and type ifconfig. Look for the en0. In the section find the address sitting near inet- this is the address of your laptop in a local network.
- 打开终端并输入 ifconfig。寻找en0。在该部分中找到位于inet附近的地址- 这是您的笔记本电脑在本地网络中的地址。
en0: flags0=<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 0
...
inet 192.168.0.123 netmask 0xffffff00 broadcast 192.168.0.255
...
- Use the IP address + the port (in case you are using localhost:4200 for development), otherwise omit the port:
- 使用 IP 地址 + 端口(如果您使用 localhost:4200 进行开发),否则省略端口:
http://<IP address found in en0 -> inet>:<port if needed>
- Profit!
- 利润!