File size: 1,951 Bytes
0eb03b6 4eaf716 0eb03b6 4eaf716 0eb03b6 4eaf716 0eb03b6 4eaf716 cb17166 4eaf716 cb17166 4eaf716 cb17166 4eaf716 8d22836 4eaf716 cb17166 4eaf716 cb17166 4eaf716 0eb03b6 8d22836 0eb03b6 4eaf716 8d22836 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | # app.py
# test_internet.py
import os
import socket
import ssl
import sys
import urllib.request
import asyncio
import httpx
def test_dns(url: str):
print(f"π Testing DNS resolution for {url}...")
try:
ip = socket.gethostbyname(url)
print(f"β
DNS OK: {url} -> {ip}")
return True
except Exception as e:
print(f"β DNS FAILED: {e}")
return False
def test_https_urllib(url: str):
print(f"\nπ Testing HTTPS access via urllib (Python stdlib) for {url}...")
try:
with urllib.request.urlopen(url, 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(url: str):
print(f"\nπ Testing HTTPS access via httpx (used by python-telegram-bot) for {url}...")
try:
async with httpx.AsyncClient(timeout=10) as client:
resp = await client.get(url)
print(f"β
HTTPS OK (httpx): status {resp.status_code}")
return True
except Exception as e:
print(f"β HTTPS FAILED (httpx): {e}")
return False
def test(host: str):
url = f"https://{host}"
dns_ok = test_dns(host)
urllib_ok = test_https_urllib(url) if dns_ok else False
httpx_ok = asyncio.run(test_https_httpx(url)) 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)
if __name__ == "__main__":
print("="*60)
print("π‘ Hugging Face Space: Internet Access Test for Telegram")
print("="*60)
test("api.telegram.org")
test("www.google.com")
test("chat.qwen.ai")
|