Created
March 27, 2013 08:38
-
-
Save GregUK/5252722 to your computer and use it in GitHub Desktop.
Secure RPC server implementation which extends the SimpleXMLRPCServer to use SSL. This was adapted from http://code.activestate.com/recipes/496786-simple-xml-rpc-server-over-https/ and http://thecodewriter.blogspot.co.uk/2011/07/creating-secure-xml-rpc-server-in.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xmlrpclib | |
s = xmlrpclib.ServerProxy('https://127.0.0.1:15057') | |
print s.add(2,3) | |
print s.div(5,2) | |
print s.system.listMethods() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SocketServer | |
import BaseHTTPServer | |
import ssl | |
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCDispatcher | |
from os import path | |
try: | |
import fcntl | |
except ImportError: | |
fcntl = None | |
class SecureXMLRPCServer(BaseHTTPServer.HTTPServer, SimpleXMLRPCDispatcher): | |
def __init__(self, server_address, certfile, keyfile=None, | |
requestHandler=SimpleXMLRPCRequestHandler, | |
logRequests=True, allow_none=False, encoding=None, | |
bind_and_activate=True, ssl_version=ssl.PROTOCOL_SSLv23, | |
key_file_path=None): | |
self.logRequests = logRequests | |
self.cert_file_path = certfile | |
if not path.isfile(self.cert_file_path): | |
raise RuntimeError("Cert file path does not exist for {0}".format(self.cert_file_path)) | |
self.key_file_path = key_file_path | |
if not path.isfile(self.key_file_path): | |
raise RuntimeError("Key file path does not exist for {0}".format(self.key_file_path)) | |
self.ssl_version=ssl_version | |
SimpleXMLRPCDispatcher.__init__(self, allow_none, encoding) | |
# call TCPServer constructor | |
SocketServer.TCPServer.__init__(self, server_address, requestHandler, bind_and_activate) | |
def get_request(self): | |
newsocket, fromaddr = self.socket.accept() | |
# create a server-side SSL socket | |
sslsocket = ssl.wrap_socket(newsocket, server_side=True, certfile=self.cert_file_path, | |
keyfile=self.key_file_path, ssl_version=self.ssl_version) | |
return sslsocket, fromaddr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from secureRPC import SecureXMLRPCServer | |
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler | |
class MyRequestHandler(SimpleXMLRPCRequestHandler): | |
rpc_paths=('/RPC2') | |
server = SecureXMLRPCServer(("127.0.0.1", 15057), "ssl/cert.pem", key_file_path="ssl/key.pem", requestHandler=MyRequestHandler) | |
server.register_introspection_functions() | |
class MyFuncs: | |
def div(self, x, y): | |
return x // y | |
def add(self, x,y): | |
return x + y | |
server.register_instance(MyFuncs()) | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment