根據IP位址查詢所在地

2022-04-08 14:24:49 字數 2906 閱讀 9233

using system;

using system.collections.generic;

using system.text;

using system.io;

using system.text.regularexpressions;

namespace aimscommon

#endregion

#region 公共屬性

public string datapath

}public string ip

}public string country

}public string local

}public string errmsg

}#endregion

#region 搜尋匹配資料

private int qqwry()

)|(1\d)|(2[0-4]\d)|(25[0-5]))\.)((\d)|(1\d)|(2[0-4]\d)|(25[0-5]))";

regex objre = new regex(pattern);

match objma = objre.match(ip);

if (!objma.success)

long ip_int = this.iptoint(ip);

int nret = 0;

if (ip_int >= iptoint("127.0.0.0") && ip_int <= iptoint("127.255.255.255"))

else if ((ip_int >= iptoint("0.0.0.0") && ip_int <= iptoint("2.255.255.255")) || (ip_int >= iptoint("64.0.0.0") && ip_int <= iptoint("126.255.255.255")) || (ip_int >= iptoint("58.0.0.0") && ip_int <= iptoint("60.255.255.255")))

objfs = new filestream(this.datapath, filemode.open, fileaccess.read);

trylong range = recordcount;

long rangb = 0;

long recno = 0;

while (rangb < range - 1)

if (ip_int > this.startip)

rangb = recno;

else

range = recno;

}this.getstartip(rangb);

this.getendip();

if (this.startip <= ip_int && this.endip >= ip_int)

else

objfs.close();

return nret;

}catch

}#endregion

#region ip位址轉換成int資料

private long iptoint(string ip)

;string iparr = ip.split(dot);

if (iparr.length == 3)

ip = ip + ".0";

iparr = ip.split(dot);

long ip_int = 0;

long p1 = long.parse(iparr[0]) * 256 * 256 * 256;

long p2 = long.parse(iparr[1]) * 256 * 256;

long p3 = long.parse(iparr[2]) * 256;

long p4 = long.parse(iparr[3]);

ip_int = p1 + p2 + p3 + p4;

return ip_int;

}#endregion

#region int轉換成ip

private string inttoip(long ip_int)

#endregion

#region 獲取起始ip範圍

private long getstartip(long recno)

#endregion

#region 獲取結束ip

private long getendip()

#endregion

#region 獲取國家/區域偏移量

private string getcountry()

return " ";

}#endregion

#region 獲取國家/區域字串

private string getflagstr(long offset)

offset = convert.toint64(buff[0].tostring()) + convert.toint64(buff[1].tostring()) * 256 + convert.toint64(buff[2].tostring()) * 256 * 256;

}else

}if (offset < 12)

return " ";

objfs.position = offset;

return getstr();

}#endregion

#region getstr

private string getstr()

else

}return str;

}#endregion

#region 獲取ip位址

public string iplocation()

public string iplocation(string datapath, string ip)

#endregion

}}

PHP獲取IP所在地位址

原理簡述 利用 ip.taobao.com 提供的乙個功能,先獲取 json 格式字串,再解碼 json 字串新建檔案getaddress.class.php classss getaddress 獲取ip所在地位址函式 param ip p位址,預設為本機ip function getipfrom...

根據使用者IP獲取其所在地

預定義變數 server的用法中,我們可以發現引數 remote addr 的作用是 瀏覽當前頁面的使用者的 ip 位址。根據這個我們可以獲取客戶端的ip,根據獲取的ip我們又可以進一步獲取對方的所在地等其他資訊。這一節主要是了解一下如何獲取客戶端的ip位址,並定位其所在地。我們先看看 實現 將 部...

Python根據IP查詢所在地理位置與運營商

python根據ip查詢所在地理位置與運營商 usr bin env python encoding utf8 author linuxhub.org 根據ip位址查詢出ip所在的地理位置 def get ip info ip import requests ip位址庫介面 r requests.g...