Python 自带方便xml.etree
,不需要额外的依赖即可输出简单的 XML。这是示例。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xml.etree.cElementTree as etree
import cherrypy
config = {
'global' : {
'server.socket_host' : '127.0.0.1',
'server.socket_port' : 8080,
'server.thread_pool' : 8
}
}
class App:
@cherrypy.expose
def index(self):
return '''<!DOCTYPE html>
<html>
<body>
<form action="/getxml" method="post">
<input type="text" name="key1" placeholder="Key 1" /><br/>
<input type="text" name="key2" placeholder="Key 2" /><br/>
<input type="text" name="key3" placeholder="Key 3" /><br/>
<select name="key4">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
<option value="4">Value 4</option>
</select><br/>
<button type="submit">Get XML</button>
</form>
</body>
</html>
'''
@cherrypy.expose
def getxml(self, **kwargs):
root = etree.Element('form')
for k, v in kwargs.items():
etree.SubElement(root, k).text = v
cherrypy.response.headers['content-type'] = 'text/xml'
return etree.tostring(root, encoding = 'utf-8')
if __name__ == '__main__':
cherrypy.quickstart(App(), '/', config)