-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlrpc_server.repy
309 lines (227 loc) · 8.35 KB
/
xmlrpc_server.repy
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
<Program Name>
xmlrpc_server.repy
<Started>
April 2009
<Author>
Conrad Meyer <cemeyer@u.washington.edu>
<Purpose>
Provide a usable XML-RPC server interface for RePy code. This module
aims to be as similar in behavior to the Python SimpleXMLRPCServer as
is possible with RePy.
"""
include xmlparse.repy
include xmlrpc_common.repy
include urllib.repy
def _xmlrpc_server_lookup_ip(hostname):
# Looks up an ip address for a given hostmask. Throws an exception for
# invalid hostnames.
# The third item of the 3-tuple returned by gethostbyname_ex() is a list
# of ips for this hostname.
listen_ips = gethostbyname_ex(hostname)[2]
# If the host to listen on resolves to more than one ip, we can't behave
# in a deterministic manner. So, we except.
if len(listen_ips) != 1:
raise ValueError("Host '%s' resolved to more than one ip (or zero ips)" % hostname)
return listen_ips[0]
class xmlrpc_server_ClientError(Exception):
"""Exception raised when the client does something wrong."""
pass
class xmlrpc_server_ServerError(Exception):
"""Exception raised when the server does something wrong."""
pass
class xmlrpc_server_SimpleXMLRPCServer:
"""
<Purpose>
Provide a simple server-side API for programs wishing to expose their
functions to an XMLRPC client.
<Side Effects>
None.
<Exceptions>
ValueError if the host part of the address passed to the
constructor is a hostname that resolves to more than one ip.
<Example Use>
# create a server object
server = xmlrpc_server_SimpleXMLRPCServer(("localhost", 12345))
# register a function
server.register_function(pow)
# wait for clients to connect and call the function
server.serve_forever()
"""
def __init__(self, addr):
"""
<Purpose>
Provide a simple server-side API for programs wishing to expose their
functions to an XMLRPC client.
<Side Effects>
None.
<Exceptions>
ValueError if the host part of the address passed to the
constructor is a hostname that resolves to more than one ip.
TypeError if the passed variable addr isn't a container (list,
tuple, etc) of length 2.
<Example Use>
# create a server object
server = xmlrpc_server_SimpleXMLRPCServer(("localhost", 12345))
# register a function
server.register_function(pow)
# wait for clients to connect and call the function
server.serve_forever()
"""
# This is to ensure 'addr' is the correct type (something that has a
# length) and is the correct length, 2.
try:
if len(addr) != 2:
raise TypeError()
except TypeError, e:
raise TypeError("SimpleXMLRPCServer(): addr must be a 2-tuple " + \
"containing the hostname and port to listen on.")
self._listen_ip = _xmlrpc_server_lookup_ip(addr[0])
self._listen_port = addr[1]
self._callback_functions = {}
self._listen_comm = None
self._serverstopped = True
self._stoplock = getlock()
def register_function(self, function, name):
"""
<Purpose>
Register a callback function with this XMLRPC server.
<Arguments>
function:
The function to expose to the XMLRPC client.
name:
The name for this function that the client will call.
<Exceptions>
None.
<Side Effects>
Adds this function to this server's function lookup table.
<Returns>
None.
"""
self._callback_functions[name] = function
def serve_forever(self):
"""
<Purpose>
Serve forever (until someone tells the server to shut down).
<Arguments>
None.
<Exceptions>
None.
<Side Effects>
Starts listening on the host/port this server was created with and
serving requests.
<Returns>
None.
"""
self._listen_comm = waitforconn(self._listen_ip, self._listen_port, \
self._connect)
self._stoplock.acquire()
self._stopped = False
# Wait until shutdown() releases the lock.
self._stoplock.acquire()
self._stoplock.release()
def serve_nonblocking(self): # Better name suggestions are welcome.
"""
<Purpose>
Start serving. Returns control of the thread to the calling code.
<Arguments>
None.
<Exceptions>
None.
<Side Effects>
Starts listening on the host/port this server was created with and
serving requests.
<Returns>
None.
"""
self._listen_comm = waitforconn(self._listen_ip, self._listen_port, \
self._connect)
# Acquire the lock so we don't try to listen in more than one server
# thread. Also, shutdown() excepts to release the lock.
self._stoplock.acquire()
self._stopped = False
def shutdown(self):
"""
<Purpose>
Stop the server.
<Arguments>
None.
<Exceptions>
None.
<Side Effects>
Tells the serving loop to stop.
<Returns>
None.
"""
if not self._stopped:
self._stoplock.release()
stopcomm(self._listen_comm)
self._stopped = True
def _call_function(self, function_name, args):
# Call the registered function function_name with the given arguments
# (args) and return the result.
if self._callback_functions.has_key(function_name):
return self._callback_functions[function_name](*args)
# If we haven't gotten a result yet, the function doesn't exist.
raise xmlrpc_server_ClientError("xmlrpc_server error: client tried" + \
" to call a function that does not exist")
def _connect(self, remoteip, remoteport, socket, commhandle, listen_commhandle):
# The callback for client connections. Handle the request.
full_request_data = ""
while full_request_data.find("\r\n\r\n") < 0: # working around
# our sockets not knowing when they
# hit EOF
try:
full_request_data += socket.recv(4096)
except Exception:
# The client probably disconnected.
stopcomm(commhandle)
return
http_commandline, request_data = full_request_data.split("\r\n", 1)
http_command, http_path, http_protocol = http_commandline.split(None, 2)
if not (http_path == "/RPC2" or http_path == "/") or http_command != "POST":
raise xmlrpc_server_ClientError("xmlrpc_server error: client " + \
"sent invalid request (wrong path or wrong HTTP method)")
# Ignore protocol for now, we'll close connections from HTTP/1.1 clients
# that want keep-alive for now.
http_header_text, post_payload = request_data.split("\r\n\r\n", 1)
http_headers = {}
for line in http_header_text.split("\r\n"):
header_name, header_value = line.split(": ", 1)
http_headers[header_name.lower()] = header_value
# Sometimes the client hasn't finished sending us the request; in this
# case we keep trying to recieve more data until we can parse the
# data. There is probably a better way of doing this (one solution is
# to fix our client to send the request as one chunk of data so we
# don't have to do things like this; another is to add EOF and non-
# blocking support to our emulated_socket class).
while True:
try:
methodname, params = xmlrpc_common_call2python(post_payload)
break
except Exception:
post_payload += socket.recv(4096)
# Make the method call, get the result.
try:
result = self._call_function(methodname, params)
# Encode the result and send it back to the client.
xmlresponse = xmlrpc_common_response2xml(result)
except xmlrpc_server_ClientError:
xmlresponse = "<?xml version=\"1.0\"?><methodResponse><fault>" + \
_xmlrpc_common_value2xml(\
{"faultCode": 1, "faultString": "No such function."}) + \
"</fault></methodResponse>"
except Exception:
xmlresponse = "<?xml version=\"1.0\"?><methodResponse><fault>" + \
_xmlrpc_common_value2xml(\
{"faultCode": 2, "faultString": "Exception occurred."}) + \
"</fault></methodResponse>"
response = "HTTP/1.1 200 OK\r\nServer: SimpleXMLRPCServer on Repy\r\n" + \
"Content-type: text/xml\r\nContent-length: %i\r\n\r\n%s" % \
(len(xmlresponse), xmlresponse)
while True:
sent = socket.send(response)
if sent >= len(response):
break
response = response[sent:]
stopcomm(commhandle)