This commit is contained in:
2024-10-16 11:07:13 +08:00
parent 7b1e27db87
commit 95c3e1cda8
14 changed files with 256 additions and 44 deletions

3
IP_region_query/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module ipquery
go 1.22.2

BIN
IP_region_query/ipquery Normal file

Binary file not shown.

52
IP_region_query/main.go Normal file
View File

@@ -0,0 +1,52 @@
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type IPInfo struct {
Code string `json:"code"`
Data struct {
Continent string `json:"continent"`
Country string `json:"country"`
} `json:"data"`
IP string `json:"ip"`
Msg string `json:"msg"`
}
func main() {
if len(os.Args) < 2 {
log.Fatalf("Usage: %s <IP>", os.Args[0])
}
// 目标 URL
url := "https://qifu.baidu.com/ip/geo/v1/district?ip=" + os.Args[1]
// 发送 GET 请求
resp, err := http.Get(url)
if err != nil {
log.Fatalf("Error making GET request: %v", err)
}
defer resp.Body.Close()
// 读取响应体
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response body: %v", err)
}
// 解析 JSON 数据
var ipInfo IPInfo
if err := json.Unmarshal(body, &ipInfo); err != nil {
log.Fatalf("Error parsing JSON: %v", err)
}
// 提取并打印 continent 和 country 字段
fmt.Printf("%s%s\n", ipInfo.Data.Continent, ipInfo.Data.Country)
}