ローカルipとパブリックipの取得方法を共有します
1707 ワード
import socket
def get_local_ip():
'''
ip
:return:
'''
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('8.8.8.8', 80))
local_ip = s.getsockname()[0]
except:
local_ip.close()
return local_ip
print(get_local_ip())
import requests, chardet, re
def get_net_ip():
'''
ip
:return:
'''
html = requests.get('http://2018.ip138.com/ic.asp')
html.encoding = chardet.detect(html.content)['encoding']
try:
match_obj = re.findall(r' IP :\[(.*?)\] ', html.text)
network_ip = match_obj[0].strip()
return network_ip
except:
print(' !')
print(get_net_ip())