NODEJS IP歸屬地查詢類

來源:互聯網
上載者:User
 1 /** 2 擷取IP資訊 歸屬地 ISP 3 */ 4 IPInfo = function(ip, callback) 5 { 6     this.http = require('http'); 7 } 8  9 IPInfo.prototype.log = function(str)10 {11     console.log(str);12 }13 14 IPInfo.prototype.GetUrl = function(ip)15 {16     var url = '/iplookup/iplookup.php?format=json&ip=' + ip;17     return url;18 }19 20 IPInfo.prototype.GetHost = function()21 {22     var host = 'int.dpool.sina.com.cn';23     return host;24 }25 26 IPInfo.prototype.GetInfo = function(ip, callback)27 {28     var options = {29         host:        this.GetHost(),30         port:        null,31         path:        this.GetUrl(ip)32     }33     34     var curtThis = this;35     var req = this.http.get(options, function(res){36         res.setEncoding('utf8');37         var data = "";38         res.on('data', function (chunk) {39             data += chunk;40         });41         42         res.on("end", function() {43             callback(data);44             //curtThis.log(data);45         });46     });47     48     req.on('error', function(e) {49         callback(null, e);50         //curtThis.log("error");51     });52     53     req.end();54 }55 56 57 //example58 /*59 var test = new IPInfo();60 61 var info = ["221.8.120.56","221.8.120.56","123.172.68.122"];62 63 for(var i=0;i<3;i++)64 {65 test.GetInfo(info[i], function(data, err){66     if(err){console.log(err)}67     else68         console.log(data);69 });70 }71 */

用的新浪的介面查詢的IP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.