Answer the question
In order to leave comments, you need to log in
How to download a generated file in Python?
I programmatically generate xml and save it to my disk
s = etree.ElementTree(root)
s.write( "keywordgroups.xml", encoding='utf8')
Answer the question
In order to leave comments, you need to log in
s = etree.ElementTree(root)
response = HttpResponse(etree.tostring(s, encoding="utf8", method='xml'), 'application/xml; charset=UTF-8')
response['Content-Disposition'] = 'attachment; filename=keywords.xml'
return response
Take any interpretation of a python web server and serve the generated file through it.
For example, via aiohttp (checked, working)
from aiohttp import web
from concurrent.futures import ThreadPoolExecutor
import xml.etree.ElementTree as ElementTree
def get_xml():
root = ElementTree.Element('root')
b = ElementTree.SubElement(root, 'b')
c = ElementTree.SubElement(root, 'c')
d = ElementTree.SubElement(root, 'd')
xmlstr = ElementTree.tostring(root, encoding='utf8', method='xml')
return xmlstr
async def handle(request):
with ThreadPoolExecutor() as executor:
xmlstr = await request.app.loop.run_in_executor(executor, get_xml)
return web.Response(body=xmlstr, content_type="text/xml")
app = web.Application()
app.router.add_get('/', handle)
web.run_app(app, port=8080)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question