前提准备
查询当前机器的公网ip,一个简单命令就可搞定
curl ipinfo.io
返回结果如下
{"ip": "58.247.218.68","city": "Shanghai","region": "Shanghai","country": "CN","loc": "31.2222,121.4571","org": "AS17421 China Unicom Shanghai network","postal": "200000","timezone": "Asia/Shanghai","readme": "https://ipinfo.io/missingauth"
}
将该网址使用jsonp嵌入到前端网页
<!DOCTYPE html>
<html lang="zh-CN"><head><meta charset="UTF-8"><title>JSONP 获取本机公网 IP</title>
</head><body><h2>通过 JSONP 获取公网 IP</h2><div id="result">正在获取...</div><script>// JSONP 回调函数function showIP(data) {document.getElementById("result").innerText ="你的公网 IP 是:" + data.ip;console.log("完整信息:", data);}</script><!-- JSONP 请求 --><script src="https://ipinfo.io?callback=showIP"></script>
</body></html>
把上面网页,命名为 index.html,运行如下命令打开。(或者使用)
python3 -m http.server 8080
