Skip to main content

JSON-RPC like protocol for Python 3

I published on Google code a JSON-RPC like protocol I developed for Python3. This is a brief description of the project that you can find here: https://github.com/kronwiz/krpc.

INTRODUCTION

The krpc protocol has been written for the Python3 language. The idea was to have a protocol that was easy to be used also from a web frontend, so it was natural to choose something based on a POST request and on JSON. For Python3 there isn't, as far as I know, a similar thing, so I wrote one from scratch. The krpc protocol is based on the JSON-RPC specification but it differs in some respects:
  • it supports file uploads;
  • to support file uploads it uses a standard POST request to call a method: the method invocation uses the JSON syntax and is contained in a JSON parameter; the files to be uploaded are contained in the following parts of a multipart message;
  • multiple method invocations in the same request are not supported.

INSTALLATION and LICENSE

The whole implementation is contained in only one file, so you can include it in your projects. However in the package there's the usual setup.py script that installs the file system wide.

The library is under the GNU LESSER GENERAL PUBLIC LICENSE Version 3. For more information about using/distributing the library see http://www.gnu.org/copyleft/lesser.html.

EXAMPLES

Before delving into details it's important to say that if you use Python then all the inner workings are hidden and you call a remote method the same way you call a standard method.
Let's start with the standard "hello world" example. This is a simple client:

  #!/usr/bin/env python3
  # -*- coding: utf8 -*-

  from krpc import KRPCClient, KRPCClientException

  def main ():
          c = KRPCClient ( "localhost", 8080 )

          try:
                  result = c.echo ( "hello world" )
                  print ( result )

          except KRPCClientException as e:
                  print ( "error: %s\nmessage: %s\ndata: %s\n" % ( e.code, e.message, e.info ) )

  main ()

You connect to a server by creating an instance of the KRPCClient class and specifying the server host and port:

  c = KRPCClient ( "localhost", 8080 )

The remote methods are called as methods of the KRPCClient instance:

  result = c.echo ( "hello world" )

If something goes wrong the KRPCClientException is raised. Its args attribute contains the error code, the error message and some additional info (if any).
If one of the method arguments is a file object then the protocol handles the upload to the server in a transparent way and on the server side "appears" a file object pointing to the contents of the uploaded file. There's no limit to the size of the files that can be transferred.

This is all you need to know as far as the client is concerned. And what about the server? This is the server implementing the "echo" method we called in the client:

  #!/usr/bin/env python3
  # -*- coding: utf8 -*-

  from krpc import KRPCServer

  class TestClass ( object ):
          def echo ( self, msg ):
                  return msg

  def main ():
          server = KRPCServer ( "localhost", 8080 )

          test_instance = TestClass ()
          server.register_instance ( test_instance )

          print ( 'Starting server, use <Ctrl-C> to stop' )
          #server.serve_forever ()

          quit = False
          while not quit:
                  server.handle_request ()


  main ()

To create a server you have to create an instance of the KRPCServer class specifying the host and the port to which clients should connect:

  server = KRPCServer ( "localhost", 8080 )

The server publishes all the methods contained in the object that is passed to the server register_instance method:

  server.register_instance ( class_instance )

In the example above the object is an instance of TestClass and this class contains only one method named "echo": this method returns the value passed in the "msg" parameter (well, it's not that useful but it's simple).
The server is started by calling the serve_forever method or, if you need more control, by writing a loop in which you call the handle_requestmethod that waits until a request is received and handles it. By the way: when a request arrives the server forks a new process to handle it, so requests are handled in parallel.
More documentation is contained in the "docs" directory of the project.

Comments

Most popular posts

Pairing the Raspberry Pi 3 with your Playstation 3 controller

While setting up the MAME emulator on the Raspberry Pi 3 I decided to experiment with the PS3 controller trying to pair it with the RPi. I found a useful guide here: http://holvin.blogspot.it/2013/11/how-to-setup-raspberry-pi-as-retro.html At section 4 the author describes how to compile sixpair utility, test that everything is working and compile the QtSixA tool. But there are some differences to be noted when working with the Raspberry Pi version 3. First, and most obvious, of all: the RPi 3 has already a Bluetooth device built in, so you don't have to plug a dongle in it, and it's compatible with the PS3 controller. 1. Sixpair The sixpair utility succeeds in coupling with the controller. But to test that it's working I had to test the js1 joystick port, and not the js0 as stated in the guide; so the actual command is: jstest /dev/input/js1 2. QtSixA The QtSixA download link must be changed, because the one shown doesn't compile with the latest

JSON Web Token Tutorial: An Example in Laravel and AngularJS

With the rising popularity of single page applications, mobile applications, and RESTful API services, the way web developers write back-end code has changed significantly. With technologies like AngularJS and BackboneJS, we are no longer spending much time building markup, instead we are building APIs that our front-end applications consume. Our back-end is more about business logic and data, while presentation logic is moved exclusively to the front-end or mobile applications. These changes have led to new ways of implementing authentication in modern applications. Authentication is one of the most important parts of any web application. For decades, cookies and server-based authentication were the easiest solution. However, handling authentication in modern Mobile and Single Page Applications can be tricky, and demand a better approach. The best known solutions to authentication problems for APIs are the OAuth 2.0 and the JSON Web Token (JWT). What is a JSON Web Token? A JSO

Software Release Management For Small Teams

Formalizing The Release Management Process (If There’s Any) In some team configurations, especially ones that are found in startups, there are no DevOps, nor infrastructure engineers, to provide support when releasing a new version of the product. Moreover, unlike large bureaucratic companies with defined formal processes, the CTO or Head of Software Development team in a startup is often not aware of the complexities of the software release management process; a few developers in the company may be aware of the complex details of the process, but not everyone. If this knowledge is not documented thoroughly , I believe it could result in confusion. In this article, I’ll try to provide some tips about how to formalize the release process, particularly from the developer’s point of view. Enter The Software Release Checklist You may be familiar with the idea of a checklist for some operations, as per the Checklist Manifesto , a book by Atul Gawande. I believe a formal release proc