| | 1 | = JSON-RPC for python = |
| | 2 | |
| | 3 | A json-rpc package which implements JSON-RPC. |
| | 4 | |
| | 5 | You can [http://json-rpc.org/browser/tags/py-jsonrpc/ browse] the code or check it out |
| | 6 | using subversion from the [http://svn.json-rpc.org/tags/py-jsonrpc/ svn repository]. |
| | 7 | |
| | 8 | This implementation is currently in development. |
| | 9 | |
| | 10 | |
| | 11 | It provides functionality for making JSON-RPC services available through CGI. |
| | 12 | In addition it provides a mod_python handler for providing services through mod_python which is more perfomant than CGI. There is also some JSON-RPC over sockets implementation. |
| | 13 | |
| | 14 | == Installation == |
| | 15 | |
| | 16 | You will need [http://cheeseshop.python.org/pypi/simplejson simplejson] installed for the jsonrpc to run. |
| | 17 | |
| | 18 | Download jsonrpc and install it into python's {{{lib/site-packages/}}}. |
| | 19 | |
| | 20 | On Unix/Linux do the following as '''root''': |
| | 21 | {{{ |
| | 22 | $ easy_install simplejson |
| | 23 | $ svn co http://svn.json-rpc.org/trunk jsonrpc |
| | 24 | $ cd jsonrpc |
| | 25 | $ python setup.py install |
| | 26 | }}} |
| | 27 | |
| | 28 | == mod_python handler == |
| | 29 | |
| | 30 | The idea is that you only need to create a python module containing a service, put it in a directory and the rest is handled by the mod_python handler. |
| | 31 | |
| | 32 | For it to work you need to tell apache to add a python handler for a certain location or directory. |
| | 33 | It also works in .htaccess files: |
| | 34 | |
| | 35 | Example: |
| | 36 | |
| | 37 | {{{ |
| | 38 | <Directory /var/www/services > |
| | 39 | AddHandler mod_python py |
| | 40 | PythonHandler jsonrpc.apacheServiceHandler |
| | 41 | PythonPath "sys.path+['path_to_jsonrpc_implementation']" |
| | 42 | </Directory> |
| | 43 | }}} |
| | 44 | |
| | 45 | You may have to set the {{{PythonPath}}} in the code above, so that mod_python can find the hanlder (see the [http://www.modpython.org/live/current/doc-html/dir-other-pp.html mod_python documentation]). |
| | 46 | |
| | 47 | Now, place a python file in the directory {{{/var/www/services/echo.py}}}. |
| | 48 | If that file is requested the {{{jsonrpc.apacheServiceHandler}}} will import the python module for that file and look for a service inside that module. |
| | 49 | |
| | 50 | You have 3 options to tell the handler what service you want to provide in your module. |
| | 51 | |
| | 52 | You either expose: |
| | 53 | * a service object called '''{{{service}}}''' |
| | 54 | * a function '''{{{getService()}}}''' which returns a service object |
| | 55 | * a class '''{{{Service}}}''' which will be used to create a service object |
| | 56 | |
| | 57 | Example service script {{{/var/www/services/echo.py}}}: |
| | 58 | {{{ |
| | 59 | #!python |
| | 60 | |
| | 61 | class Service: |
| | 62 | def echo(self, msg): |
| | 63 | return msg |
| | 64 | |
| | 65 | service = Service() |
| | 66 | }}} |
| | 67 | |
| | 68 | |
| | 69 | All methods of the service object are now available through JSON-RPC over HTTP:[[br]] |
| | 70 | (service url:{{{http://yourdomain/services/echo.py}}}). |
| | 71 | |
| | 72 | |
| | 73 | == CGI handler == |
| | 74 | |
| | 75 | To be used to server services in a CGI environment. |
| | 76 | |
| | 77 | Example CGI script |
| | 78 | {{{ |
| | 79 | #!python |
| | 80 | #!/usr/bin/env python |
| | 81 | |
| | 82 | class Service: |
| | 83 | def echo(self, msg): |
| | 84 | return msg |
| | 85 | |
| | 86 | from jsonrpc.cgihandler import handleCGIRequest |
| | 87 | |
| | 88 | handleCGIRequest(Service()) |
| | 89 | }}} |
| | 90 | |
| | 91 | |
| | 92 | == TCP service server == |
| | 93 | |
| | 94 | To be used to serve JSON-RPC over TCP. |
| | 95 | |
| | 96 | ''This is under development'' |
| | 97 | |
| | 98 | {{{ |
| | 99 | #!python |
| | 100 | |
| | 101 | class Service: |
| | 102 | def echo(self, msg): |
| | 103 | return msg |
| | 104 | |
| | 105 | |
| | 106 | from jsonrpc.socketserver import ThreadedTCPServiceServer |
| | 107 | |
| | 108 | ThreadedTCPServiceServer(Service()).serve(('localhost', 5766)) |
| | 109 | }}} |
| | 110 | |
| | 111 | [[br]] |