当前位置:网站首页>H5如何获取内网IP和公网IP
H5如何获取内网IP和公网IP
2022-07-17 05:05:00 【昔日_少年】
前端H5开发,获取内网IP和公网IP
可直接打开,简答便捷,重要的事情说三遍
直接使用!
直接使用!
直接使用!
获取内网IP
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<ul>
<li>内网ip</li>
</ul>
<script> function getIPs(callback) {
var ip_dups = {
}; var RTCPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection; var useWebKit = !!window.webkitRTCPeerConnection; if (!RTCPeerConnection) {
var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.sandbox = 'allow-same-origin'; iframe.addEventListener("DOMNodeInserted", function(e) {
e.stopPropagation(); }, false); iframe.addEventListener("DOMNodeInsertedIntoDocument", function(e) {
e.stopPropagation(); }, false); document.body.appendChild(iframe); var win = iframe.contentWindow; RTCPeerConnection = win.RTCPeerConnection || win.mozRTCPeerConnection || win.webkitRTCPeerConnection; useWebKit = !!win.webkitRTCPeerConnection; } var mediaConstraints = {
optional: [{
RtpDataChannels: true }] }; var servers = undefined; if (useWebKit) servers = {
iceServers: [{
urls: "stun:stun.services.mozilla.com" }] }; var pc = new RTCPeerConnection(servers, mediaConstraints); function handleCandidate(candidate) {
var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ var ip_addr = ip_regex.exec(candidate)[1]; if (ip_dups[ip_addr] === undefined) callback(ip_addr); ip_dups[ip_addr] = true; } pc.onicecandidate = function(ice) {
if (ice.candidate) handleCandidate(ice.candidate.candidate); }; pc.createDataChannel(""); pc.createOffer(function(result) {
pc.setLocalDescription(result, function() {
}, function() {
}); }, function() {
}); setTimeout(function() {
var lines = pc.localDescription.sdp.split('\n'); lines.forEach(function(line) {
if (line.indexOf('a=candidate:') === 0) handleCandidate(line); }); }, 1000); } getIPs(function(ip) {
var li = document.createElement("li"); li.textContent = ip; if (ip.match(/^(192\.168\.|169\.254\.|10\.|172\.(1[6-9]|2\d|3[01]))/)) {
//内网ip document.getElementsByTagName("ul")[0].appendChild(li); } }); </script>
</body>
</html>
获取公网IP
通常我们使用搜狐的接口
<!DOCTYPE html>
<html>
<head>
<title>公网ip获取</title>
<meta http-equiv=Content-Type content="text/html; charset=gb2312">
</head>
<body>
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript"> document.write(returnCitySN["cip"] + ',' + returnCitySN["cname"]) </script>
</body>
</html>
边栏推荐
- 微信小程序状态栏
- Travel data acquisition, data analysis and data mining [2022.5.30]
- 02_ Movie recommendation (contentbased)_ User portrait
- Install MySQL
- 【LeetCode——编程能力入门第一天】基本数据类型[在区间范围内统计奇数数目/去掉最低工资和最高工资后的工资平均值)
- 机器学习之PCA特征降维+案例实践
- 模拟库函数
- 轮播图移动速度(匀速,缓动)案例归总
- Feature extraction of machine learning (digitization and discretization of category features and digitization of text features)
- Bi design: distributed high concurrency epidemic prevention health management system based on vue+socket+redis
猜你喜欢
基于cuda10.0的pytorch深度学习环境配置
PyGame installation -requirement already satisfied
读论文《Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detec》
Email (including attachments, Netease, QQ)
Word2Vec原理及应用与文章相似度(推荐系统方法)
泰迪杯A题完整版 优化更新(4/23)
ThreadLocal thread safety example and its principle
645. 错误的集合
pygame安装-Requirement already satisfied问题
mysql数据库实验实训5,数据查询yggl数据库查询(详细)
随机推荐
Logic of image uploading
mysql数据库实验实训5,数据查询yggl数据库查询(详细)
ArcGIS Pro发布服务
学习C语言的第6天
PCA feature dimensionality reduction of machine learning + case practice
实习项目1-个性化主页配置
Installation and fast use of Mongo DB stand-alone version
[batch] batch delete intermediate folder - personal research script
Getting started with harmonios
【p5.js】模拟烟花效果-交互媒体设计作业
【C语言—零基础第十课】数组王国奇遇记
Fanoutexchange switch is simple to use
【C】 Beam calculator
uni-app 条件编译#ifdef #endif 兼容多个终端
微信小程序云开发使用方法-1
第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)
这么6的刷题网站你不会没听说过吧?你已经out 了?
卷积神经网络
小程序云开发表单提交并在页面中获取数据
C语言 带你 手撕 通讯录