'Returns a connection to the url from which data can be retrieved with connection.read().
When the timeout amount of seconds is exceeded, raises a URLTimeout.
When an error occurs, raises a URLError (e.g. HTTP404NotFound).'

def open(self, timeout=10, proxy=None, user_agent=USER_AGENT, referrer=REFERRER, authentication=None):

    url = self.string
    if os.path.exists(url):
        return urllib.urlopen(url)
    post = (((self.method == POST) and self.querystring) or None)
    socket.setdefaulttimeout(timeout)
    handlers = []
    if proxy:
        handlers.append(urllib2.ProxyHandler({proxy[1]: proxy[0]}))
    handlers.append(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
    handlers.append(urllib2.HTTPHandler)
    urllib2.install_opener(urllib2.build_opener(*handlers))
    try:
        request = urllib2.Request(bytestring(url), post, {'User-Agent': user_agent, 'Referer': referrer})
        if (authentication is not None):
            request.add_header('Authorization', ('Basic    %s' % base64.encodestring(('%s:%s' % authentication))))
        return urllib2.urlopen(request)
    except urllib2.HTTPError as e:
        if (e.code == 301):
            raise HTTP301Redirect(src=e, url=url)
        if (e.code == 400):
            raise HTTP400BadRequest(src=e, url=url)
        if (e.code == 401):
            raise HTTP401Authentication(src=e, url=url)
        if (e.code == 403):
            raise HTTP403Forbidden(src=e, url=url)
        if (e.code == 404):
            raise HTTP404NotFound(src=e, url=url)
        if (e.code == 414):
            raise HTTP414RequestURITooLong(src=e, url=url)
        if (e.code == 420):
            raise HTTP420Error(src=e, url=url)
        if (e.code == 429):
            raise HTTP429TooMayRequests(src=e, url=url)
        if (e.code == 500):
            raise HTTP500InternalServerError(src=e, url=url)
        if (e.code == 503):
            raise HTTP503ServiceUnavailable(src=e, url=url)
        raise HTTPError(str(e), src=e, url=url)
    except httplib.BadStatusLine as e:
        raise HTTPError(str(e), src=e, url=url)
    except socket.timeout as e:
        raise URLTimeout(src=e, url=url)
    except socket.error as e:
        if (('timed    out' in str((e.args + ('', ''))[0])) or ('timed    out' in str((e.args + ('', ''))[1]))):
            raise URLTimeout(src=e, url=url)
        raise URLError(str(e), src=e, url=url)
    except urllib2.URLError as e:
        if ('timed    out' in str(e.reason)):
            raise URLTimeout(src=e, url=url)
        raise URLError(str(e), src=e, url=url)
    except ValueError as e:
        raise URLError(str(e), src=e, url=url)
