Using GEOIP database to resolve IP to City Jsonrpc service tutorials

Source: Internet
Author: User
Tags geoip geoip database
This is a creation in Article, where the information may have evolved or changed. # # # RESTful interface * * Request url:**-' https://api.turboes.com/Tbsapi/v1/ip2addr?ip=219.140.227.235 ' * * Request method: **-GET * * Parameters: * * | parameter name | Type | description | |:-----|:-----|-----| | IP |url-qurey-string | ' Optional ' to query the IP address, if not pass this represents the current IP | * * Return Example * * "' {" code ": 1," data ": {" Country ":" China "," province ":" Hubei Province "," City ":" Wuhan "," ISP ":", "Latitude": 30.5801, "Long Itude ": 114.2734," TimeZone ":" Asia/shanghai "}," IP ":" 219.140.227.235 "}" > Json_rpc ' tcp ' Address: ' 121.40.238.123 ' ( IP address faster) ' api.turboes.com ' port: ' 3344 ' ***### third-party resource * [GeoIP2 Reader for Go] (https://github.com/oschwald/geoip2-golang) * [ GeoLite2 Open Source database] (https://dev.maxmind.com/zh-hans/geoip/geoip2/geolite2-%E5%BC%80%E6%BA%90%E6%95%B0%E6%8D%AE%E5% ba%93/) # # GO standard library JSONRPC server > Go official provides an RPC library: Net/rpc. Package RPC provides the ability to access an object through the network. The server needs to register the object, exposing the service through the object's type name. The output method of this object can be called remotely after registration, which encapsulates the details of the underlying transport, including serialization. The server can register multiple objects of different types, but there is an error when registering multiple objects of the same type. > * The type of method is output (the method ' s type is exported) > * methods themselves are also output (the is exported) > * method must be two parameters, must be the output type or the built-in type (The Method has two arguments, both exported or builtin types) > * The second parameter of the methods is the pointer type (the method ' s second argument is a pointer) > * Method return type is error (the way has return type error) ' Gopackage mainimport ("FMT" "Github.com/oschwald/geoip2-golang" " NET "" Net/rpc "" Net/rpc/jsonrpc "" OS "" Log "//return value struct//need to meet above requirements type Response struct {country stringprovince stringcity Stringisp stringlatitude float64longitude float64timezone string}type ip2addr struct {db *geoip2. READER}//parameter structure//need to meet the above requirements type Agrs struct {ipstring string}//json RPC processing request//need to meet the above requirements func (T *ip2addr) Address (AGR *agrs, R Es *response) Error {netip: = net. PARSEIP (AGR. ipstring)//Call Open source GeoIP database to query IP address record, err: = T.db.city (Netip) Res. City = record. city.names["ZH-CN"]res. Province = record. Subdivisions[0]. names["ZH-CN"]res. Country = record. country.names["ZH-CN"]res. Latitude = record. Location.Latituderes.Longitude = record. Location.Longituderes.TimeZone = record. Location.timezonereturn Err}func Main () {//Load GeoIP db, err: = Geoip2. Open ("./geoLite2-city.mmdb ") if err! = Nil {log. Fatal (ERR)}//Initialize JSONRPCIP2ADDR: = &ip2addr{db}//Register RPC. Register (IP2ADDR)//bind port Address: = ": 3344" tcpaddr, err: = Net. RESOLVETCPADDR ("TCP", address) checkerror (ERR) listener, err: = Net. LISTENTCP ("TCP", TCPADDR) checkerror (err) log. PRINTLN ("JSON RPC is Listening", tcpaddr) for {conn, err: = Listener. Accept () if err! = Nil {continue}jsonrpc. SERVECONN (conn)}}func CheckError (err error) {if err! = Nil {fmt. PRINTLN ("Fatal error", Err. Error ()) OS. Exit (1)}} ' # # # PHP-JSONRPC client ' goclass jsonrpc{public $conn; function __construct ($host, $port) {$this->conn = FSO Ckopen ($host, $port, $errno, $ERRSTR, 3); if (! $this->conn) {return false;}} Public function call ($method, $params) {$obj = new StdClass (); $obj->code = 0; if (! $this->conn) {$obj->info = "JSONRPC Connection failed! Please contact qzhou@turboes.com"; return $obj; } $err = fwrite ($this->conn, Json_encode (Array (' method ' + $method, ' params ' = = ' Array ($params), ' id ' = 0,)) . "\ n"); if ($err = = = FALSE) {fclose ($this->conn); $obj->info = "Jsonrpc Send parameter failed! Please check your rpc-client code"; return $obj;} stream_set_timeout ($ This->conn, 0, 3000); $line = fgets ($this->conn); Fclose ($this->conn); if ($line = = = False) {$obj->info = "JSONRPC return message is empty! Please check your rpc-client code"; return $obj;} $temp = Json_decode ($line); $obj->code = $temp->error = = null? 1:0; $obj->data = $temp->result; return $obj; }}function json_rpc_ip_address ($ipString) {$client = new Jsonrpc ("127.0.0.1", 3344); $obj = $client->call (" Ip2addr.address ", [' ipstring ' = ' $ipString]); return $obj;} ' # # # Go Language jsonrpc client ' gopackage mainimport ("FMT" "Log" "Net/rpc/jsonrpc") type Response struct {country Stringprovince stringcity stringisp stringlatitude float64longitude float64timezone string}type Agrs struct {IpString String}func Main () {client, err: = Jsonrpc. Dial ("TCP", "121.40.238.123:3344") if err! = Nil {log. Fatal ("Dialing:", err)}//synchronous Callvar Res responseerr = client. Call ("Ip2addr.address", Agrs{"219.140.227.235"}, &res) if err! = Nil {log. Fatal ("Ip2addr error:", err)}fmt. Println (Res)} ' # # # # [code Address] (https://github.com/mojocn/ip2location/tree/master/example) # # # # [Welcome Pr/star Golang-captcha] (Https://github.com/mojocn/base64Captcha) 306 reads  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.