Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Replaced urllib2 with requests
  • Loading branch information
apb7 committed Nov 21, 2017
commit 0743c73981cbb784ebeb8b837bf373896344b3c1
28 changes: 16 additions & 12 deletions website_status_check/website_status_check.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import urllib2 #importing library urllib2 (which user might need to install before running the script)
import socket #importing socket
import requests

def check_url( url, timeout=10 ): #creating function to keep code orginized
if urllib2.urlopen(url,timeout=timeout).getcode() == 200: #this is checking if the return request of urllib2 is 200 which means website is online
return "website is online" #returns a string
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
return "website was redirected permanently" #returns a string
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
return "website was not found" #returns a string
print "please enter a website to check the status. don`t forget to add the http://" #printing prompt line
site = raw_input() #input string was put into site as a variable
print check_url(site) #run function with given variable
# TODO: Add more status code functionality.
def check_url(url):
'''
This function uses status codes to check various states of any website.
'''
r=requests.get(url)
if r.status_code==200:
return "Website is online."
elif r.status_code==301:
return "Website has been redirected permanently."
elif r.status_code==404:
return "Website not found!"

url=input("Please enter a website, inclusive of 'http://' > ")
print(check_url(url))