In this post, I wanna give some examples for working on network operations with Python. These examples are to check internet speed, to find the IP address, to find the location.
Check Internet Speed
The first example to check internet speed. We use speedtest library. To install this library pip3 install speedtest-cli
import speedtest
test = speedtest.Speedtest()
#check donload speed
download = test.download()
#check upload speed
upload = test.upload()
#print the results by converting to GB and rounding
print(f'Download speed: {round(download/1024**2,2)} GB')
print(f'Upload speed: {round(upload/1024**2,2)} GB')
The output will be :
Download speed: 25.91 GB
Upload speed: 2.79 GB
Find Public IP Address
The next one is to find public IP address. You should install requests. To install this library: pip3 install requests
from requests import get
#use get to get ip adress from ipify.org
ip= get("https://api.ipify.org/").text
#print IP address
print("Your IP Address is", ip)
The output will be:
Your IP Address is 212.129.81.62
Find Location From IP
The another one is find public IP and location. You need to install json and urllib. To install: pip3 install urllib and pip3 install json.
import json
from urllib.request import urlopen
#get response
response=urlopen('http://ipinfo.io/json')
#store reponse
data=json.load(response)
#print response via formating
print('{:<15}|{:<}'.format('IP Address',data['ip']))
print('{:<15}|{:<}'.format('Server',data['org']))
print('{:<15}|{:<}'.format('City',data['city']))
print('{:<15}|{:<}'.format('Country',data['country']))
print('{:<15}|{:<}'.format('Region',data['region']))
print('{:<15}|{:<}'.format('Time zone',data['timezone']))
The output will be:
IP Address |212.129.81.62
Server |AS15751 Meteor Mobile Communications Limited
City |Dublin
Country |IE
Region |Leinster
Time zone |Europe/Dublin
Be First to Comment