Convert an IP address to a Country Location

9 ビュー (過去 30 日間)
Chris Williams
Chris Williams 2013 年 11 月 6 日
コメント済み: Cedric 2013 年 11 月 7 日
Is there a way to convert a series of IP addresses to country locations?

採用された回答

Cedric
Cedric 2013 年 11 月 6 日
編集済み: Cedric 2013 年 11 月 6 日
One way to do it is to use some online API for IP identification. Here is an example: save the following function into your working dir..
function country = getCountryByIP( ipv4 )
url = sprintf( 'http://www.geoplugin.net/json.gp?ip=%s', ipv4 ) ;
buf = urlread( url ) ;
country = regexp( buf, '(?<=countryName":")[^"]*', 'match', 'once' ) ;
end
You can then use it as follows in your command window (or loop over a list of IPs)..
>> getCountryByIP( '216.236.14.34' )
ans =
United States
Note that there are dozen of websites which provide APIs for doing this (geoplugin.net, whatismyip.com, ipinfodb.com, hostip.info, etc) and you might find one which accepts a list of IPs as input and outputs a list of countries.
These APIs set a limit on the number of queries that you can send/get per second, and some are free only for a limited number of queries per hour or per day. Also, sending a query and getting the return from the server takes time (could take a few seconds). If you had millions of IPs to look up, using an API would therefore not be the best option, and you should rather implement a solution based on a local database of IPs/countries (e.g. hostip.info rsync).

その他の回答 (1 件)

Chris Williams
Chris Williams 2013 年 11 月 6 日
Great, thanks for your help. The local database looks the way to go.
  1 件のコメント
Cedric
Cedric 2013 年 11 月 7 日
You're welcome.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall Web Services from MATLAB Using HTTP についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by