鍍金池/ 問(wèn)答/Python  Linux  HTML/ 如何獲取當(dāng)前的本機(jī)IP?

如何獲取當(dāng)前的本機(jī)IP?

比如我的本機(jī)IP是192.168.0.151

如何獲取到這個(gè)地址??

回答
編輯回答
櫻花霓

獲取本機(jī)內(nèi)網(wǎng)IP

def get_host_ip():
    with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
        s.connect(('8.8.8.8', 80))
        return s.getsockname()[0]

獲取本機(jī)外網(wǎng)IP

import requests
ip = requests.get('http://ip.cip.cc/').text
2017年5月25日 15:24