本文整理汇总了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
示例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