本文整理汇总了Python中twisted.web.error.Error方法的典型用法代码示例。如果您正苦于以下问题:Python error.Error方法的具体用法?Python error.Error怎么用?Python error.Error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.web.error
的用法示例。
在下文中一共展示了error.Error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handleResponse
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def handleResponse(self, response):
if self.quietLoss:
return
if self.failed:
self.factory.noPage(
Failure(
error.Error(
self.status, self.message, response)))
if self.factory.method == b'HEAD':
# Callback with empty string, since there is never a response
# body for HEAD requests.
self.factory.page(b'')
elif self.length != None and self.length != 0:
self.factory.noPage(Failure(
PartialDownloadError(self.status, self.message, response)))
else:
self.factory.page(response)
# server might be stupid and not close connection. admittedly
# the fact we do only one request per connection is also
# stupid...
self.transport.loseConnection()
示例2: _http_do
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def _http_do(url, headers, timeout, method, params):
id_ = 0
try:
data = yield client.getPage(
url=url,
method='POST',
headers=dict(headers, **{'Content-Type': 'application/json'}),
postdata=json.dumps({
'jsonrpc': '2.0',
'method': method,
'params': params,
'id': id_,
}),
timeout=timeout,
)
except error.Error, e:
try:
resp = json.loads(e.response)
except:
raise e
示例3: handleResponse
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def handleResponse(self, response):
if self.quietLoss:
return
if self.failed:
self.factory.noPage(
failure.Failure(
error.Error(
self.status, self.message, response)))
if self.factory.method == 'HEAD':
# Callback with empty string, since there is never a response
# body for HEAD requests.
self.factory.page('')
elif self.length != None and self.length != 0:
self.factory.noPage(failure.Failure(
PartialDownloadError(self.status, self.message, response)))
else:
self.factory.page(response)
# server might be stupid and not close connection. admittedly
# the fact we do only one request per connection is also
# stupid...
self.transport.loseConnection()
示例4: handleResponse
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def handleResponse(self, response):
if self.quietLoss:
return
if self.failed:
self.factory.noPage(
failure.Failure(
error.Error(
self.status, self.message, response)))
elif self.length != None and self.length != 0:
self.factory.noPage(failure.Failure(
PartialDownloadError(self.status, self.message, response)))
else:
self.factory.page(response)
# server might be stupid and not close connection. admittedly
# the fact we do only one request per connection is also
# stupid...
self.transport.loseConnection()
示例5: handleFailure
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def handleFailure(f, request=None):
"""
Handle failure in callback chain, log and respond with traceback.
See also:
https://twistedmatrix.com/documents/16.0.0/core/howto/defer.html#errbacks
"""
if f.type is Error:
if request:
request.setResponseCode(int(f.value.status))
if hasattr(f.value, 'with_traceback'):
f.with_traceback = f.value.with_traceback
msg = None
if isinstance(f.value.response, Failure):
msg = f.value.response.getErrorMessage()
elif type(f.value.response) in types.StringTypes:
msg = f.value.response
request.messages.append({'type': 'error', 'message': msg})
else:
if request:
request.setResponseCode(http.INTERNAL_SERVER_ERROR)
request.setHeader('Content-Type', 'text/plain; charset=utf-8')
f.with_traceback = True
if hasattr(f, 'with_traceback') and f.with_traceback:
traceback = f.getTraceback()
log.error(traceback)
#f.trap(RuntimeError)
request.write(traceback.encode('utf-8'))
示例6: error_response
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def error_response(bucket, error_message='', code=http.BAD_REQUEST, with_traceback=False):
"""
Error handling method logging and returning appropriate stacktrace.
"""
# FIXME: Check for privacy. Do something more sane with the stacktrace
# or enable only when sending appropriate request arguments.
if with_traceback:
error_message += '\n' + last_error_and_traceback()
log.error(error_message)
bucket.request.setResponseCode(code)
#bucket.request.setHeader('Content-Type', 'text/plain; charset=utf-8')
return error_message.encode('utf-8')
示例7: handleResponseEnd
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def handleResponseEnd(self):
if self.length:
self.transmittingPage = 0
self.factory.noPage(
Failure(
PartialDownloadError(self.status)))
if self.transmittingPage:
self.factory.pageEnd()
self.transmittingPage = 0
if self.failed:
self.factory.noPage(
Failure(
error.Error(
self.status, self.message, None)))
self.transport.loseConnection()
示例8: noPage
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def noPage(self, reason):
"""
Close the storage file and errback the waiting L{Deferred} with the
given reason.
"""
if self.waiting:
self.waiting = 0
if self.file:
try:
self.file.close()
except:
log.err(None, "Error closing HTTPDownloader file")
self.deferred.errback(reason)
示例9: test_noMessageValidStatus
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def test_noMessageValidStatus(self):
"""
If no C{message} argument is passed to the L{Error} constructor and the
C{code} argument is a valid HTTP status code, C{code} is mapped to a
descriptive string to which C{message} is assigned.
"""
e = error.Error(b"200")
self.assertEqual(e.message, b"OK")
示例10: test_noMessageInvalidStatus
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def test_noMessageInvalidStatus(self):
"""
If no C{message} argument is passed to the L{Error} constructor and
C{code} isn't a valid HTTP status code, C{message} stays L{None}.
"""
e = error.Error(b"InvalidCode")
self.assertEqual(e.message, None)
示例11: test_str
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def test_str(self):
"""
C{str()} on an L{Error} returns the code and message it was
instantiated with.
"""
# Bytestring status
e = error.Error(b"200", b"OK")
self.assertEqual(str(e), "200 OK")
# int status
e = error.Error(200, b"OK")
self.assertEqual(str(e), "200 OK")
示例12: _downloadTest
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def _downloadTest(self, method):
dl = []
for (url, code) in [("nosuchfile", b"404"), ("error", b"401"),
("error?showlength=1", b"401")]:
d = method(url)
d = self.assertFailure(d, error.Error)
d.addCallback(lambda exc, code=code: self.assertEqual(exc.args[0], code))
dl.append(d)
return defer.DeferredList(dl, fireOnOneErrback=True)
示例13: testMethodNotFound
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def testMethodNotFound(self):
"""
Check that a non existing method return error 500.
"""
d = self.proxy().callRemote('doesntexist')
self.assertFailure(d, error.Error)
def cb(err):
self.assertEqual(int(err.status), 500)
d.addCallback(cb)
return d
示例14: noPage
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def noPage(self, reason):
"""
Close the storage file and errback the waiting L{Deferred} with the
given reason.
"""
if self.waiting:
self.waiting = 0
if self.file:
try:
self.file.close()
except:
self._log.failure("Error closing HTTPDownloader file")
self.deferred.errback(reason)
示例15: checkHTTPError
# 需要导入模块: from twisted.web import error [as 别名]
# 或者: from twisted.web.error import Error [as 别名]
def checkHTTPError(failure, url):
failure.trap(weberror.Error)
if failure.value.status=='401':
print('>>>', sys.stderr, failure.getErrorMessage())
username = input('用户名:')
password = getpass.getpass('密码:')
basicAuth = base64.encodestring('%s:%s' % (username, password))
authHeader = 'Basic' + basicAuth.strip()
# 加入验证信息后,尝试再次获取页面,
return client.getPage(url.encode(), headers={'Authorization':authHeader})
else:
return failure