Skip to content

Commit 04b48ee

Browse files
authored
Followed PEP-8 rules
1 parent 77d281a commit 04b48ee

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,39 @@
11
from urllib.request import urlopen
22
import time
33

4-
#Storing user defined URl
5-
URL = input("Enter the url whose loading time you want to check: ")
64

7-
#Open the url as entered by the user and checking protocols
8-
if ("https" or "http") in URL:
9-
open_this_url = urlopen(URL)
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.
108
11-
else:
12-
open_this_url = urlopen("https://" + URL)
9+
Args:
10+
url (string): The user defined url.
1311
14-
#Time stamp before the reading of url starts
15-
start_time = time.time()
12+
Returns:
13+
time_to_load (float): The time taken to load the website in seconds.
14+
"""
1615

17-
#Reading the user defined url
18-
read_the_url = open_this_url.read()
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
1920

20-
#Time stamp after the reading of the url
21-
end_time = time.time()
21+
start_time = time.time() # Time stamp before the reading of url starts
2222

23-
#Closing the instance of the urlopen object
24-
open_this_url.close()
23+
open_this_url.read() # Reading the user defined url
2524

26-
print(f"\nThe time taken to load {URL} is {end_time - start_time} seconds.")
25+
end_time = time.time() # Time stamp after the reading of the url
26+
27+
open_this_url.close() # Closing the instance of the urlopen object
28+
29+
time_to_load = end_time - start_time
30+
31+
return time_to_load
32+
33+
34+
if __name__ == '__main__':
35+
36+
url = input("Enter the url whose loading time you want to check: ")
37+
38+
print(
39+
f"\nThe time taken to load {url} is {get_load_time(url):.2} seconds.")

0 commit comments

Comments
 (0)