Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 5ab2b051c4
Fetching contributors…

Cannot retrieve contributors at this time

45 lines (29 sloc) 1.376 kB
layout title
Serving XML

Serving XML

Other languages: français | ...


How to serve XML files correctly?

This is needed when you have a third-party application posting data to your service and expecting some kind of XML response.


Create your XML template with the XML file you want to serve (i.e. response.xml). If the XML has any variables, use the corresponding templating code. This is just an example:

$def with (code)
<?xml version="1.0"?>

To serve this file, create a standard Web.Py program (i.e. and use the following code. Be aware that you should use web.header('Content-Type', 'text/xml') to tell the client that you are sending a XML file. (You don't need to set the header explicitly for XML files if your template file has the .xml extension.)

import web

render = web.template.render('templates/', cache=False)

urls = (
    '/(.*)', 'index'

app = web.application(urls, globals())

class index:
    def GET(self, code):
        web.header('Content-Type', 'text/xml')
        return render.response(code)

web.webapi.internalerror = web.debugerror
if __name__ == '__main__':
Jump to Line
Something went wrong with that request. Please try again.