python通过ip查询网站获得本地ip的代码
import re
import urllib.request
class GetIP():
def get_external_ip(self, nurl='http://city.ip138.com/city0.asp'):
try:
open_url = urllib.request.urlopen(nurl)
url_content = str(open_url.read())
ip = re.search('\d+\.\d+\.\d+\.\d+', url_content).group(0)
open_url.close()
return(ip)
except urllib.error.URLError as err:
print(str(err))
