class downloadHandler(RequestHandler):
def fetdata(self,inde):
with MogoContext() as mongo:
res=list(mongo.db['datas'].find().limit(10).skip(inde*10))
for d in res:
yield d
def get(self):
page=1
self.set_header('Content-Type', 'text/csv; charset=utf-8')
self.set_header('Content-Disposition', 'attachment; filename="data.csv"')
self.set_header('Cache-Control', 'no-cache')
while True:
data= list(self.fetdata(page))
if len(data) == 0:
break
stream = io.StringIO()
writer = csv.writer(stream)
for row in data:
writer.writerow(list(row.values()))
self.write(stream.getvalue().encode('gbk'))
self.flush()
page+=1
self.finish()
标签:set,读取,stream,tornado,self,writer,header,csv,data From: https://blog.51cto.com/u_13495873/6392429