13

I have the following script (below). which will return the status code of URLs. It loops through a file and tries to connect to each host. Only problem is that it obviously stops looping when it reaches an exception.

I have tried numerous things to put the how of it in a loop, but to no avail. Any thoughts?

import urllib
import sys
import time

hostsFile = "webHosts.txt"


try:
    f = file(hostsFile)
    while True:
        line = f.readline().strip()
        epoch = time.time()
        epoch = str(epoch)
        if len(line) == 0:
            break
        conn = urllib.urlopen(line)
        print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except IOError:
    epoch = time.time()
    epoch = str(epoch)
    print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
    sys.exit()
else:
    f.close()

EDIT:

I've come up with this in the mean-time, any issues with this? (i'm still learning :p )...

f = file(hostsFile)
while True:
    line = f.readline().strip()
    epoch = time.time()
    epoch = str(epoch)
    if len(line) == 0:
        break
    try:
        conn = urllib.urlopen(line)
        print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
    except IOError:
        print epoch + "connection unsuccessful"

Thanks,

MHibbin

2
  • 1
    Have you tried catching the exception using a try...except block, and then warning and continuing? Commented Jul 3, 2012 at 8:17
  • @Alex Wilson, I had another play around... and changed my question... is this what you meant? Commented Jul 3, 2012 at 8:20

3 Answers 3

16

You could handle the exception where it is raised. Also, use a context manager when opening files, it makes for simpler code.

with open(hostsFile, 'r') as f:
    for line in f:
        line = line.strip()
        if not line:
            continue

        epoch = str(time.time())

        try:
            conn = urllib.urlopen(line)
            print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
        except IOError:
            print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
Sign up to request clarification or add additional context in comments.

6 Comments

I came to the same conclusion just after I posted the question . :-) Thanks for confirming What I had done.
for line in f is probably better here than a while loop.
Absolutely, and if you test each line against the empty string (line == '':) before stripping the newlines (\n) then you can handle blank lines in the source file without exiting prematurely.
@H. Dunlop: if line: or if not line: continue
@H.Dunlop, Thanks I have list of approx 100 urls to test (for internal network connectivity purposes (they are appliances with webUI). We would like to check them every 60 seconds. is it possible to speed this process up... can I use this script to generate a small script for each host (or would that be bad for performance)
|
4

You need to handle exception raised by urllib.urlopen(line), something like this.

try:
    f = file(hostsFile)
    while True:
        line = f.readline().strip()
        epoch = time.time()
        epoch = str(epoch)
        if len(line) == 0:
            break
        try:
           conn = urllib.urlopen(line)
        except IOError:
           print "Exception occured"
           pass
except IOError:
    epoch = time.time()
    epoch = str(epoch)
    print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
    sys.exit()
else:
    f.close()

1 Comment

Thanks for the answer. I've edited my question with a possible solution I found in the mean time.
1

You could try catching the exception inside the while loop as something like this.

try:
    f = file(hostsFile)
    while True:
        line = f.readline().strip()
        epoch = time.time()
        epoch = str(epoch)
        if len(line) == 0:
            break
        try:
            conn = urllib.urlopen(line)
            print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
        except:
            epoch = time.time()
            epoch = str(epoch)
            print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
except IOError:
    pass
else:
    f.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.