获取ip地址

import socket
import urllib2

def     public_ip():
        read = urllib2.urlopen('http://ipecho.net/plain').read()
        return read.decode('utf-8')

def     local_ip():

        return socket.gethostbyname(socket.gethostname())

if __name__ == "__main__":
        print("Getting public and local IP...")
        print("Public IP: {}\nLocal IP: {}".format(public_ip(), local_ip()))

你可能感兴趣的:(socket,python,python)