触动精灵 获取外网IP

来源:互联网 发布:粤数大数据公司 评价 编辑:程序博客网 时间:2024/06/10 08:52

一开始我获取获取外网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地址:

--获取外网ipfunction 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 ipend

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地址.然后在客户端访问的时候将结果输出出来就可以了

原创粉丝点击