今天在上網頁課程的時候,老師為了讓我們用手機可以直接看 Demo,老師就使用 QRCode 分享了一個 .local 的 Domain 讓我們連進去用手機看,在下課的時候我同事就跟我說覺得很神奇為什麼可以這樣做 Demo 感覺好方便,我就解釋了一下它的原理。
它的原理其實很簡單,當別人輸入了 .local 的 Domain(假設我的 Domain 是 clarence.local 舉例),他的電腦就會在內網裡面用 224.0.0.251:5353 UDP 廣播問 clarence.local 是誰啊?這時候我的電腦收到就會再用同樣的方法發送到 224.0.0.251:5353 說明我的 IP 是多少,如此內網內所有的機器都會知道這個 Domain 就是你,就可以用來做 HTTP 網頁的功能之類的,是不是很方便啊 ~
這個 Domain 的設定方法其實在 MAC 裡面的系統偏好設定 > 共享 > 編輯,可以看到它
在這邊做了一個 Demo 讓大家了解 mDNS 的用法
- 用 Python 開啟一個 HTTP Server,此指令會直接以這個目錄開啟一個 HTTP Server
- 使用 Curl Get 上面開啟的 HTTP Server 服務網站,並且使用上面提到的 mDNS Domain,當然這個位置是可以直接分享給同區網的人的呦 ~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clarence~: python -m SimpleHTTPServer 80 | |
clarence~: curl http://clarence.local/ | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html> | |
<title>Directory listing for /</title> | |
<body> | |
<h2>Directory listing for /</h2> | |
<hr> | |
<ul> | |
</ul> | |
<hr> | |
</body> | |
</html> |
大家未來想要在內網 Demo 東西又覺得直接給 IP 很麻煩的時候就可以用這個方法呦 ~
《AWS CDK 完全學習手冊:打造雲端基礎架構程式碼 IaC》
第 12 屆 iT 邦幫忙鐵人賽 DevOps 組冠的《用 CDK 定 義 AWS 架構》
第 11 屆 iT 邦幫忙鐵人賽《LINE bot 好好玩 30 天玩轉 LINE API》
一個熱愛分享的雲端工程師!