原创 Python/Golang获取 IPv4 和 IPv6 地址
本站是个人站点,用于作者实战学习前端和后台知识,请勿用于商业用途,仅供个人测试学习之用,请遵守中国法律法规
通过 Python/Golang 获取公网 IPv4 和 IPv6 地址,还可以返回是 IPv4 还是 IPv6 访问优先。
1. Python/Golang获取 IPv4 地址
1.1 Python 获取本机公网IPv4地址
- 输入示例
#!/usr/bin/python3
import requests
# 获取 IPv6 地址接口; https://ipv6.ipw.cn/api/ip/myip
r = requests.get('https://ipv4.ipw.cn/api/ip/myip')
clientIP = r.text
print(clientIP)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 输出示例
106.224.145.147
1
1.2 Golang 获取本机公网IPv4 地址
- 输入示例
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// 获取 IPv6 地址接口; https://ipv6.ipw.cn/api/ip/myip
responseClient, errClient := http.Get("https://ipv4.ipw.cn/api/ip/myip")
if errClient != nil {
fmt.Printf("获取外网 IP 失败,请检查网络\n")
panic(errClient)
}
defer responseClient.Body.Close()
// 获取 http response 的 body
body, _ := ioutil.ReadAll(responseClient.Body)
clientIP := string(body)
print(clientIP)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- 输出示例
106.224.145.147
1
2. Python/Golang获取 IPv6 地址
2.1 Python 获取本机公网IPv6地址
- 输入示例
#!/usr/bin/python3
import requests
r = requests.get('https://ipv6.ipw.cn/api/ipv6/myip')
clientIP = r.text
print(clientIP)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 输出示例
2408:824c:200::2b8b:336f:cc9c
1
2.2 Golang 获取本机公网IPv6地址
- 输入示例
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
responseClient, errClient := http.Get("https://ipv6.ipw.cn/api/ip/myip")
if errClient != nil {
fmt.Printf("获取外网 IP 失败,请检查网络\n")
panic(errClient)
}
defer responseClient.Body.Close()
// 获取 http response 的 body
body, _ := ioutil.ReadAll(responseClient.Body)
clientIP := string(body)
print(clientIP)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- 输出示例
2408:824c:200::2b8b:336f:cc9c
1
3. 测试网络是IPv4还是IPv6访问优先
3.1 Python 测试网络是IPv4还是IPv6访问优先
- 输入示例
#!/usr/bin/python3
import json
import requests
## HTTP GET
r = requests.get('https://test.ipw.cn/api/ip/myip?json')
## 转成 Python 字典并赋值
ip_detail = json.loads(r.text)
result = ip_detail['result']
IP = ip_detail['IP']
IPVersion = ip_detail['IPVersion']
## 打印
print('result:',result)
print('IP:',IP)
print('IPVesion:',IPVersion)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- 输出示例
## 返回示例1
result: True
IP: 106.224.145.147
IPVesion: IPv4
## 返回示例2
result: True
IP: 2408:824c:200::2b8b:336f:cc9c
IPVesion: IPv6
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
3.2 Golang 测试网络是IPv4还是IPv6访问优先
- 输入示例
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
type TestIPVesion struct {
Result bool `json:"result"`
IP string `json:"IP"`
IPVersion string `json:"IPVersion"`
}
responseClient, errClient := http.Get("https://test.ipw.cn/api/ip/myip?json") // 获取外网 IP
if errClient != nil {
fmt.Printf("获取外网 IP 失败,请检查网络\n")
panic(errClient)
}
// 程序在使用完 response 后必须关闭 response 的主体。
defer responseClient.Body.Close()
body, _ := ioutil.ReadAll(responseClient.Body)
var insTestIPVesion TestIPVesion
err := json.Unmarshal(body, &insTestIPVesion)
if err != nil {
fmt.Println(err)
}
result := insTestIPVesion.Result
IP := insTestIPVesion.IP
IPVersion := insTestIPVesion.IPVersion
fmt.Printf("Result: %t\n", result)
fmt.Printf("IP: %s\n", IP)
fmt.Printf("IPVersion: %s\n", IPVersion)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
- 输出示例
## 返回示例1
result: true
IP: 106.224.145.147
IPVesion: IPv4
## 返回示例2
result: true
IP: 2408:824c:200::2b8b:336f:cc9c
IPVesion: IPv6
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
访客IP: ,您的网络 访问优先
本站运行于腾讯云