|
1 | 1 | from urllib.request import urlopen |
2 | 2 | import time |
3 | 3 |
|
4 | | -#Storing user defined URl |
5 | | -URL = input("Enter the url whose loading time you want to check: ") |
6 | 4 |
|
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. |
10 | 8 |
|
11 | | -else: |
12 | | - open_this_url = urlopen("https://" + URL) |
| 9 | + Args: |
| 10 | + url (string): The user defined url. |
13 | 11 |
|
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 | + """ |
16 | 15 |
|
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 |
19 | 20 |
|
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 |
22 | 22 |
|
23 | | -#Closing the instance of the urlopen object |
24 | | -open_this_url.close() |
| 23 | + open_this_url.read() # Reading the user defined url |
25 | 24 |
|
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