Identifying country by IP address

前端 未结 13 898
盖世英雄少女心
盖世英雄少女心 2020-12-01 05:24

Is there a way to figure out the country name just by looking at an IP address? I mean, do countries have specific ranges of IP addresses? For example, Australia can have IP

相关标签:
13条回答
  • 2020-12-01 05:49

    You could use ipdata.co to perform the lookup

    This answer uses a 'test' API Key that is very limited and only meant for testing a few calls. Signup for your own Free API Key and get up to 1500 requests daily for development.

    curl https://api.ipdata.co/23.221.76.66?api-key=test
    

    Ipdata has 10 endpoints globally each able to handle >10,000 requests per second!

    Gives

    {
        "ip": "23.221.76.66",
        "city": "Cambridge",
        "region": "Massachusetts",
        "region_code": "MA",
        "country_name": "United States",
        "country_code": "US",
        "continent_name": "North America",
        "continent_code": "NA",
        "latitude": 42.3626,
        "longitude": -71.0843,
        "asn": "AS20940",
        "organisation": "Akamai International B.V.",
        "postal": "02142",
        "calling_code": "1",
        "flag": "https://ipdata.co/flags/us.png",
        "emoji_flag": "\ud83c\uddfa\ud83c\uddf8",
        "emoji_unicode": "U+1F1FA U+1F1F8",
        "is_eu": false,
        "languages": [
            {
                "name": "English",
                "native": "English"
            }
        ],
        "currency": {
            "name": "US Dollar",
            "code": "USD",
            "symbol": "$",
            "native": "$",
            "plural": "US dollars"
        },
        "time_zone": {
            "name": "America/New_York",
            "abbr": "EDT",
            "offset": "-0400",
            "is_dst": true,
            "current_time": "2018-04-19T06:32:30.690963-04:00"
        },
        "threat": {
            "is_tor": false,
            "is_proxy": false,
            "is_anonymous": false,
            "is_known_attacker": false,
            "is_known_abuser": false,
            "is_threat": false,
            "is_bogon": false
        }
    }⏎ 
    
    0 讨论(0)
  • 2020-12-01 05:50

    No you can't - IP addresses get reallocated and reassigned from time to time, so the mapping of IP to location will also change over time.

    If you want to find out the location that an IP address currently maps to you can either download a geolocation database, such as GeoLite from MaxMind, or use an API like http://ipinfo.io (my own service) which will also give you additional details:

    $ curl ipinfo.io/8.8.8.8
    {
      "ip": "8.8.8.8",
      "hostname": "google-public-dns-a.google.com",
      "loc": "37.385999999999996,-122.0838",
      "org": "AS15169 Google Inc.",
      "city": "Mountain View",
      "region": "California",
      "country": "US",
      "phone": 650
    }
    
    0 讨论(0)
  • 2020-12-01 05:54

    I think what you're looking for is an IP Geolocation database or service provider. There are many out there and some are free (get what you pay for).

    Although I haven't used this service before, it claims to be in real-time. https://kickfire.com/kf-api

    But just do a google search on IP geo and you'll get more results than you need.

    0 讨论(0)
  • 2020-12-01 05:54

    Amazon's CloudFront content delivery network can now be configured to pass this information through as a header. Given Amazon's size (they're big and stable, not going anywhere) and this is configuration over code (no third-party API to learn or code to maintain), all around believe this to be the best option.

    If you do not use AWS CloudFront, I'd look into seeing if your CDN has a similar header option that can be turned on. Usually the large providers are quick to push for feature parity. And if you are not using a CDN, you could put CloudFront in front of your infrastructure and simply set the origin to resolve to whatever you are currently using.

    Additionally, it also makes sense to resolve this at the CDN level. Your CDN is already having to figure out geo location to route the user to the nearest content node, might as well pass this information along and not figure it out twice through a third party API (this becomes chokepoint for your app, waiting for a geo location lookup to resolve). No need to do this work twice (and the second time, arguably less resilient [e.g., 3rd party geo lookup]).

    https://aws.amazon.com/blogs/aws/enhanced-cloudfront-customization/

    Geo-Targeting – CloudFront will detect the user’s country of origin and pass along the county code to you in the CloudFront-Viewer-Country header. You can use this information to customize your responses without having to use URLs that are specific to each country.

    0 讨论(0)
  • 2020-12-01 05:55

    Yes, countries have specific IP address ranges as you mentioned.

    For example, Australia is between 16777216 - 16777471. China is between 16777472 - 16778239. But one country may have multiple ranges. For example, Australia also has this range between 16778240 - 16779263

    (These are numerical conversions of IP addresses. It depends whether you use IPv4 or IPv6)

    More information about these ranges can be seen here: http://software77.net/cidr-101.html

    We get the ip addresses of our website visitors and sometimes want to make relevant campaign for a specific country. We were using bulk conversion tools but later on decided to define the rules in an Excel file and convert it in the tool. And we have built this Excel template: https://www.someka.net/excel-template/ip-to-country-converter/

    Now we use this for our own needs and also sell it. I don't want it to be a sales pitch but for those who are looking for an easy solution can benefit from this.

    0 讨论(0)
  • 2020-12-01 05:58

    May be these two links can help you Associate IP addresses with countries

    http://en.wikipedia.org/wiki/Regional_Internet_Registry

    0 讨论(0)
提交回复
热议问题