Update app.py
Browse files
app.py
CHANGED
|
@@ -8,31 +8,31 @@ import urllib.request
|
|
| 8 |
import asyncio
|
| 9 |
import httpx
|
| 10 |
|
| 11 |
-
def test_dns():
|
| 12 |
-
print("π Testing DNS resolution for
|
| 13 |
try:
|
| 14 |
-
ip = socket.gethostbyname(
|
| 15 |
-
print(f"β
DNS OK:
|
| 16 |
return True
|
| 17 |
except Exception as e:
|
| 18 |
print(f"β DNS FAILED: {e}")
|
| 19 |
return False
|
| 20 |
|
| 21 |
-
def test_https_urllib():
|
| 22 |
-
print("\nπ Testing HTTPS access via urllib (Python stdlib)...")
|
| 23 |
try:
|
| 24 |
-
with urllib.request.urlopen(
|
| 25 |
print(f"β
HTTPS OK: status {response.getcode()}")
|
| 26 |
return True
|
| 27 |
except Exception as e:
|
| 28 |
print(f"β HTTPS FAILED (urllib): {e}")
|
| 29 |
return False
|
| 30 |
|
| 31 |
-
async def test_https_httpx():
|
| 32 |
-
print("\nπ Testing HTTPS access via httpx (used by python-telegram-bot)...")
|
| 33 |
try:
|
| 34 |
async with httpx.AsyncClient(timeout=10) as client:
|
| 35 |
-
resp = await client.get(
|
| 36 |
print(f"β
HTTPS OK (httpx): status {resp.status_code}")
|
| 37 |
return True
|
| 38 |
except Exception as e:
|
|
@@ -44,9 +44,17 @@ if __name__ == "__main__":
|
|
| 44 |
print("π‘ Hugging Face Space: Internet Access Test for Telegram")
|
| 45 |
print("="*60)
|
| 46 |
|
| 47 |
-
dns_ok = test_dns()
|
| 48 |
-
urllib_ok = test_https_urllib() if dns_ok else False
|
| 49 |
-
httpx_ok = asyncio.run(test_https_httpx()) if dns_ok else False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
print("\n" + "="*60)
|
| 52 |
if dns_ok and (urllib_ok or httpx_ok):
|
|
|
|
| 8 |
import asyncio
|
| 9 |
import httpx
|
| 10 |
|
| 11 |
+
def test_dns(url: str):
|
| 12 |
+
print(f"π Testing DNS resolution for {url}...")
|
| 13 |
try:
|
| 14 |
+
ip = socket.gethostbyname(url)
|
| 15 |
+
print(f"β
DNS OK: {url} -> {ip}")
|
| 16 |
return True
|
| 17 |
except Exception as e:
|
| 18 |
print(f"β DNS FAILED: {e}")
|
| 19 |
return False
|
| 20 |
|
| 21 |
+
def test_https_urllib(url: str):
|
| 22 |
+
print(f"\nπ Testing HTTPS access via urllib (Python stdlib) for {url}...")
|
| 23 |
try:
|
| 24 |
+
with urllib.request.urlopen(str, timeout=10) as response:
|
| 25 |
print(f"β
HTTPS OK: status {response.getcode()}")
|
| 26 |
return True
|
| 27 |
except Exception as e:
|
| 28 |
print(f"β HTTPS FAILED (urllib): {e}")
|
| 29 |
return False
|
| 30 |
|
| 31 |
+
async def test_https_httpx(url: str):
|
| 32 |
+
print(f"\nπ Testing HTTPS access via httpx (used by python-telegram-bot) for {url}...")
|
| 33 |
try:
|
| 34 |
async with httpx.AsyncClient(timeout=10) as client:
|
| 35 |
+
resp = await client.get(url)
|
| 36 |
print(f"β
HTTPS OK (httpx): status {resp.status_code}")
|
| 37 |
return True
|
| 38 |
except Exception as e:
|
|
|
|
| 44 |
print("π‘ Hugging Face Space: Internet Access Test for Telegram")
|
| 45 |
print("="*60)
|
| 46 |
|
| 47 |
+
dns_ok = test_dns("api.telegram.org")
|
| 48 |
+
urllib_ok = test_https_urllib("https://api.telegram.org") if dns_ok else False
|
| 49 |
+
httpx_ok = asyncio.run(test_https_httpx("https://api.telegram.org")) if dns_ok else False
|
| 50 |
+
|
| 51 |
+
dns_ok = test_dns("www.google.com")
|
| 52 |
+
urllib_ok = test_https_urllib("https://www.google.com") if dns_ok else False
|
| 53 |
+
httpx_ok = asyncio.run(test_https_httpx("https://www.google.com")) if dns_ok else False
|
| 54 |
+
|
| 55 |
+
dns_ok = test_dns("chat.qwen.ai")
|
| 56 |
+
urllib_ok = test_https_urllib("https://chat.qwen.ai") if dns_ok else False
|
| 57 |
+
httpx_ok = asyncio.run(test_https_httpx("https://chat.qwen.ai")) if dns_ok else False
|
| 58 |
|
| 59 |
print("\n" + "="*60)
|
| 60 |
if dns_ok and (urllib_ok or httpx_ok):
|