LucaR84 commited on
Commit
b876bd7
Β·
verified Β·
1 Parent(s): ab84364

Create dns_test.py

Browse files
Files changed (1) hide show
  1. dns_test.py +56 -0
dns_test.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # test_internet.py
2
+ import os
3
+ import socket
4
+ import ssl
5
+ import sys
6
+ import urllib.request
7
+ import asyncio
8
+ import httpx
9
+
10
+ def test_dns():
11
+ print("πŸ” Testing DNS resolution for api.telegram.org...")
12
+ try:
13
+ ip = socket.gethostbyname("api.telegram.org")
14
+ print(f"βœ… DNS OK: api.telegram.org -> {ip}")
15
+ return True
16
+ except Exception as e:
17
+ print(f"❌ DNS FAILED: {e}")
18
+ return False
19
+
20
+ def test_https_urllib():
21
+ print("\n🌐 Testing HTTPS access via urllib (Python stdlib)...")
22
+ try:
23
+ with urllib.request.urlopen("https://api.telegram.org", timeout=10) as response:
24
+ print(f"βœ… HTTPS OK: status {response.getcode()}")
25
+ return True
26
+ except Exception as e:
27
+ print(f"❌ HTTPS FAILED (urllib): {e}")
28
+ return False
29
+
30
+ async def test_https_httpx():
31
+ print("\nπŸš€ Testing HTTPS access via httpx (used by python-telegram-bot)...")
32
+ try:
33
+ async with httpx.AsyncClient(timeout=10) as client:
34
+ resp = await client.get("https://api.telegram.org")
35
+ print(f"βœ… HTTPS OK (httpx): status {resp.status_code}")
36
+ return True
37
+ except Exception as e:
38
+ print(f"❌ HTTPS FAILED (httpx): {e}")
39
+ return False
40
+
41
+ if __name__ == "__main__":
42
+ print("="*60)
43
+ print("πŸ“‘ Hugging Face Space: Internet Access Test for Telegram")
44
+ print("="*60)
45
+
46
+ dns_ok = test_dns()
47
+ urllib_ok = test_https_urllib() if dns_ok else False
48
+ httpx_ok = asyncio.run(test_https_httpx()) if dns_ok else False
49
+
50
+ print("\n" + "="*60)
51
+ if dns_ok and (urllib_ok or httpx_ok):
52
+ print("πŸŽ‰ SUCCESS: Outbound internet appears to work!")
53
+ else:
54
+ print("πŸ›‘ FAILURE: Outbound internet is BLOCKED.")
55
+ print(" β†’ Telegram bots will NOT work on this platform.")
56
+ print("="*60)