python 获取本机IP(公网,局域网)

发布时间 2023-08-07 14:38:07作者: 菜鸟要飞~

获取公网IP

1 import requests
2 import re
3 res = requests.get("https://myip.ipip.net",timeout=5).text
4 ip = re.findall(r'(\d+\.\d+\.\d+\.\d+)', res)[0]
5 print(ip)

获取局域网IP

1 import socket
2 
3 res = socket.gethostbyname(socket.gethostname())
4 print(res)