當前位置: 首頁>>代碼示例>>Python>>正文


Python httplib.error方法代碼示例

本文整理匯總了Python中httplib.error方法的典型用法代碼示例。如果您正苦於以下問題:Python httplib.error方法的具體用法?Python httplib.error怎麽用?Python httplib.error使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在httplib的用法示例。


在下文中一共展示了httplib.error方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _fetch

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import error [as 別名]
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 
開發者ID:avelino,項目名稱:bottle-auth,代碼行數:62,代碼來源:httpclient.py

示例2: search

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import error [as 別名]
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 
開發者ID:krintoxi,項目名稱:NoobSec-Toolkit,代碼行數:48,代碼來源:google.py


注:本文中的httplib.error方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。