优化
This commit is contained in:
3
IP_region_query/go.mod
Normal file
3
IP_region_query/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ipquery
|
||||
|
||||
go 1.22.2
|
||||
BIN
IP_region_query/ipquery
Normal file
BIN
IP_region_query/ipquery
Normal file
Binary file not shown.
52
IP_region_query/main.go
Normal file
52
IP_region_query/main.go
Normal 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)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user