import getpass
import ssl
import urllib.error
import urllib.parse
import urllib.request

host = input("Proxy IP: ").strip()
port = int(input("Proxy port: "))
username = input("Proxy username: ").strip()
password = getpass.getpass("Proxy password: ")
# Encode credentials, not the complete URL. A colon/@ in a password is data.
auth = urllib.parse.quote(username, safe="") + ":" + urllib.parse.quote(password, safe="")
proxy = f"http://{auth}@{host}:{port}"
opener = urllib.request.build_opener(
    urllib.request.ProxyHandler({"http": proxy, "https": proxy}),
    urllib.request.HTTPSHandler(context=ssl.create_default_context()),
)
try:
    with opener.open("https://example.com", timeout=30) as response:
        response.read(1024)  # Small connectivity check; not a page-size measurement.
        print("Destination HTTP status:", response.status)
except urllib.error.HTTPError as error:
    print("HTTP error:", error.code)
    raise SystemExit(1)
except (urllib.error.URLError, TimeoutError):
    # Do not print the proxy URL or the exception object: they can contain secrets.
    print("Connection failed. Check rental status, proxy fields, network and TLS trust.")
    raise SystemExit(1)
