You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importurllib2#importing library urllib2 (which user might need to install before running the script)
2
-
importsocket#importing socket
1
+
importrequests
3
2
4
-
defcheck_url( url, timeout=10 ): #creating function to keep code orginized
5
-
ifurllib2.urlopen(url,timeout=timeout).getcode() ==200: #this is checking if the return request of urllib2 is 200 which means website is online
6
-
return"website is online"#returns a string
7
-
elifurllib2.urlopen(url,timeout=timeout).getcode() ==301: #this is checking if the return request of urllib2 is 301 which means website has been permanently redirected
8
-
return"website was redirected permanently"#returns a string
9
-
elifurllib2.urlopen(url,timeout=timeout).getcode() ==404: #this is checking if the return request of urllib2 is 404 which means page was not found
10
-
return"website was not found"#returns a string
11
-
print"please enter a website to check the status. don`t forget to add the http://"#printing prompt line
12
-
site=raw_input() #input string was put into site as a variable
13
-
printcheck_url(site) #run function with given variable
3
+
# TODO: Add more status code functionality.
4
+
defcheck_url(url):
5
+
'''
6
+
This function uses status codes to check various states of any website.
7
+
'''
8
+
r=requests.get(url)
9
+
ifr.status_code==200:
10
+
return"Website is online."
11
+
elifr.status_code==301:
12
+
return"Website has been redirected permanently."
13
+
elifr.status_code==404:
14
+
return"Website not found!"
15
+
16
+
url=input("Please enter a website, inclusive of 'http://' > ")
0 commit comments