触动精灵 获取外网IP

版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn.net/w18756901575 https://blog.csdn.net/w18756901575/article/details/77645577

一开始我获取获取外网IP,是用的是触动文档里提供的方法:

--获取外网ip地址
local sz = require("sz")
local http = require("szocket.http")
local res, code = http.request("http://www.ip.cn/");--如果此网址无反应,请尝试替换为 http://1212.ip138.com/ic.asp 或其他网址
if code == 200 then
    local i,j = string.find(res, "%d+%.%d+%.%d+%.%d+")
    local ipaddr =string.sub(res,i,j)
    dialog(ipaddr,0)
end

但是有次目标网站访问失败,就会使IP地址获取失败,过于依赖目标网址,所以我又新找到一个方法去获取IP地址:

--获取外网ip
function getIP()
    os.execute("curl ifconfig.me > /var/mobile/ip.txt ")
    local ip= readFileString("/var/mobile/ip.txt")
    if ip~=nil and ip~="" then
        ip= string.gsub(ip,"\\s","")
        local i,j = string.find(ip, "%d+%.%d+%.%d+%.%d+")
        ip =string.sub(ip,i,j)
        nLog("获取到的ip地址为:"..ip)
    else
        nLog("未获取到ip")
        if ip==nil then
            ip=""
        end
    end
    return ip
end

iOS是基于Linux系统,所以使用Linux的命令来获取外网IP,然后输出到文件中,读取文件来获取IP地址.
但是看到网上的一些信息ifconfig.me好像还会挂? !-_-,如果使用上面的方法还是获取不到,可以尝试使用curl ipip.net,curl ip.cn, curl cip.cc, curl myip.ipip.net等多个地址
其他获取IP地址的信息了来自:http://blog.csdn.net/orangleliu/article/details/51994513


2017/09/18

有次我通过上面的方式去获取IP地址的信息,但是没有获取到,查了下,好像是命令的原因,结果输出到文件中,但是里面的内容是空的,而且速度很快不像是进行了网络访问的样子.不知道是不是某些iOS的设备或者是版本不支持的原因,没太深究,将其换到了自己的服务器上,写了一个接口可以返回ip地址信息的.算是一个补充吧.

在服务器可以获取到客户端的IP地址.然后在客户端访问的时候将结果输出出来就可以了


2018/07/06

如果上面的方法都行不通

百度搜索ip有很多网站提供查看ip的功能,去用http访问这些网站,然后拿到结果之后解析出其中的ip地址就OK了

猜你喜欢

转载自blog.csdn.net/w18756901575/article/details/77645577