파이썬은 편리한 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)