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())
如果对Python有兴趣或者入门找不到方向可以加群:834179111,本群整理了,从Python入门零基础到项目实战的学习资料。欢迎欢迎各位前来交流学习。
网友评论