#!/bin/python ###--# Imports #--### import httplib from sys import argv,exit # Import functions to global scope! ###--# Functions #--### def puterror( errstring,exitcode=0 ): """Commonly used function to generate an error, close connection and exit""" # Close connection (its existence is not important) try: conn.close() except: pass # Print error message and get out print "[E] ERROR: " + errstring exit( exitcode ) ###--# Syntax Check #--### if ( len(argv) < 2 ) or ( argv[1][:7] != 'http://' ): print "Syntax: %s [filename]" % argv[0] exit(1) else: url = argv[1] if len(argv) > 2: filename = argv[2] else: filename = 'data.html' try: address = url[7:url[7:].index('/')+7] # If we have / later in the URL except ValueError: address = url[7:] # If we don't ###--# Logo & Initialization #--### print "---++ HTTP Retrieval Script ++---\n" conn = httplib.HTTPConnection( address ) ###--# Connection Phase #--### print "[#] Connecting to `%s`..." % address try: conn.connect() except (errno, errstring): puterror( errstring,1 ) ###--# Sending Request #--### print "[#] Requesting page `%s`..." % url try: conn.putrequest ('GET', url) conn.putheader ('User-Agent', 'HTTPGet 1.0 (Python)') conn.endheaders () except (errno, errstring): puterror( errstring,2 ) ###--# Getting Response #--### print "[#] Getting response..." res = conn.getresponse() print "[#] Status: %d %s" % ( res.status,res.reason ) if res.status == 200: print "[+] Website found!" # Displaying server headers print "[#] Headers:" for header in res.getheaders(): print " > %-20s : %s" % ( header[0],header[1] ) ###--# Storing Data #--### print "[#] Storing data to `%s`..." % filename try: fd = open( filename, 'wb' ) except (errno, errstring): puterror( errstring,4 ) # Read/Write data buffer = res.read() # Aren't doing directly so we can see the length below fd.write( buffer ) # Closing descriptors fd.close () conn.close () print "[#] %ld bytes downloaded!" % len( buffer )