Skip to content

Instantly share code, notes, and snippets.

@akx
Last active December 17, 2015 09:09
Show Gist options
  • Save akx/5585377 to your computer and use it in GitHub Desktop.
Save akx/5585377 to your computer and use it in GitHub Desktop.
Slightly less simple uWSGI + XML-RPC + uWSGI RPC (madness!)
import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:58080')
print s.system.listMethods()
print s.pow(2,3)
print s.sum([2,3])
print s.hello("world")
# In addition to HTTP on 58080, we'll have to have a normal uwsgi socket on some other port
# and more than one worker, so we can make the RPC call from the HTTP-based request:
uwsgi --wsgi uw_xmlrpc --http :58080 --socket :58081 --master --processes 2
from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
import uwsgi
import time
#### Register stuff in uWSGI's RPC (this could well be in another app or another server!)
def hello(name, *args):
time.sleep(len(str(name)) / 10.0) # Let's make it look like it does something important :P
return "Hello, %s! This is uWSGI RPC." % name
uwsgi.register_rpc("hello", hello)
#### Configure the XML-RPC dispatcher
def shim(server, func_name):
""" Return a function that uses uWSGI RPC to call a function named `func_name` on `RPC_SERVER`. """
def shimmed(*args):
return uwsgi.rpc(server, func_name, *args)
shimmed.__name__ = func_name # Fool SimpleXMLRPCDispatcher.register_function
return shimmed
dispatch = SimpleXMLRPCDispatcher()
dispatch.register_introspection_functions()
dispatch.register_function(pow)
dispatch.register_function(sum)
dispatch.register_function(shim("127.0.0.1:58081", "hello")) # instead of 127.... we could use a remote node
def application(environ, start_response):
if environ["REQUEST_METHOD"] != "POST":
start_response("500 Error", ())
return ["Only POST allowed"]
try:
length = int(environ.get('CONTENT_LENGTH', None))
except (TypeError, ValueError):
length = -1
request_text = environ["wsgi.input"].read(length)
response = dispatch._marshaled_dispatch(request_text)
start_response("200 OK", [("Content-Type", "text/xml"), ("Content-Length", str(len(response)))])
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment