-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
executable file
·111 lines (101 loc) · 3.67 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
#
# (C) Copyright 2007 Novell, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Bart Whiteley <bwhiteley suse.de>
from twisted.web import http
import pywbem
from pywbem import tupleparse
import cimserver
from cStringIO import StringIO
import sys
cxd = None
class MyRequestHandler(http.Request):
def process(self):
print self.content.read()
self.content.seek(0,0)
try:
tt = tupleparse.xml_to_tupletree(self.content.read())
tt = tupleparse.parse_cim(tt)
mid = tt[2][1]['ID']
tt = tt[2][2][0][2]
method = tt[0]
rmethod = method == 'METHODCALL' and 'METHODRESPONSE' or \
'IMETHODRESPONSE'
op = tt[1]['NAME']
print 'operation:', op
if method == 'METHODCALL':
fn = 'invokemethod'
else:
fn = op.lower()
try:
fn = getattr(cxd, fn)
except AttributeError:
raise pywbem.CIMError(pywbem.CIM_ERR_FAILED,
'Unknown operation: %s' % op)
resp = """<?xml version="1.0" ?>
<CIM CIMVERSION="2.0" DTDVERSION="2.0">
<MESSAGE ID="%s" PROTOCOLVERSION="1.0">
<SIMPLERSP>
<%s NAME="%s">""" % (mid, rmethod, op)
if method == 'IMETHODCALL':
resp+= '<IRETURNVALUE>'
output = StringIO()
output.write(resp)
fn(tt, output)
if method == 'IMETHODCALL':
resp = '</IRETURNVALUE>'
else:
resp = ''
resp+= """</%s>
</SIMPLERSP>
</MESSAGE>
</CIM>""" % rmethod
output.write(resp)
self.write(output.getvalue().encode('utf8'))
output.close()
except pywbem.CIMError, arg:
num = arg.args[0]
descr = ''
if len(arg.args) > 1:
descr = arg.args[1]
import traceback
traceback.print_exc(file=sys.stdout)
print 'sending error', descr
msg = """<?xml version="1.0" encoding="utf-8" ?>
<CIM CIMVERSION="2.0" DTDVERSION="2.0">
<MESSAGE ID="%s" PROTOCOLVERSION="1.0">
<SIMPLERSP>
<%s NAME="%s">
<ERROR CODE="%s" DESCRIPTION="%s"/>
</%s>
</SIMPLERSP>
</MESSAGE>
</CIM>""" % (mid, rmethod, op, num, descr, rmethod)
self.write(msg.encode('utf8'))
print 'calling finish()'
self.finish()
class MyHttp(http.HTTPChannel):
requestFactory = MyRequestHandler
class MyHttpFactory(http.HTTPFactory):
protocol = MyHttp
if __name__ == '__main__':
global cxd
cxd = cimserver.CIMXMLDispatch()
from twisted.internet import reactor
reactor.listenTCP(8000, MyHttpFactory())
reactor.run()