geoip【go和C#】

在项目中经常遇到通过ip获取区域信息,geoip无疑是一个很好的选择https://dev.maxmind.com/geoip/, 大家在这里可以下载IP库 。还有在ip中经常遇到字符串和数字的转换

GO
ip数字和字符的转换我们可以用 github.com/thinkeridea/go-extend/exnet包,获取ip信息可以用github.com/oschwald/geoip2-golang,代码如下:

package main

import (
    "fmt"
    "log"
    "net"

    "github.com/oschwald/geoip2-golang"
    "github.com/thinkeridea/go-extend/exnet"
)

func main() {
    //n, _ := exnet.IPString2Long(ip)
    //s, _ := exnet.Long2IPString(ip)

    ip := "171.223.95.14"
    n, _ := exnet.IPString2Long(ip)
    fmt.Println(n)

    printIpInfo(ip)
}

func printIpInfo(ipstr string) {

    db, err := geoip2.Open("./GeoLite2-City.mmdb")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()

    ip := net.ParseIP(ipstr)
    record, err := db.City(ip)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Portuguese (BR) city name: %v\n", record.City.Names["zh-CN"])
    if len(record.Subdivisions) > 0 {
        fmt.Printf("English subdivision name: %v\n", record.Subdivisions[0].Names["zh-CN"])
    }

    fmt.Printf("Russian country name: %v\n", record.Country.Names["zh-CN"])
    fmt.Printf("Russian Continent name: %v\n", record.Continent.Names["zh-CN"])

    fmt.Printf("ISO country code: %v\n", record.Country.IsoCode)
    fmt.Printf("Time zone: %v\n", record.Location.TimeZone)
    fmt.Printf("Coordinates: %v, %v\n", record.Location.Latitude, record.Location.Longitude)
}

运行结果:

D:\Project\GoProject\src\main>go run main.go
2883542798
Portuguese (BR) city name: 成都
English subdivision name: 四川省
Russian country name: 中国
Russian Continent name: 亚洲
ISO country code: CN
Time zone: Asia/Shanghai
Coordinates: 30.6667, 104.0667

C#

需要添加相应的包 dotnet add package MaxMind.GeoIP2 --version 4.0.1 代码如下:

namespace geoip2
{
    using MaxMind.GeoIP2;
    using System;
    using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            long a = IpToInt("171.223.95.14");
            Console.WriteLine(a);
            Console.WriteLine(IntToIp(a));
            using (var reader = new DatabaseReader("GeoLite2-City.mmdb"))
            {
                var record = reader.City("171.223.95.14");

                Console.WriteLine("Portuguese (BR) city name:{0}", record.City.Names["zh-CN"]);
                if(record.Subdivisions!=null && record.Subdivisions.Count>0)  {
                   Console.WriteLine("English subdivision name:{0}", record.Subdivisions[0].Names["zh-CN"]);
                }
                Console.WriteLine("Russian country name:{0}", record.Country.Names["zh-CN"]);
                Console.WriteLine("Russian Continent name:{0}", record.Continent.Names["zh-CN"]);
                Console.WriteLine("ISO country code: {0}", record.Country.IsoCode);
                Console.WriteLine("Time zone:{0}", record.Location.TimeZone);
                Console.WriteLine("Coordinates:{0}, {1}", record.Location.Latitude, record.Location.Longitude);
            }        
        }
         static long IpToInt(string ip)
        {
            char[] separator = new char[] { '.' };
            string[] items = ip.Split(separator);
            return long.Parse(items[0]) << 24
                    | long.Parse(items[1]) << 16
                    | long.Parse(items[2]) << 8
                    | long.Parse(items[3]);
        }
         static string IntToIp(long ipInt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((ipInt >> 24) & 0xFF).Append(".");
            sb.Append((ipInt >> 16) & 0xFF).Append(".");
            sb.Append((ipInt >> 8) & 0xFF).Append(".");
            sb.Append(ipInt & 0xFF);
            return sb.ToString();
        }

    }
}

运行结果:

 

posted on 2021-02-26 14:25  dz45693  阅读(2230)  评论(0编辑  收藏  举报

导航