Python - Get HTTP Status Codes With urllib

Traverse urls in a file, and output their http status codes.

Page 1 of 1

4 Replies - 11269 Views - Last Post: 08 April 2010 - 04:56 PM Rate Topic: -----

#1 eazyigz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 09

Python - Get HTTP Status Codes With urllib

Posted 08 April 2010 - 07:34 AM

The following python module can be used to:
1) open a file that contains URL addresses on each line (must start with http://)
2) traverse each line and use urllib to open a network object denoted by a URL for reading
3) print the http status code for each URL

#!/usr/bin/env python
import urllib
import sys
 
FILE_NAME = "file"

def traverse_file():
    """Traverse each line (which is a URL) in a file, and output an http status code."""
    try:
        f = file(FILE_NAME)    # if no mode is specified, 'r'ead mode is assumed by default
        while True:
            line = f.readline()
            if len(line) == 0:    # Zero length indicates EOF
                break
            conn = urllib.urlopen(line)
            print ("Status code for " + line + ": " + str(conn.code) + "\n") 
    except IOError:
        print '\nFile does not exist'
        sys.exit()
    else:
        f.close()
        
   
if __name__=="__main__":
    traverse_file()

This post has been edited by eazyigz: 08 April 2010 - 02:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Python - Get HTTP Status Codes With urllib

#2 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Python - Get HTTP Status Codes With urllib

Posted 08 April 2010 - 02:18 PM

Is there a question?
Was This Post Helpful? 0
  • +
  • -

#3 eazyigz  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 05-November 09

Re: Python - Get HTTP Status Codes With urllib

Posted 08 April 2010 - 02:31 PM

I just wanted to share this code with anybody who may need it. I'm not sure how to do that properly on dreamincode.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Python - Get HTTP Status Codes With urllib

Posted 08 April 2010 - 03:09 PM

You would submit it as a snippet here.
Was This Post Helpful? 0
  • +
  • -

#5 programble  Icon User is offline

  • (cons :dic :head)

Reputation: 49
  • View blog
  • Posts: 1,315
  • Joined: 21-February 09

Re: Python - Get HTTP Status Codes With urllib

Posted 08 April 2010 - 04:56 PM

Or post it in the Share Your Project forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1