python獲取外網(wǎng)ip地址的方法總結(jié)
更新時間:2015年07月02日 15:26:36 作者:不吃皮蛋
這篇文章主要介紹了python獲取外網(wǎng)ip地址的方法,實例總結(jié)了四種常用的獲取外網(wǎng)IP地址的技巧,需要的朋友可以參考下
本文實例總結(jié)了python獲取外網(wǎng)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( "您的外網(wǎng)IP地址:",ipAddr )
二、http庫,模式匹配獲取
import inet.http;
getIp = function(){
var http = inet.http()
http.flags = 0x80000000/*_INTERNET_FLAG_RELOAD強制文件從服務(wù)器下載不是緩存*/
| 0x4000000/*_INTERNET_FLAG_DONT_CACHE*不緩存數(shù)據(jù)*/
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程序設(shè)計有所幫助。
相關(guān)文章
python+opencv3.4.0 實現(xiàn)HOG+SVM行人檢測的示例代碼
這篇文章主要介紹了python+opencv3.4.0 實現(xiàn)HOG+SVM行人檢測的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
python非單一.py文件用Pyinstaller打包發(fā)布成exe
第一次將自己做的python爬蟲項目打包成exe,所以留個筆記,本文詳細的介紹了python非單一.py文件用Pyinstaller打包發(fā)布成exe,具有一定的參考價值,感興趣的可以了解一下2022-03-03
Python集成開發(fā)環(huán)境Pycharm的使用及技巧
本文詳細講解了Python集成開發(fā)環(huán)境Pycharm的使用及技巧,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python摳圖教程之使用OpenCV實現(xiàn)背景去除
這篇文章主要給大家介紹了關(guān)于Python摳圖教程之使用OpenCV實現(xiàn)背景去除的相關(guān)資料,背景去除是在很多視覺應(yīng)用里的主要預(yù)處理步驟,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-10-10

