Skip to content

Commit 8b60da4

Browse files
authored
Merge pull request prateekiiest#66 from apb7/master
Replacing urllib2 with requests
2 parents 477eec3 + 4dd7a65 commit 8b60da4

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed
+16-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import urllib2 #importing library urllib2 (which user might need to install before running the script)
2-
import socket #importing socket
1+
import requests
32

4-
def check_url( url, timeout=10 ): #creating function to keep code orginized
5-
if urllib2.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-
elif urllib2.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-
elif urllib2.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-
print check_url(site) #run function with given variable
3+
# TODO: Add more status code functionality.
4+
def check_url(url):
5+
'''
6+
This function uses status codes to check various states of any website.
7+
'''
8+
r=requests.get(url)
9+
if r.status_code==200:
10+
return "Website is online."
11+
elif r.status_code==301:
12+
return "Website has been redirected permanently."
13+
elif r.status_code==404:
14+
return "Website not found!"
15+
16+
url=input("Please enter a website, inclusive of 'http://' > ")
17+
print check_url(url)

0 commit comments

Comments
 (0)