1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python获取内网IP和外网IP的方法

python获取内网IP和外网IP的方法

时间:2019-10-17 10:05:32

相关推荐

python获取内网IP和外网IP的方法

1、获取内网IP

示例代码:

import socket# 获取内网IPs = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.connect(('8.8.8.8', 80))print(s.getsockname()[0])print(s.getsockname())s.close()

运行结果:

2、获取外网IP

示例代码:

import requestsprint(requests.get('http://ifconfig.me/ip', timeout=1).text.strip())print(requests.get('http://ifconfig.me', timeout=1).text.strip())print(requests.get('http://ifconfig.me', timeout=1))print(requests.get('http://ifconfig.me/ip', timeout=1))

运行结果:

参考博文:

python获取内网IP和外网IP的方法_slp_44777680的博客-CSDN博客_python 获取内网ip

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。