|
4 | 4 |
|
5 | 5 | def get_local_ip(): |
6 | 6 | try: |
7 | | - return socket.gethostbyname(socket.gethostname()) |
8 | | - except: |
| 7 | + local_ip = socket.gethostbyname(socket.gethostname()) |
| 8 | + return local_ip |
| 9 | + except Exception: |
9 | 10 | return "Unavailable" |
10 | 11 |
|
11 | 12 | def get_external_ip_and_isp(): |
12 | 13 | try: |
13 | | - response = requests.get("https://ipinfo.io/json") |
| 14 | + response = requests.get("https://ipinfo.io/json", timeout=5) |
14 | 15 | data = response.json() |
15 | | - return data.get("ip", "N/A"), data.get("org", "N/A"), data.get("city", "N/A"), data.get("country", "N/A") |
16 | | - except: |
| 16 | + ip = data.get("ip", "N/A") |
| 17 | + org = data.get("org", "N/A") |
| 18 | + city = data.get("city", "N/A") |
| 19 | + country = data.get("country", "N/A") |
| 20 | + return ip, org, city, country |
| 21 | + except Exception: |
17 | 22 | return "N/A", "N/A", "N/A", "N/A" |
18 | 23 |
|
| 24 | +def perform_speed_test(): |
| 25 | + st = speedtest.Speedtest() |
| 26 | + print("🔎 Searching for the best speedtest server based on ping...") |
| 27 | + best_server = st.get_best_server() |
| 28 | + print(f"✅ Best Server Found: {best_server['host']} located in {best_server['name']}, {best_server['country']}\n") |
| 29 | + print("⚡ Measuring download speed...") |
| 30 | + download_speed_mbps = st.download() / 1_000_000 |
| 31 | + print("⚡ Measuring upload speed...") |
| 32 | + upload_speed_mbps = st.upload() / 1_000_000 |
| 33 | + ping_ms = st.results.ping |
| 34 | + result_link = st.results.share() |
| 35 | + return { |
| 36 | + "download": download_speed_mbps, |
| 37 | + "upload": upload_speed_mbps, |
| 38 | + "ping": ping_ms, |
| 39 | + "server": best_server, |
| 40 | + "result_link": result_link |
| 41 | + } |
| 42 | + |
19 | 43 | def main(): |
20 | | - print("🔍 Getting network details...\n") |
| 44 | + print("🔍 Starting network details retrieval...\n") |
21 | 45 | local_ip = get_local_ip() |
22 | 46 | external_ip, isp, city, country = get_external_ip_and_isp() |
23 | | - |
24 | 47 | print(f"📡 Local IP Address : {local_ip}") |
25 | 48 | print(f"🌐 External IP Address : {external_ip}") |
26 | 49 | print(f"🏢 ISP : {isp}") |
27 | 50 | print(f"📍 Location : {city}, {country}\n") |
28 | | - |
29 | | - st = speedtest.Speedtest() |
30 | | - |
31 | | - print("🔎 Finding best server...") |
32 | | - best = st.get_best_server() |
33 | | - print(f"✅ Best Server Found: {best['host']} ({best['name']}, {best['country']})\n") |
34 | | - |
35 | | - print("⚡ Testing download speed...") |
36 | | - download_speed = st.download() / 1_000_000 |
37 | | - |
38 | | - print("⚡ Testing upload speed...") |
39 | | - upload_speed = st.upload() / 1_000_000 |
40 | | - |
41 | | - ping = st.results.ping |
42 | | - |
43 | | - print("\n📊 Speed Test Results:") |
44 | | - print(f"⬇️ Download Speed : {download_speed:.2f} Mbps") |
45 | | - print(f"⬆️ Upload Speed : {upload_speed:.2f} Mbps") |
46 | | - print(f"⏱ Ping : {ping:.2f} ms") |
47 | | - |
48 | | - print(f"\n🔗 Result Share Link : {st.results.share()}") |
| 51 | + results = perform_speed_test() |
| 52 | + print("\n📊 Speed Test Results Summary:") |
| 53 | + print(f"⬇️ Download Speed : {results['download']:.2f} Mbps") |
| 54 | + print(f"⬆️ Upload Speed : {results['upload']:.2f} Mbps") |
| 55 | + print(f"⏱ Ping : {results['ping']:.2f} ms") |
| 56 | + print(f"\n🔗 Share your results with this link: {results['result_link']}") |
49 | 57 |
|
50 | 58 | if __name__ == "__main__": |
51 | 59 | main() |
| 60 | + |
0 commit comments