im试图创建函数来检查连接性和localhost [关闭]

问题描述 投票:-1回答:1
import requests
import socket
def check_localhost():
    host_name = socket.gethostname()
    localhost = socket.gethostbyname(host_name)
    if localhost == "172.0.0.1":
        return True
    else:
        return False
check_localhost()

def Check_connectivity():
    request=requests.get('http://google.com')
    if request.status_code == 200:
        return True
    else:
        return False
check_connectivity()
python connectivity
1个回答
0
投票
也许您要这样检查localhost?

def check_localhost(): host_name = 'localhost' # not socket.gethostname() localhost = socket.gethostbyname(host_name) if localhost == "127.0.0.1": # not 172.0.0.1 return True else: return False check_localhost()

注意:对于IPv6,本地主机转换为:: 1
© www.soinside.com 2019 - 2024. All rights reserved.