Golang parsing IP to City JSONRPC service tutorials

Source: Internet
Author: User
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:

Name of parameter type Description
Ip Url-qurey-string 可选The IP address to be queried, if not transmitted, indicates that the current IP

return example

{    "code": 0,    "data": {        "Country": "中国",        "Province": "湖北省",        "City": "武汉",        "ISP": "",        "Latitude": 30.5801,        "Longitude": 114.2734,        "TimeZone": "Asia/Shanghai"    }}
JSON_RPC Address: 121.40.238.123 (IP address is faster) api.turboes.com Port: 3344

Third-party Resources

    • GeoIP2 Reader for Go
    • GeoLite2 Open Source Database

Go standard library JSONRPC service side

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 the

  • method is output (the method's type is exported)
  • The method itself is also output (the is exported)
  • method must The second parameter of the
  • method, which must be the output type or the built-in type (the methods has two arguments, both exported or builtin types), is the pointer type (the Second argument is a pointer)
  • method returns type error (the method has return type error)
Package Mainimport ("FMT" "Github.com/oschwald/geoip2-golang" "Net" "Net/rpc" "Net/rpc/jsonrpc" "OS" "Log")//return value struct//need to meet the above requirements type Response struct {country string province string City string ISP s Tring Latitude float64 Longitude float64 TimeZone 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 , res *response) Error {netip: = net. PARSEIP (AGR. ipstring) 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.latitude Res. Longitude = record. Location.longitude Res. TimeZone = record. Location.timezone return Err}func Main () {//Load GeoIP db, err: = Geoip2. Open ("./geolite2-city.mmdb") if err! = Nil {log. Fatal (ERR)}//InitializationJsonrpc ip2addr: = &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

Class jsonrpc{private $conn;        function __construct ($host, $port) {$this->conn = Fsockopen ($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 (' method ' + $method, ' params ' = > Array ($params), ' id ' = 0, ').        "\ n");            if ($err = = = False) {$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);            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

package mainimport (    "fmt"    "log"    "net/rpc/jsonrpc")type Response struct {    Country   string    Province  string    City      string    ISP       string    Latitude  float64    Longitude float64    TimeZone  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 call    var res Response    err = client.Call("Ip2addr.Address", Agrs{"219.140.227.235"}, &res)    if err != nil {        log.Fatal("arith error:", err)    }    fmt.Println(res)}

Golang-captcha Image Verification Code

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.