Python httplib.error() Examples

The following are 3 code examples of httplib.error(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module httplib , or try the search function .
Example #1
Source File: httpclient.py    From bottle-auth with MIT License 4 votes vote down vote up
def _fetch(self, url, payload=None, method=GET, headers={}, allow_truncated=False):
        if method in [POST, PUT]:
            payload = payload or ''
            if 'Content-Type' not in headers:
                headers['Content-Type'] = 'application/x-www-form-urlencoded'
        else:
            payload = ''
        for redirect_number in xrange(MAX_REDIRECTS+1):
            scheme, host, path, params, query, fragment = urlparse.urlparse(url)
            try:
                if scheme == 'http':
                    connection = httplib.HTTPConnection(host)
                elif scheme == 'https':
                    connection = httplib.HTTPSConnection(host)
                else:
                    raise InvalidURLError('Protocol \'%s\' is not supported.')

                if query != '':
                    full_path = path + '?' + query
                else:
                    full_path = path

                adjusted_headers = {
                    'Content-Length': len(payload),
                    'Host': host,
                    'Accept': '*/*',
                }
                for header in headers:
                    adjusted_headers[header] = headers[header]

                try:
                    connection.request(method, full_path, payload,
                                       adjusted_headers)
                    http_response = connection.getresponse()
                    http_response_data = http_response.read()
                finally:
                    connection.close()

                if http_response.status in REDIRECT_STATUSES:
                    newurl = http_response.getheader('Location', None)
                    if newurl is None:
                        raise DownloadError('Redirect is missing Location header.')
                    else:
                        url = urlparse.urljoin(url, newurl)
                        method = 'GET'
                else:
                    response = Response()
                    response.body = http_response_data
                    response.status_code = http_response.status
                    response.request = Request(full_path)
                    response.headers = {}
                    for header_key, header_value in http_response.getheaders():
                        response.headers[header_key] = header_value
                    return response

            except (httplib.error, socket.error, IOError), e:
                response = Response()
                response.request = Request(full_path)
                response.error = str(e) or 'unknown error'
                return response 
Example #2
Source File: google.py    From NoobSec-Toolkit with GNU General Public License v2.0 4 votes vote down vote up
def search(self, dork):
        """
        This method performs the effective search on Google providing
        the google dork and the Google session cookie
        """

        gpage = conf.googlePage if conf.googlePage > 1 else 1
        logger.info("using Google result page #%d" % gpage)

        if not dork:
            return None

        url = "https://www.google.com/search?"
        url += "q=%s&" % urlencode(dork, convall=True)
        url += "num=100&hl=en&complete=0&safe=off&filter=0&btnG=Search"
        url += "&start=%d" % ((gpage - 1) * 100)

        try:
            conn = self.opener.open(url)

            requestMsg = "HTTP request:\nGET %s" % url
            requestMsg += " %s" % httplib.HTTPConnection._http_vsn_str
            logger.log(CUSTOM_LOGGING.TRAFFIC_OUT, requestMsg)

            page = conn.read()
            code = conn.code
            status = conn.msg
            responseHeaders = conn.info()
            page = decodePage(page, responseHeaders.get("Content-Encoding"), responseHeaders.get("Content-Type"))

            responseMsg = "HTTP response (%s - %d):\n" % (status, code)

            if conf.verbose <= 4:
                responseMsg += getUnicode(responseHeaders, UNICODE_ENCODING)
            elif conf.verbose > 4:
                responseMsg += "%s\n%s\n" % (responseHeaders, page)

            logger.log(CUSTOM_LOGGING.TRAFFIC_IN, responseMsg)
        except urllib2.HTTPError, e:
            try:
                page = e.read()
            except Exception, ex:
                warnMsg = "problem occurred while trying to get "
                warnMsg += "an error page information (%s)" % getSafeExString(ex)
                logger.critical(warnMsg)
                return None 
Example #3
Source File: google.py    From NoobSec-Toolkit with GNU General Public License v2.0 4 votes vote down vote up
def search(self, dork):
        """
        This method performs the effective search on Google providing
        the google dork and the Google session cookie
        """

        gpage = conf.googlePage if conf.googlePage > 1 else 1
        logger.info("using Google result page #%d" % gpage)

        if not dork:
            return None

        url = "https://www.google.com/search?"
        url += "q=%s&" % urlencode(dork, convall=True)
        url += "num=100&hl=en&complete=0&safe=off&filter=0&btnG=Search"
        url += "&start=%d" % ((gpage - 1) * 100)

        try:
            conn = self.opener.open(url)

            requestMsg = "HTTP request:\nGET %s" % url
            requestMsg += " %s" % httplib.HTTPConnection._http_vsn_str
            logger.log(CUSTOM_LOGGING.TRAFFIC_OUT, requestMsg)

            page = conn.read()
            code = conn.code
            status = conn.msg
            responseHeaders = conn.info()
            page = decodePage(page, responseHeaders.get("Content-Encoding"), responseHeaders.get("Content-Type"))

            responseMsg = "HTTP response (%s - %d):\n" % (status, code)

            if conf.verbose <= 4:
                responseMsg += getUnicode(responseHeaders, UNICODE_ENCODING)
            elif conf.verbose > 4:
                responseMsg += "%s\n%s\n" % (responseHeaders, page)

            logger.log(CUSTOM_LOGGING.TRAFFIC_IN, responseMsg)
        except urllib2.HTTPError, e:
            try:
                page = e.read()
            except Exception, ex:
                warnMsg = "problem occurred while trying to get "
                warnMsg += "an error page information (%s)" % getSafeExString(ex)
                logger.critical(warnMsg)
                return None