本文整理汇总了Python中twisted.protocols.basic.FileSender.addErrback方法的典型用法代码示例。如果您正苦于以下问题:Python FileSender.addErrback方法的具体用法?Python FileSender.addErrback怎么用?Python FileSender.addErrback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.protocols.basic.FileSender
的用法示例。
在下文中一共展示了FileSender.addErrback方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sendFile
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def sendFile(request, filename, tb_path, ctype):
"""Send file to user.
Send file to user using producers and consumers system.
:param: filename (string)
:param: filepath (string)
:param: ctype (string) the value for content-type HTTP header
"""
request.setHeader(b'content-type', ctype)
request.setHeader(
b'content-disposition', 'attachment; filename=%s' %
filename
)
fp = FilePath(tb_path).child(filename).open()
d = FileSender().beginFileTransfer(fp, request)
def cbFinished(ignored):
fp.close()
request.finish()
d.addErrback(err).addCallback(cbFinished)
示例2: render_GET
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render_GET(self, request):
log.debug("request from %s: %s" % (self.userid, request.args))
if "f" in request.args:
fn = request.args["f"][0]
info = self.fileserver.storage.get(fn, False)
if info:
filename, mime, md5sum = info
log.debug("sending file type %s, path %s, md5sum %s" % (mime, filename, md5sum))
genfilename = util.generate_filename(mime)
request.setHeader("content-type", mime)
request.setHeader("content-length", os.path.getsize(filename))
request.setHeader("content-disposition", 'attachment; filename="%s"' % (genfilename))
request.setHeader("x-md5sum", md5sum)
# stream file to the client
fp = open(filename, "rb")
d = FileSender().beginFileTransfer(fp, request)
def finished(ignored):
fp.close()
request.finish()
d.addErrback(log.error).addCallback(finished)
return server.NOT_DONE_YET
# file not found in extra storage
else:
return self._quick_response(request, 404, "not found")
return self._quick_response(request, 400, "bad request")
示例3: sendFile
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def sendFile(self, dataObj):
pycb.log(logging.INFO, "===== def sendFile of cbRequest.py")
try:
#etag = dataObj.get_md5()
etag = pycb.config.bucket.get_md5()
pycb.log(logging.INFO, "=====## md5(etag) is %s"%etag)
if etag == None:
etag = self.etag
if etag == None:
etag = self.calcMd5Sum(dataObj)
dataObj.set_md5(etag)
self.setHeader(self.request, 'ETag', '"%s"' % (etag))
self.setResponseCode(self.request, 200, 'OK')
fp = dataObj
d = FileSender().beginFileTransfer(fp, self.request)
def cbFinished(ignored):
pycb.log(logging.INFO, "===== def cbFinished of cbRequest.py")
fp.close()
self.request.finish()
d.addErrback(err).addCallback(cbFinished)
except cbException, (ex):
ex.sendErrorResponse(self.request, self.requestId)
traceback.print_exc(file=sys.stdout)
pycb.log(logging.ERROR, "Error sending file %s" % (str(ex)), traceback)
示例4: render_GET
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render_GET(self, request):
#log.debug("request from %s: %s" % (self.userid, request.args))
if 'f' in request.args:
fn = request.args['f'][0]
info = self.fileserver.storage.get_extra(fn, self.userid)
if info:
(filename, mime, md5sum) = info
log.debug("sending file type %s, path %s, md5sum %s" % (mime, filename, md5sum))
genfilename = utils.generate_filename(mime)
request.setHeader('content-type', mime)
request.setHeader('content-length', os.path.getsize(filename))
request.setHeader('content-disposition', 'attachment; filename="%s"' % (genfilename))
request.setHeader('x-md5sum', md5sum)
# stream file to the client
fp = open(filename, 'rb')
d = FileSender().beginFileTransfer(fp, request)
def finished(ignored):
fp.close()
request.finish()
d.addErrback(err).addCallback(finished)
return server.NOT_DONE_YET
# file not found in extra storage
else:
return self.not_found(request)
return self.bad_request(request)
示例5: render_POST
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render_POST(self, request):
# Read POST into a temporary file
tmpfile = tempfile.TemporaryFile()
while True:
data = request.content.read(512 * 1024)
if not data:
break
tmpfile.write(data)
tmpfile.seek(0)
# Decrypt it
decrypted = self.server.key_manager.decrypt_file(tmpfile)
request.setHeader("content-type", "text/plain")
# Make sure we know the client
if not self.server.key_manager.is_gpg_key_in_ring(decrypted.key_id):
print("WARNING: Unknown client (Key ID: %s)" % (decrypted.key_id))
request.setResponseCode(511)
return "ERROR: I'm sorry sir, I don't know any Mr. %s" % (str(decrypted.key_id))
tmpfile.close()
# JSON parse
op = json.loads(decrypted.data)
# Operations
if op["op"] == "ping":
return self.server.key_manager.crypt("PONG", decrypted.key_id)
elif op["op"] == "get channel list":
channels = self.server.channel_manager.get_local_channels()
channels_json = json.dumps(channels)
channels_crypted = self.server.key_manager.crypt(channels_json, decrypted.key_id)
return channels_crypted
elif op["op"] == "get file list":
files = self.server.channel_manager.get_local_files(op["channels"])
files_json = json.dumps(files)
return self.server.key_manager.crypt(files_json, decrypted.key_id)
elif op["op"] == "download file":
f = self.server.channel_manager.get_filestream(op["channel"], op["file"])
p = self.server.key_manager.crypt_file_popen(f, decrypted.key_id)
d = FileSender().beginFileTransfer(p.stdout, request)
def cbFinished(ignored):
p.kill()
p.wait()
request.finish()
d.addErrback(err).addCallback(cbFinished)
return server.NOT_DONE_YET
示例6: render
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render(self,request):
self.isLeaf = False
#print request
#print dir(request)
print request.path
if (request.path.split('.')[-1] == 'mkv'):
print request.path.split('.')[-1]
request.setHeader('Content-Type',"application/x-mpegurl")
if (self.converter.checkStatus(request.path)):
self.converter.start('../../Torrents',request.path)
#f = open('playlist.m3u8','rb')
#print f.read()
#f.close()
#f = open('playlist.m3u8','rb')
#def cbFinished(ignored):
#f.close()
#request.finish()
playlist = self.converter.getPlaylist()
print playlist
return playlist
#d = FileSender().beginFileTransfer(f,request)
#d.addErrback(err).addCallback(cbFinished)
#return NOT_DONE_YET
elif(request.path.split('.')[-1] == 'ts'):
request.setHeader('Content-Type','video/MP2T')
print (request.path + '<--------')
self.converter.updateRecentSeg(request.path)
f = open(request.path[1:],'rb')
def cbFinished(ignored):
f.close()
request.finish()
d = FileSender().beginFileTransfer(f,request)
d.addErrback(err).addCallback(cbFinished)
return NOT_DONE_YET
else:
print ('travelled well')
print self.path
request.setHeader('Content-Type','video/octet-stream')
self.isLeaf = False
return static.File.render(self,request)
"""f = open(self.path+request.path,'rb')
示例7: sendOpenFile
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def sendOpenFile(request, openFile):
'''Use FileSender to asynchronously send an open file
[JBY] From: http://stackoverflow.com/questions/1538617/http-download-very-big-file'''
contentType, junk = mimetypes.guess_type(request.path)
request.setHeader('Content-Type', contentType if contentType else 'text/plain')
dd = FileSender().beginFileTransfer(openFile, request)
def cbFinished(ignored):
openFile.close()
request.finish()
dd.addErrback(err)
dd.addCallback(cbFinished)
return server.NOT_DONE_YET
示例8: render_POST
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render_POST (self, request):
# Read POST into a temporary file
tmpfile = tempfile.TemporaryFile()
while True:
data = request.content.read(512 * 1024)
if not data:
break
tmpfile.write (data)
tmpfile.seek(0)
# Decrypt it
decrypted = self.server.key_manager.decrypt_file (tmpfile)
request.setHeader("content-type", "text/plain")
# JSON parse
op = json.loads (decrypted.data)
print op
# Operations
if op['op'] == 'ping':
return self.server.key_manager.crypt ("PONG", decrypted.key_id)
elif op['op'] == 'get channel list':
channels = self.server.channel_manager.get_local_channels()
channels_json = json.dumps(channels)
return self.server.key_manager.crypt (channels_json, decrypted.key_id)
elif op['op'] == 'get file list':
files = self.server.channel_manager.get_local_files(op['channels'])
files_json = json.dumps(files)
return self.server.key_manager.crypt (files_json, decrypted.key_id)
elif op['op'] == 'download file':
f = self.server.channel_manager.get_filestream (op['channel'], op['file'])
p = self.server.key_manager.crypt_file_popen (f, decrypted.key_id)
d = FileSender().beginFileTransfer(p.stdout, request)
def cbFinished(ignored):
p.kill()
p.wait()
request.finish()
d.addErrback(err).addCallback(cbFinished)
return server.NOT_DONE_YET
示例9: attachment
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def attachment(self, request, attachment_id):
encoding = request.args.get('encoding', [None])[0]
filename = request.args.get('filename', [attachment_id])[0]
attachment = self.querier.attachment(attachment_id, encoding)
request.setHeader(b'Content-Type', b'application/force-download')
request.setHeader(b'Content-Disposition', bytes('attachment; filename=' + filename))
bytes_io = io.BytesIO(attachment['content'])
d = FileSender().beginFileTransfer(bytes_io, request)
def cbFinished(ignored):
bytes_io.close()
request.finish()
d.addErrback(err).addCallback(cbFinished)
return d
示例10: render_GET
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render_GET(self, request):
encoding = request.args.get('encoding', [None])[0]
filename = request.args.get('filename', [self.attachment_id])[0]
attachment = self.mail_service.attachment(self.attachment_id, encoding)
request.setHeader(b'Content-Type', b'application/force-download')
request.setHeader(b'Content-Disposition', bytes('attachment; filename=' + filename))
bytes_io = io.BytesIO(attachment['content'])
d = FileSender().beginFileTransfer(bytes_io, request)
def cb_finished(_):
bytes_io.close()
request.finish()
d.addErrback(err).addCallback(cb_finished)
return server.NOT_DONE_YET
示例11: render_GET
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def render_GET(self, request):
request.setHeader("content-type", "text/html")
response = self.get_matching_response(request.path)
if not response:
return None
if "static" in response:
file_path = response["static"]
contentType, junk = mimetypes.guess_type(file_path)
request.setHeader("Content-Type", contentType if contentType else "text/plain")
fp = open(file_path, "rb")
d = FileSender().beginFileTransfer(fp, request)
def cbFinished(ignored):
fp.close()
request.finish()
d.addErrback(err).addCallback(cbFinished)
return server.NOT_DONE_YET
return template % {"title": str(response["title"]), "body": str(response["body"])}
示例12: sendFile
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def sendFile(self, dataObj):
try:
etag = dataObj.get_md5()
if etag == None:
etag = self.calcMd5Sum(dataObj)
dataObj.set_md5(etag)
self.setHeader(self.request, 'ETag', '"%s"' % (etag))
self.setResponseCode(self.request, 200, 'OK')
fp = dataObj
d = FileSender().beginFileTransfer(fp, self.request)
def cbFinished(ignored):
fp.close()
self.request.finish()
d.addErrback(err).addCallback(cbFinished)
except cbException, (ex):
ex.sendErrorResponse(self.request, self.requestId)
traceback.print_exc(file=sys.stdout)
示例13: accessFile
# 需要导入模块: from twisted.protocols.basic import FileSender [as 别名]
# 或者: from twisted.protocols.basic.FileSender import addErrback [as 别名]
def accessFile(self,filepath,request):
try:
fileType = (filepath.split('.')[-1])
request.setHeader('Content-Type',"image/octet-stream")
if fileType == 'png':
request.setHeader('Content-Type',"image/png")
f = open('images/'+filepath,'rb')
elif fileType == 'ico':
request.setHeader('Content-Type',"image/ico")
f = open('images/favicon.ico','rb')
else:
print 'failed to get %s'%(name)
def cbFinished(ignored):
f.close()
request.finish()
d = FileSender().beginFileTransfer(f,request)
d.addErrback(err).addCallback(cbFinished)
return NOT_DONE_YET
except:
request.setHeader('Content-Type',"text/plain")
print request.path + ' failed'
return 'failed'