0

The following code runs when I execute the program outside a proxy. When I run this program while on my work's proxy using a Windows 7 Operating System, I either get "[Errno 11004]" with Command Prompt or "[Errno 8]" with Cygwin.

The goal of this program is to be a portable executable which the executive could use to capture HTTP responses and URL redirects of our companies owned websites.

#!/bin/python
import urllib, urllib2, sys, logging, time

# Variables 
s = time.strftime('%Y%m%d%H%M%S')
f = open("list.txt",'r')

# Logging
class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("assets_"+s+".txt", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)  

# Capture logging class
sys.stdout = Logger()

# Text file header
print "ASSET, STATUS, REDIRECT, DATE/TIME"

# Feed program 16,000 URLs
for url in f.readlines():
    try:
        http_connection = 'http://' + (url)
        connection = urllib2.urlopen(http_connection)
        print (url).rstrip("\n"), ",", connection.getcode(), ",", connection.geturl(), ",", (s)
        connection.close()
    except urllib2.URLError as e:
        print e.reason
Christopher
  • 125
  • 1
  • 8
  • Does [this question](http://stackoverflow.com/questions/4847649/opening-websites-using-urllib2-from-behind-corporate-firewall-11004-getaddrinf) help? – bbayles Aug 22 '13 at 23:51

2 Answers2

0

Looks like an environment or data issue. You mentioned you're running this on a proxy - any restrictions? work on just getting this line to work.

connection = urllib2.urlopen(http_connection)

-1

Here is the final solution. Found out the errors where not proxy related.

#!/bin/python

import urllib2, sys, logging, time, errno, unittest

# Variables
s = time.strftime('%Y%m%d%H%M%S')
f = open("list.txt",'r')

# Create file text file for importing into database
class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("assets_"+s+".txt", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)  

# Start capture of screen output for database file
sys.stdout = Logger()
print "UID, ASSET, STATUS, REDIRECT, DATE/TIME"

# Loop to capture URL status and redirect information
for assets in f.readlines():
    url = assets.split()
    if len(url) > 1:
        try:
            http = 'http://' + url[1]           
            http_connection = urllib2.urlopen(http, timeout=5)
        except IOError, e:
            if e.errno == None:         
                try:
                    www = 'http://www.' + url[1]
                    http_connection = urllib2.urlopen(www, timeout=5)
                except IOError, e:
                    print url[0], ",", url[1].rstrip("\n"), ",", "", ",", e.errno, ",", (s)
                else:
                    print url[0], ",", url[1].rstrip("\n"), ",", http_connection.getcode(), ",", http_connection.geturl(), ",", (s)
        else:
            print url[0], ",", url[1].rstrip("\n"), ",", http_connection.getcode(), ",", http_connection.geturl(), ",", (s)
Christopher
  • 125
  • 1
  • 8