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

获取公网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)

 

posted @ 2023-08-07 14:34  菜鸟要飞~  Views(288)  Comments(0Edit  收藏  举报