Package: remotecall
RemotecallThe module provides functionality to expose Python functions to be called remotely over ethernet.The implementation uses Python'shttp.server.HTTPServerto implement a server
side functionality andRequestfor client implementation.Getting startedExposing a function to be called remotely.ServerCreate a server and expose a function.fromremotecallimportServerdefhello()->str:return"Hello World"withServer(("localhost",8000))asserver:server.expose(hello)server.serve_forever()ClientSubclass a client from BaseClient to call exposed functions.fromremotecallimportServerclassClient(BaseClient):defhello(self)->str:returnself.call("hello")client=Client(("localhost",8000))client.hello()Code generationUse the module to generate client code instead of writing the client code.python-mremotecallgenerate_clienthttp://localhost:8000UsageBasic exampleServer side implementation to expose hello() function:fromremotecallimportServerdefhello()->str:return"Hello World"withServer(server_address=("localhost",8000))asserver:server.register(hello)server.serve_forever()Client side implementation is based on BaseClient class which is a thin wrapper around Requests
module.fromremotecallimportBaseClientclient=BaseClient(server_address=("localhost",8000))client.call("hello")Subclassing BaseClient makes accessing the available functions more convenient - for example, by
enabling IDE autocompletion and providing the type information.fromremotecallimportBaseClientclassClient(BaseClient):defhello(self)->str:returnself.call("hello")client=Client(server_address=("localhost",8000))client.hello()Usage ofpython -m remotecall generate_clientautomates the client code generation.For more example, see examples directory.Passing argumentsModule uses Python type annotations to determine type of parameter and return
values.The type information is used to generate API definition. The API definition is provided in JSON
format and can be used to generate client code.The type information is also used to encode and decode data - like parameter and return values -
send between client and server.The implementation support basic Python data types includingbytes,int,float,bool,str,list,tupleanddict. It is possible to add support for addition types. See examples
directory for an implementation forPIL ImagesandNumPyarrays.ServerExposing a function with parameters and return value.defecho(message:str)->str:returnmessageClientCalling the function from the client side.classClient(BaseClient):defecho(self,message:str)->str:returnself.call("echo",message=message)HTTPS and SSLBy default, the server uses HTTP for the communication. HTTPS communication can be enabled by
providing SSL certificate. The certificate is used to create a SSLContext to wrap
the underlying socket instance.Seeexamplesfolder for an example and instructions to generate a self-signed certificate.Serverserver=Server(...)server.use_ssl(cert_file="server.crt",key_file="server.key")Clientclient=BaseClient(...)client.use_ssl(cert_file="server.pem")AuthenticationBy default, the server does not require authentication.Server provides an implementation for HTTP basic authenticator that can be set in use as
demonstrated below. However, it's also possible to subclassremotecall.authentication.Authenticatorand implement other type of authenticators.Client implementation wrapsRequestslibrary and makes it possible to use the
authentication methods available for that library. More documentation can be found from the
library documentationhttps://requests.readthedocs.io/en/latest/user/authentication/.Serverserver=Server(...)server.set_authenticator(BasicAuthenticator("user","pass"))Clientfromrequests.authimportHTTPBasicAuthclient=BaseClient(...)client.set_authentication(HTTPBasicAuth("user","pass"))Implementing authenticatorImplementing a custom authenticator begins with subclassingremotecall.authentication. Authenticator.Authenticator is expected to implementauthenticatemethod to carry out the authentication. It
is called right after the HTTP server'sdo_*method gets called.authenticatemethod receives an instance ofBaseHTTPRequestHandlerthat represents the HTTP request received by the HTTP server and to handle it.Authenticatoris expected to handle the actions required to carry out the authentication.It may raiseAuthenticationErrorif the server should not continue processing the request.
Server expects the authenticator to resolves the request by sending the corresponding headers and
related data.classAuthenticator(ABC):@abstractmethoddefauthenticate(self,request:BaseHTTPRequestHandler):"""Do the authentication."""Code generationThe module provides an easy way to generate a Python client for a service provided by a server.The API definition can be fetched from the server with the following command line command. The
command returns an API definition in JSON format.$python-mremotecallfetch_apihttp://localhost:8000For example, in case ofechoexample the output would be:{"endpoints":[{"name":"echo","documentation":"","parameters":[{"name":"message","type":"str"}],"return_type":"str"}],"ssl_enabled":false,"address":{"hostname":"localhost","port":8000}}The API definition can be written in a file withoutputoption.$python-mremotecallfetch_apihttp://localhost:8000--outputapi.jsonClient code can be generated based on the definition:$python-mremotecallgenerate_clientapi.json--outputclient.pyWhere,outputdefines the output file.By default, the client class is named asClient. This can be changed by usingnameoption. For example,--name EchoClient.The above steps can be combined if the intermediate results are not needed.$python-mremotecallgenerate_clienthttp://localhost:8000--outputclient.pyExtending supported data typeSeeexamples/imageandexamples/numpyfor image and numpy codec examples.DevelopmentSet up virtual environmentCreate virtual environment:python3-mvenvvenvActivate virtual environment:sourcevenv/bin/activateRunning from sourcepipinstall-e.Installing from sourcepipinstall.
python-mremotecallRunning testspipinstall.[test]pytest

py.test-sBuildingCreating a wheel$pipinstallwheel
$pythonsetup.pybdist_wheel