Skip to content

Commit b5734b4

Browse files
Update speed_test_cli.py
Included location of data interpretor
1 parent a9d7dcb commit b5734b4

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

InternetSpeedTestApp/speed_test_cli.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,57 @@
44

55
def get_local_ip():
66
try:
7-
return socket.gethostbyname(socket.gethostname())
8-
except:
7+
local_ip = socket.gethostbyname(socket.gethostname())
8+
return local_ip
9+
except Exception:
910
return "Unavailable"
1011

1112
def get_external_ip_and_isp():
1213
try:
13-
response = requests.get("https://ipinfo.io/json")
14+
response = requests.get("https://ipinfo.io/json", timeout=5)
1415
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:
1722
return "N/A", "N/A", "N/A", "N/A"
1823

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+
1943
def main():
20-
print("🔍 Getting network details...\n")
44+
print("🔍 Starting network details retrieval...\n")
2145
local_ip = get_local_ip()
2246
external_ip, isp, city, country = get_external_ip_and_isp()
23-
2447
print(f"📡 Local IP Address : {local_ip}")
2548
print(f"🌐 External IP Address : {external_ip}")
2649
print(f"🏢 ISP : {isp}")
2750
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']}")
4957

5058
if __name__ == "__main__":
5159
main()
60+

0 commit comments

Comments
 (0)