-
Notifications
You must be signed in to change notification settings - Fork 25
/
service.py
55 lines (42 loc) · 1.49 KB
/
service.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
"""
Expose the Bloomberg Desktop API as an XML RPC server. Useful when running a virtual machine locally.
"""
from bbg3 import Terminal
from socket import gethostname
from SimpleXMLRPCServer import SimpleXMLRPCServer
from xmlrpclib import Binary, ServerProxy
import pickle
import logging
import sys
_logger = logging.getLogger(__name__)
def terminal_as_server(hostport=None):
class BbgServer(object):
def execute_request(self, brequest):
request = pickle.loads(brequest.data)
response = Terminal.execute_request(request)
return Binary(pickle.dumps(response))
methods = BbgServer()
hostport = hostport or (gethostname(), 3030)
server = SimpleXMLRPCServer(hostport)
server.register_instance(methods)
_logger.info('starting server on %s:%s' % hostport)
server.serve_forever()
class Client(object):
def __init__(self, url):
self.url = url
self.proxy = ServerProxy(url, allow_none=1)
def execte_request(self, request):
brequest = Binary(pickle.dumps(request))
bresponse = self.proxy.execute_request(brequest)
return pickle.loads(bresponse.data)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--hostport', help='hostport to bind server to')
args = parser.parse_args()
if args.hostport:
hostport = tuple( args.hostport.split(':') )
else:
hostport = None
# start the server
terminal_as_server(hostport)