python27 License PyPI version PyPI downloads

flaskJSONRPCServer

This library is an extended implementation of server for JSON-RPC protocol. It supports only json-rpc 2.0 specification for now, which includes batch submission, keyword arguments, notifications, etc.

Comments, bug reports

flaskJSONRPCServer resides on github. You can file issues or pull requests there.

Requirements

How to install

Documentation

Simple example

Licensing

Pros

Cons

Install

pip install flaskJSONRPCServer

Examples

Simple server. More examples you can find in directory example/

import sys, time, random
from flaskJSONRPCServer import flaskJSONRPCServer

class mySharedMethods:
   def random(self):
      # Sipmly return random value (0..mult)
      return int(random.random()*65536)

class mySharedMethods2:
   def random(self):
      # Sipmly return random value (0..mult)
      return round(random.random()*1, 1)

def echo(data='Hello world!'):
   # Simply echo
   return data
echo._alias='helloworld' #setting alias for method

def myip(_connection=None):
   # Return client's IP
   return 'Hello, %s!'%(_connection.ip)

def setcookie(_connection=None):
   # Set cookie to client
   print _connection.cookies
   _connection.cookiesOut.append({'name':'myTestCookie', 'value':'Your IP is %s'%_connection.ip, 'domain':'byaka.name'})
   return 'Setted'

def stats(_connection=None):
   #return server's speed stats
   return _connection.server.stats(inMS=True) #inMS=True return stats in milliseconds

def big(_connection=None):
   _connection.allowCompress=True #allow compression for this method only
   s="""
... large data here ...
   """
   return s

big._alias=['bigdata', 'compressed'] #setting alias for method

if __name__=='__main__':
   print 'Running api..'
   # Creating instance of server
   #    <blocking>         switch server to sync mode when <gevent> is False
   #    <cors>             switch auto CORS support
   #    <gevent>           switch to using Gevent as backend
   #    <debug>            switch to logging connection's info from Flask
   #    <log>              switch to logging debug info from flaskJSONRPCServer
   #    <fallback>         switch auto fallback to JSONP on GET requests
   #    <allowCompress>    switch auto compression
   #    <compressMinSize>  set min limit for compression
   #    <tweakDescriptors> set descriptor's limit for server
   #    <jsonBackend>      set JSON backend. Auto fallback to native when problems
   #    <notifBackend>     set backend for Notify-requests
   server=flaskJSONRPCServer(("0.0.0.0", 7001), blocking=False, cors=True, gevent=True, debug=False, log=False, fallback=True, allowCompress=False, jsonBackend='simplejson', notifBackend='simple', tweakDescriptors=[1000, 1000])
   # Register dispatcher for all methods of instance
   server.registerInstance(mySharedMethods(), path='/api')
   # same name, but another path
   server.registerInstance(mySharedMethods2(), path='/api2')
   # Register dispatchers for single functions
   server.registerFunction(setcookie, path='/api')
   server.registerFunction(echo, path='/api')
   server.registerFunction(myip, path='/api')
   server.registerFunction(big, path='/api')
   server.registerFunction(stats, path='/api')
   # Run server
   server.serveForever()
   # Now you can access this api by path http://127.0.0.1:7001/api for JSON-RPC requests
   # Or by path http://127.0.0.1:7001/api/<method>?jsonp=<callback>&(params) for JSONP requests
   #    For example by http://127.0.0.1:7001/api/echo?data=test_data&jsonp=jsonpCallback_129620

License

It is licensed under the Apache License, Version 2.0 (read).