Skip to content

Commit 7944593

Browse files
authored
Merge pull request Python-World#196 from AdityaJ7/website-load-time
Added script to check website load time
2 parents c0597a7 + 68b9e70 commit 7944593

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Time to load website
2+
3+
This script takes a url from the user and returns the time taken to load that website.
4+
5+
## How to use this ?
6+
7+
1. Just type the following on the command prompt:
8+
9+
python time_to_load_website.py
10+
11+
2. It will reuest you to provide a url. Provide the url and hit enter to see the script in action.
12+
13+
## Sample use:
14+
15+
<p align = "center">
16+
<img src="sample.PNG" alt="sample">
17+
</p>
7.42 KB
Loading
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from urllib.request import urlopen
2+
import time
3+
4+
5+
def get_load_time(url):
6+
"""This function takes a user defined url as input
7+
and returns the time taken to load that url in seconds.
8+
9+
Args:
10+
url (string): The user defined url.
11+
12+
Returns:
13+
time_to_load (float): The time taken to load the website in seconds.
14+
"""
15+
16+
if ("https" or "http") in url: # Checking for presence of protocols
17+
open_this_url = urlopen(url) # Open the url as entered by the user
18+
else:
19+
open_this_url = urlopen("https://" + url) # Adding https to the url
20+
start_time = time.time() # Time stamp before the reading of url starts
21+
open_this_url.read() # Reading the user defined url
22+
end_time = time.time() # Time stamp after the reading of the url
23+
open_this_url.close() # Closing the instance of the urlopen object
24+
time_to_load = end_time - start_time
25+
26+
return time_to_load
27+
28+
29+
if __name__ == '__main__':
30+
url = input("Enter the url whose loading time you want to check: ")
31+
print(f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")

0 commit comments

Comments
 (0)