本文整理汇总了Python中twisted.protocols.basic.FileSender类的典型用法代码示例。如果您正苦于以下问题:Python FileSender类的具体用法?Python FileSender怎么用?Python FileSender使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FileSender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render_GET
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)
示例2: sendFile
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)
示例3: render_GET
def render_GET(self, request):
# no auth here on purpose, to allow anyone to view, even across home
# servers.
# TODO: A little crude here, we could do this better.
filename = request.path.split('/')[-1]
# be paranoid
filename = re.sub("[^0-9A-z.-_]", "", filename)
file_path = self.directory + "/" + filename
logger.debug("Searching for %s", file_path)
if os.path.isfile(file_path):
# filename has the content type
base64_contentype = filename.split(".")[1]
content_type = base64.urlsafe_b64decode(base64_contentype)
logger.info("Sending file %s", file_path)
f = open(file_path, 'rb')
request.setHeader('Content-Type', content_type)
d = FileSender().beginFileTransfer(f, request)
# after the file has been sent, clean up and finish the request
def cbFinished(ignored):
f.close()
request.finish()
d.addCallback(cbFinished)
else:
respond_with_json_bytes(
request,
404,
json.dumps(cs_error("Not found", code=Codes.NOT_FOUND)),
send_cors=True)
return server.NOT_DONE_YET
示例4: getshared_cb
def getshared_cb(data):
if len(data) == 0:
error = { 'status': {'error': "Invalid Request",
'message': "File does not exist."} }
request.write(json.dumps(error, sort_keys=True, encoding="utf-8"))
request.finish()
return
file_path = str(data[0][0]) + "/" + fileid
if not os.path.exists(file_path):
error = { 'status': {'error': "Invalid Request",
'message': "File does not exist."} }
request.write(json.dumps(error, sort_keys=True, encoding="utf-8"))
request.finish()
request.write(str(data[0][1])) # writing key
iv_plain = self.sid.decryptData(data[0][2]) # writing IV
print iv_plain
iv = self.sid.encryptData(iv_plain, pubkey)
request.write(iv)
file = open(file_path ,"r")
sender = FileSender()
sender.CHUNK_SIZE = 200
df = sender.beginFileTransfer(file, request)
df.addErrback(err)
df.addCallback(finishTrnf_cb, file)
示例5: start_stream
def start_stream():
file_sender = FileSender()
d = file_sender.beginFileTransfer(file_handle, lbry_file_creator)
d.addCallback(lambda _: stop_file(lbry_file_creator))
d.addCallback(lambda _: make_stream_desc_file(lbry_file_creator.stream_hash))
d.addCallback(lambda _: lbry_file_creator.stream_hash)
return d
示例6: render_GET
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")
示例7: connectionMade
def connectionMade(self):
self.transport.write('%s\r\n' % (self.insize))
sender = FileSender()
sender.CHUNK_SIZE = 2 ** 16
d = sender.beginFileTransfer(self.infile, self.transport,
self._monitor)
d.addCallback(self.cbTransferCompleted)
示例8: sendFile
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)
示例9: lineReceived
def lineReceived(self, line):
print 'sender %s' % line
line = line.strip()
if line == OK:
sender = FileSender()
sender.CHUNK_SIZE = 2 ** 16
deffered = sender.beginFileTransfer(self.fileObj, self.transport, None)
deffered.addCallback(self.success).addErrback(self.error)
示例10: on_write_status
def on_write_status(self, consumer):
content = self.generate_content()
buffer = StringIO(content)
sender = FileSender()
d = sender.beginFileTransfer(buffer, consumer)
d.addCallbacks(lambda _: self.on_write_completed(consumer),
self.on_error)
return d
示例11: _sendMail_data
def _sendMail_data(self, code, resp):
transfer = FileSender()
deferred = transfer.beginFileTransfer(self._requestParams['file'], self.transport, self.transformChunk)
deferred.addCallbacks(self.finishedFileTransfer, self.sendError)
self._expected = SUCCESS
self._okresponse = self._sendMail_sent
self._failresponse = self._sendMail_fail
示例12: start_streaming
def start_streaming(self):
file_sender = FileSender()
d = file_sender.beginFileTransfer(self.file_handle, self)
def stop_stream():
d = self.stop()
return d
d.addCallback(lambda _: stop_stream())
return d
示例13: test_pullFileConsumer
def test_pullFileConsumer(self):
fileToSend = StringIO(fileData)
clock = Clock()
consumer = FileConsumer(clock)
d = FileSender().beginFileTransfer(fileToSend, consumer)
finished = []
d.addCallback(finished.append)
while not finished:
clock.advance(1)
self.assertEqual(consumer.value(), fileData)
示例14: send_open_file
def send_open_file(openFile, request):
'''Use FileSender to asynchronously send an open file
[JBY] From: http://stackoverflow.com/questions/1538617/http-download-very-big-file'''
dd = FileSender().beginFileTransfer(openFile, request)
def cbFinished(ignored):
openFile.close()
request.finish()
dd.addCallback(cbFinished).addErrback(err)
示例15: _start_transfer
def _start_transfer(self, _discard):
if self.fileObj is None:
self._failure((2, "NO_FILE_LOADED"))
else:
logging.info("Started file transfer")
self.busy = True
fileProducer = FileSender()
fileProducer.CHUNK_SIZE = 65536
def_obj = fileProducer.beginFileTransfer(file=self.fileObj, consumer=self.transport)
def_obj.addCallback(self._done_transfer, True)
def_obj.addErrback(self._done_transfer, False)