python获取外网ip地址的方法总结

本文实例总结了python获取外网ip地址的方法。分享给大家供大家参考。具体如下:

一、利用脚本引擎库直接获取

import console;
import web.script
import inet.http;
var jsvm = web.script(“javascript”)
jsvm.addcode( inet.http().get(“http://fw.qq.com/ipaddress”) )
var ipaddr = jsvm.codeobject.ipdata[0];
console.log( “您的外网ip地址:”,ipaddr )

二、http库,模式匹配获取

import inet.http;
getip = function(){
var http = inet.http()
http.flags = 0x80000000/*_internet_flag_reload强制文件从服务器下载不是缓存*/
| 0x4000000/*_internet_flag_dont_cache*不缓存数据*/
var str = http.get(“http://fw.qq.com/ipaddress”)
return str?string.match(str,'”(.+?)”‘)
}
io.open()
io.print(getip())

三、whttp库,模式匹配获取

//获取ip
import inet.whttp;
getip = function(){
var whttp = inet.whttp()
var str = whttp.get(“http://www.ip138.com/ip2city.asp? r=”+tonumber(time.now()))
whttp.close()
return str?string.match(str,”\[(.*?)\]”);
}
io.open()
io.print(getip())

四、api方法

io.open();
var iphlpapi = raw.loaddll(“iphlpapi.dll”)
var getipaddrtable = iphlpapi.api(“getipaddrtable”,”int(struct &pipaddrtable,int &pdwsize,int border)”)
ipinfo = class {
int dwaddr;
int dwindex;
int dwmask;
int dwbcastaddr;
int dwreasmsize;
word unused1;
word unused2
}
var pmib_ipaddrtable = class {
int dentrys;
struct mipinfo[255] = { ..ipinfo() };
}
var ipstruct = pmib_ipaddrtable();
var re, ipstruct, ret = getipaddrtable(ipstruct, 0, 1);
var re, ipstruct, ret = getipaddrtable(ipstruct, re, 0);
var ipdata = ipstruct.mipinfo[ipstruct.dentrys – 1].dwaddr;
var ip = raw.convert({ int n = ipdata }, { byte data[4] });
io.print( string.format(“%d.%d.%d.%d”, ip.data[1], ip.data[2], ip.data[3], ip.data[4]) );

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类

发表评论