Skip to content

A Python (2) "JSON-RPC over http" server with hot-reloading and many more

License

Notifications You must be signed in to change notification settings

byaka/flaskJSONRPCServer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • Python2.6 or Python2.7
  • Flask >= 0.10 (not tested with older version)
  • Gevent >= 1.0 (optionally, but recommended)

####How to install ####Documentation ####Simple example ####Licensing

Pros

  • Lib ready for production, we use it in some products
  • Lib tested over "highload" (over 100 connections per second, 24/7 and it's not simulation) with Gevent enabled and no stability issues or memory leak (this is why i'm wrote this library)
  • Auto CORS
  • Simple switching to Gevent
  • Auto fallback to JSONP on GET requests (for old browsers, that don't support CORS like IE<10)
  • Dispatchers can simply get info about connection (IP, Cookies, Headers and many more)
  • Dispatchers can simply set Cookies, change output Headers, change output format for JSONP requests
  • Lib fully support Notification requests (see example/notify.py)
  • Lib supports restarting server (see example/restart.py)
  • Lib supports hot-reloading of API (see example/hotReload1.py, example/hotReload2.py)
  • Lib supports multiple servers in one app (see example/multiple.py)
  • Lib supports merging with another WSGI app on the same IP:PORT (see example/mergeFlaskApp.py)
  • Lib supports different execution-backends, for example multiprocessing (see example/parallelExecuting.py)
  • Lib supports locking (you can switch all server or specific dispatchers to one-request-per-time mode)
  • Lib supports different serializing-backends so you can implement any protocol, not only JSON
  • Lib supports individual settings for different dispatchers. For example one of them can be processed with parallel (multiprocess) backend, other with standard processing
  • Lib collects self speed-stats

Cons

  • Not fully documentated. Current documentation here.
  • Lib not has decorators, so it not a "Flask-way" (this can be simply added, but i not use decorators, sorry)
  • Lib not covered with tests.

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 one-request-per-time mode
   #    <cors>             switch auto CORS support
   #    <gevent>           switch to using Gevent
   #    <debug>            switch to logging connection's info from Flask
   #    <log>              set log level
   #    <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=3, fallback=True, allowCompress=False, jsonBackend='simplejson', notifBackend='threaded', 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).