Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Design and implement function is_reachable(url) that shall check if the url i

ID: 3578903 • Letter: 1

Question

1. Design and implement function is_reachable(url) that shall check if the url is reachable and return True or False, respectively. Hint: Assume that a URL is reachable if you can urlopen() it.

In the same file, write a program shall read the URLs from the file urls.txt (attached) that contains a list of URLs, one URL per line. The file has no blank lines. Each line possibly has white spaces at the beginning and/or at the end. The program shall display the URLs that are unreachable, one URL per line. The program shall use function is_reachable().

Explanation / Answer

import urllib2

def is_reachable(url):
        try:
                urllib2.urlopen( url );
                return True;
        except:
                pass;
        return False;

def tell_about_urls():
        urlfile = open("urls.txt");
        for line in urlfile:
                line = line.strip(); #remove white spaces
                if( line[:-1] == ' '): #remove endline character if it is there
                        line= line[-1];
                if not is_reachable(line):
                        print line;

tell_about_urls();