0

I am having some problems understanding how a application written in python jsonrpc2 is related to a wgsi application.

I have a json rpc test application in a file called greeting.py

It is a simple test case

def hello(name=None,greeting=None):
    # Print to stdout the greeting
    result =  "From jsonrpc you have: {greeting} , {name}".format(greeting=greeting,name=name)
    # print result
    # You can basically now return the string result
    return  result

Using the jsonrpc2 module I am able to POST json to this function which then returns a json response.

Sample post :

 self.call_values_dict_webpost = dict(zip(["jsonrpc","method","id","params"],["2.0","greeting.hello","2",["Hari","Hello"]]))

Response returned as json:

u"jsonrpc": u"2.0", u"id": u"2", u"result": u"From jsonrpc you have: Hello , Hari"

I start the server with an entry point defined in the jsonrpc2 module which essentially does the following

from jsonrpc2 import JsonRpcApplication
from wsgiref.simple_server import make_server
app = JsonRpcApplication()
app.rpc.add_module("greeting")
httpd = make_server(host, port, app)
httpd.serve_forever()

I can currently run this jsonrpc2 server as a standalone "web app" and test it approproately.

I wanted to understand how to go from this simple function web app to a wsgi web app that reads and writes json without using a web framework such as flask or django ( which I know some of)

I am looking for whether there is a simple conceptual step that makes my function above compatible with a wsgi "callable" : or am I just better off using flask or django to read/receive json "POST" and write json response.

harijay
  • 11,303
  • 12
  • 38
  • 52

1 Answers1

2

I don't know that particular module, but it looks like your app object is the WSGI application. All you do in that code is instantiate the app, then create a server for it via wsgiref. So instead of doing that, just point your real WSGI server - Apache/mod_wsgi, or gunicorn, or whatever - to that app object in exactly the same way as you would serve Flask or Django.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Thank you, I finally figured it out thanks to your reply. I had to start the server with uwsgi --http :9090 --wsgi-file wsgi.py --callable app". The file I created "wsgi.py" has the same code as entry point code mentioned above. – harijay Sep 20 '14 at 01:28