| |
| import os |
| import socket |
| import ssl |
| import sys |
| import urllib.request |
| import asyncio |
| import httpx |
|
|
| def test_dns(): |
| print("π Testing DNS resolution for api.telegram.org...") |
| try: |
| ip = socket.gethostbyname("api.telegram.org") |
| print(f"β
DNS OK: api.telegram.org -> {ip}") |
| return True |
| except Exception as e: |
| print(f"β DNS FAILED: {e}") |
| return False |
|
|
| def test_https_urllib(): |
| print("\nπ Testing HTTPS access via urllib (Python stdlib)...") |
| try: |
| with urllib.request.urlopen("https://api.telegram.org", timeout=10) as response: |
| print(f"β
HTTPS OK: status {response.getcode()}") |
| return True |
| except Exception as e: |
| print(f"β HTTPS FAILED (urllib): {e}") |
| return False |
|
|
| async def test_https_httpx(): |
| print("\nπ Testing HTTPS access via httpx (used by python-telegram-bot)...") |
| try: |
| async with httpx.AsyncClient(timeout=10) as client: |
| resp = await client.get("https://api.telegram.org") |
| print(f"β
HTTPS OK (httpx): status {resp.status_code}") |
| return True |
| except Exception as e: |
| print(f"β HTTPS FAILED (httpx): {e}") |
| return False |
|
|
| if __name__ == "__main__": |
| print("="*60) |
| print("π‘ Hugging Face Space: Internet Access Test for Telegram") |
| print("="*60) |
| |
| dns_ok = test_dns() |
| urllib_ok = test_https_urllib() if dns_ok else False |
| httpx_ok = asyncio.run(test_https_httpx()) if dns_ok else False |
|
|
| print("\n" + "="*60) |
| if dns_ok and (urllib_ok or httpx_ok): |
| print("π SUCCESS: Outbound internet appears to work!") |
| else: |
| print("π FAILURE: Outbound internet is BLOCKED.") |
| print(" β Telegram bots will NOT work on this platform.") |
| print("="*60) |