from urllib.request import urlopen
from urllib.error import URLError, HTTPError
import telnetlib

def is_webpage_accessible_urllib(url, timeout=2):
    try:
        # 打开URL
        with urlopen(url, timeout=timeout) as response:
            # 检查状态码
            if response.getcode() == 200:
                print(f"The webpage at {url} is accessible.")
                return True
            else:
                print(f"The webpage at {url} returned status code: {response.getcode()}")
                return False

    except HTTPError as e:
        # 处理HTTP错误（例如404, 500等）
        print(f"HTTP Error: {e.code} - {e.reason} for URL: {url}")
        return False
    except URLError as e:
        # 处理其他类型的错误（例如无法解析主机名、连接失败等）
        print(f"URL Error: {e.reason} for URL: {url}")
        return False
    except Exception as e:
        # 处理所有其他异常
        print(f"An error occurred while trying to access {url}: {e}")
        return False


def telnet(host, port, timeout=2):
    import telnetlib
    try:
        telnetlib.Telnet(host, port, timeout)
        return True
    except Exception as e:
        print(f"Failed to connect to {host}:{port}: {e}")
    return False