|
1 |
| -import urllib2 |
2 |
| -import socket |
| 1 | +import urllib2 #importing library urllib2 (which user might need to install before running the script) |
| 2 | +import socket #importing socket |
3 | 3 |
|
4 |
| -def check_url( url, timeout=10 ): |
5 |
| - httpcode =1 |
6 |
| - if urllib2.urlopen(url,timeout=timeout).getcode() == 200: |
7 |
| - return "website is online" |
8 |
| - elif urllib2.urlopen(url,timeout=timeout).getcode() == 301: |
9 |
| - return "website was redirected permaanently" |
10 |
| - elif urllib2.urlopen(url,timeout=timeout).getcode() == 404: |
11 |
| - return "website was not found" |
12 |
| -print "please enter a website to check the status. don`t forget to add the http://" |
13 |
| -site = raw_input() |
14 |
| -print check_url(site) |
| 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 |
0 commit comments