def _check_url_exists(self, url, state):
        global http_client, urlparse, socket
        if http_client is None:
            from six.moves import http_client
        if urlparse is None:
            from six.moves.urllib import parse as urlparse
        if socket is None:
            import socket
        scheme, netloc, path, params, query, fragment = urlparse.urlparse(
            url, 'http')
        if params:
            path += ';' + params
        if query:
            path += '?' + query
        try:
            conn = (http_client.HTTPSConnection if scheme == 'https'
                    else http_client.HTTPConnection)(netloc)
            try:
                conn.request('HEAD', path)
                res = conn.getresponse()
            finally:
                conn.close()
        except http_client.HTTPException as e:
            e = str(e)
            if str is not six.text_type:  # Python 2
                try:
                    e = e.decode('utf-8')
                except UnicodeDecodeError:
                    try:
                        e = e.decode('latin-1')
                    except UnicodeDecodeError:
                        e = e.decode('ascii', 'replace')
            raise Invalid(
                self.message('httpError', state, error=e), state, url)
        except socket.error as e:
            e = str(e)
            if str is not six.text_type:  # Python 2
                try:
                    e = e.decode('utf-8')
                except UnicodeDecodeError:
                    try:
                        e = e.decode('latin-1')
                    except UnicodeDecodeError:
                        e = e.decode('ascii', 'replace')
            raise Invalid(
                self.message('socketError', state, error=e), state, url)
        else:
            if res.status == 404:
                raise Invalid(
                    self.message('notFound', state), state, url)
            if not 200 <= res.status < 500:
                raise Invalid(
                    self.message('status', state, status=res.status),
                    state, url)
dataset/ETHPy150Open formencode/formencode/formencode/validators.py/URL._check_url_exists/original