33 lines
536 B
Python
33 lines
536 B
Python
import socket
|
|
import os
|
|
import re
|
|
|
|
def get_local_ip():
|
|
|
|
|
|
try:
|
|
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
|
s.connect(('8.8.8.8',80))
|
|
ip=s.getsockname()[0]
|
|
finally:
|
|
s.close()
|
|
|
|
|
|
print(ip)
|
|
return ip
|
|
|
|
def get_local_ipv6():
|
|
|
|
output = os.popen("ipconfig /all").read()
|
|
# print(output)
|
|
result = re.findall(r"(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})", output, re.I)
|
|
ip = result[0][0]
|
|
|
|
print(ip)
|
|
return ip
|
|
|
|
|
|
if __name__ == '__main__':
|
|
get_local_ip()
|
|
get_local_ipv6()
|