GithubHelp home page GithubHelp logo

simplestrpc's Introduction

simplestRPC

current ๐Ÿ’š license python

A simple RPC for python.

The easiest, cleanest and simplest way to creating interactions via RPC (Remove Procedure Call)

Description

simplestRPC is a light Python 3 librarie, designed especially to allow communications between Microservices bypass the REST API signatures. But it can be used in any soluction who need a simple and light weight comunication between a process and a server like: games, fancy calculators, etc...


Using

Steps
  1. Install dependencies โ”€ see section bellow
  2. crate a file called varenv.conf.json, define a json with the variable SRPC_SERVER and SRPC_SERVER_PORT as especified by the on varenv project here
  3. Create your Server
    1. Import the server class
    2. Instantiate it
    3. Add the remove calls and its names on the client
    4. Serve it
  4. Create your client
    1. Import the client class
    2. Instantiate it

Coding

I recommend you to isolate your environment using virtualenv, but feel free to jump the 'setting' step if you what you're doing.

setting
> virtualenv --python=python3 virenv
> source virenv/bin/activate
> pip install simplestRPC

conf exemple

varenv.conf.json

{
  "SRPC_SERVER": "127.0.0.1",
  "SRPC_SERVER_PORT": "2727",
}
server

file you_server.py

from simplestRPC.srpc_server import SRPCServer


class TestClass:
	localVar = 'var in TestClass'

	def __init__(self):
		pass

	def remote_method(self, a, b, c):
		print('remote_method: ' + self.localVar + ' ' + a)
		return int(b) - int(c)


def remote_function(a, b, c):
	print("remote_function: " + a)
	return int(b) + int(c)


tclass = TestClass()

# start to listen at the port defined by the environment variable
# if you like, you would call it like this directly defining the ip/port
# server = SRPCServer(customIP='127.0.0.1', customPort=6497)
# server = SRPCServer(debug=True) #to generate debug output
server = SRPCServer()

# at the client, the funciton 'remote_function' can be called by 'remote_function'
server.add_rpc(remote_function)
# you may pass a diferent name, at the client, 'remote_method' can be called by 'customName'
server.add_rpc(tclass.remote_method, 'customName')

# start to listen on the create ip/port
server.serve()

output expected

remote_function: xablau!
remote_method: var in TestClass custom method

client

file you_client.py

from simplestRPC.srpc_client import SRPCClient


# conects
client = SRPCClient()

ret = client.call_rpc('remote_function', 'xablau!', 12, 108)
print("return: ", ret)

ret = client.call_rpc('customName', 'custom method', 12, 108)
print("return: ", ret)

output expected

return: 120
return: -96

callbacks

file you_client.py

from simplestRPC.srpc_client import SRPCClient

def do_something_before(self, a, b):
	print("I'm doing some work always before this rpc. " + str(int(a) + int(b)))

def do_something_after(self, a):
	print("Here I'm, every time after the rpc. " + str(a))

# client = SRPCClient(debug=True) #to generate debug output
# client = SRPCClient(customIP='127.0.0.1', customPort=6497)
client = SRPCClient()
client.set_before_rpc_call(do_something_before)

ret = client.call_rpc('remote_function', (10, 8), None, 'xablau!', 12, 108)
print("return: ", ret)
client.set_after_rpc_call(do_something_after)
ret = client.call_rpc('customName', (10, 8), ('!!print this!!',), 'custom method', 12, 108)
print("return: ", ret)

output expected

I'm doing some work always before this rpc. 18
return:  120
I'm doing some work always before this rpc. 18
Here I'm, every time after the rpc. !!print this!!
return:  -96

Author's Note

create by me, davincif, this project of first though the needs of a another professional project made by me. But it sounds so potentially useful the the community that I decided to open this package here freely distributed.

I have no intention to continue enhancing this project professionally, but would love to carry its development with the community if there's anyone interest.

So let me know if you want to help, or also if you need any formal concentiment to use this software, despite the fact that it's already free and open by terms of a very permissive license as zlib.

simplestrpc's People

Contributors

davincif avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

shawwn

simplestrpc's Issues

Cryptography

Implement Cryptography for every msg sent by the middlewere

Better Defined Application Protocol

A better Aplication Protocol standard shall be defined for the middlewere.
A module shall be created to warp and unwarp, and message TCP messages, buffering them when needed.

Authorization

Implement authorization, allowing the user to define whether a client shall be accepted or not.

Race Condition Risk

By the time client and server are pairing the RPCs, there are, mathematically, a slight probability that two message get crushed into one, and the both, client and server, get deadlocked into a socket.receiv.
In the real world it does not seem to heappen. But it should be fixed anyway.

Need to pass 2 arguments for correct behavor of rpc_call invoke

Client

import sys
sys.path.append('../')

# for the user developer test
from simplestRPC.srpc_client import SRPCClient

client = SRPCClient()
ret = client.call_rpc('get_user_by_email', '[email protected]')
print(ret)

Server

import sys
sys.path.append('../')

from simplestRPC.srpc_server import SRPCServer


def get_user_by_email(email):
	print('rodando -> get_user_by_email: ', email)
	return str(2)

server = SRPCServer()
server.add_rpc(get_user_by_email)
server.serve()

it leads to this to a chash at the client, saying you passed no argumento to the rpc.
change this line

ret = client.call_rpc('get_user_by_email', '[email protected]')

for this

ret = client.call_rpc('get_user_by_email', '[email protected]', '[email protected]')

and it'll work.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.