How to use the mechanize.polyglot.URLError function in mechanize

To help you get started, we’ve selected a few mechanize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
port = int(port)

        # username/password handling
        user, host = splituser(host)
        if user:
            user, passwd = splitpasswd(user)
        else:
            passwd = None
        host = unquote(host)
        user = unquote(user or '')
        passwd = unquote(passwd or '')

        try:
            host = socket.gethostbyname(host)
        except socket.error as msg:
            raise URLError(msg)
        path, attrs = splitattr(req.get_selector())
        dirs = path.split('/')
        dirs = list(map(unquote, dirs))
        dirs, file = dirs[:-1], dirs[-1]
        if dirs and not dirs[0]:
            dirs = dirs[1:]
        try:
            fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout)
            type = file and 'I' or 'D'
            for attr in attrs:
                attr, value = splitvalue(attr)
                if attr.lower() == 'type' and \
                   value in ('a', 'A', 'i', 'I', 'd', 'D'):
                    type = value.upper()
            fp, retrlen = fw.retrfile(file, type)
            headers = ""
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
proxy_auth_hdr = "Proxy-Authorization"
            if proxy_auth_hdr in headers:
                tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr]
                # Proxy-Authorization should not be sent to origin server.
                del headers[proxy_auth_hdr]
            set_tunnel(req._tunnel_host, headers=tunnel_headers)

        if self.parent.finalize_request_headers is not None:
            self.parent.finalize_request_headers(req, headers)

        try:
            h.request(str(req.get_method()), str(req.get_selector()), req.data,
                      headers)
            r = h.getresponse()
        except socket.error as err:  # XXX what error?
            raise URLError(err)

        # Pick apart the HTTPResponse object to get the addinfourl
        # object initialized properly.
        fp = create_readline_wrapper(r)

        resp = closeable_response(
            fp, r.msg, req.get_full_url(), r.status, r.reason,
            getattr(r, 'version', None))
        return resp
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
mtype = mimetypes.guess_type(file)[0]
            headers = create_response_info(BytesIO(
                ('Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                    (mtype or 'text/plain', size, modified)).encode(
                        'iso-8859-1')))
            if host:
                host, port = splitport(host)
            if not host or (
                    not port and socket.gethostbyname(host) in self.get_names()
            ):
                fp = open(localfile, 'rb')
                return closeable_response(fp, headers, 'file:' + file)
        except OSError as msg:
            # urllib2 users shouldn't expect OSErrors coming from urlopen()
            raise URLError(msg)
        raise URLError('file not on local host')
github Masood-M / yalih / mechanize / _urllib2_fork.py View on Github external
proxy_auth_hdr = "Proxy-Authorization"
            if proxy_auth_hdr in headers:
                tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr]
                # Proxy-Authorization should not be sent to origin server.
                del headers[proxy_auth_hdr]
            set_tunnel(req._tunnel_host, headers=tunnel_headers)

        if self.parent.finalize_request_headers is not None:
            self.parent.finalize_request_headers(req, headers)

        try:
            h.request(str(req.get_method()), str(req.get_selector()), req.data,
                      headers)
            r = h.getresponse()
        except socket.error as err:  # XXX what error?
            raise URLError(err)

        # Pick apart the HTTPResponse object to get the addinfourl
        # object initialized properly.
        fp = create_readline_wrapper(r)

        resp = closeable_response(
            fp, r.msg, req.get_full_url(), r.status, r.reason,
            getattr(r, 'version', None))
        return resp
github Masood-M / yalih / mechanize / _urllib2_fork.py View on Github external
def ftp_open(self, req):
        import ftplib
        import mimetypes
        host = req.get_host()
        if not host:
            raise URLError('ftp error: no host given')
        host, port = splitport(host)
        if port is None:
            port = ftplib.FTP_PORT
        else:
            port = int(port)

        # username/password handling
        user, host = splituser(host)
        if user:
            user, passwd = splitpasswd(user)
        else:
            passwd = None
        host = unquote(host)
        user = unquote(user or '')
        passwd = unquote(passwd or '')
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
def do_open(self, http_class, req):
        """Return an addinfourl object for the request, using http_class.

        http_class must implement the HTTPConnection API from httplib.
        The addinfourl return value is a file-like object.  It also
        has methods and attributes including:
            - info(): return a HTTPMessage object for the headers
            - geturl(): return the original request URL
            - code: HTTP status code
        """
        host_port = req.get_host()
        if not host_port:
            raise URLError('no host given')

        h = http_class(host_port, timeout=req.timeout)
        h.set_debuglevel(self._debuglevel)

        headers = OrderedDict(req.headers)
        for key, val in iteritems(req.unredirected_hdrs):
            headers[key] = val
        # We want to make an HTTP/1.1 request, but the addinfourl
        # class isn't prepared to deal with a persistent connection.
        # It will try to read all remaining data from the socket,
        # which will block while the server waits for the next request.
        # So make sure the connection gets closed after the (only)
        # request.
        headers["Connection"] = "close"
        # httplib in python 2 needs str() not unicode() for all request
        # parameters
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(file)[0]
            headers = create_response_info(BytesIO(
                ('Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                    (mtype or 'text/plain', size, modified)).encode(
                        'iso-8859-1')))
            if host:
                host, port = splitport(host)
            if not host or (
                    not port and socket.gethostbyname(host) in self.get_names()
            ):
                fp = open(localfile, 'rb')
                return closeable_response(fp, headers, 'file:' + file)
        except OSError as msg:
            # urllib2 users shouldn't expect OSErrors coming from urlopen()
            raise URLError(msg)
        raise URLError('file not on local host')
github Masood-M / yalih / mechanize / _urllib2_fork.py View on Github external
modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(file)[0]
            headers = create_response_info(BytesIO(
                ('Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                    (mtype or 'text/plain', size, modified)).encode(
                        'iso-8859-1')))
            if host:
                host, port = splitport(host)
            if not host or (
                    not port and socket.gethostbyname(host) in self.get_names()
            ):
                fp = open(localfile, 'rb')
                return closeable_response(fp, headers, 'file:' + file)
        except OSError as msg:
            # urllib2 users shouldn't expect OSErrors coming from urlopen()
            raise URLError(msg)
        raise URLError('file not on local host')
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
def open_local_file(self, req):
        import email.utils as emailutils
        import mimetypes
        host = req.get_host()
        file = req.get_selector()
        try:
            localfile = url2pathname(file)
        except IOError as err:
            # url2pathname raises this on windows for bad urls
            raise URLError(err)
        try:
            stats = os.stat(localfile)
            size = stats.st_size
            modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(file)[0]
            headers = create_response_info(BytesIO(
                ('Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                    (mtype or 'text/plain', size, modified)).encode(
                        'iso-8859-1')))
            if host:
                host, port = splitport(host)
            if not host or (
                    not port and socket.gethostbyname(host) in self.get_names()
            ):
                fp = open(localfile, 'rb')
                return closeable_response(fp, headers, 'file:' + file)