-
Notifications
You must be signed in to change notification settings - Fork 3
Example Client Server
mcyph edited this page Feb 4, 2020
·
1 revision
echoserver.py:
from speedysvc import ServerMethodsBase, raw_method, json_method
class EchoServer(ServerMethodsBase):
port = 5555
name = 'echo_serv'
# Note that raw_method can only send data as the "bytes" type.
# json, msgpack, pickle, marshal etc are options in place of raw,
# with significantly differing performance, serialisable types
# and security: please see below.
@raw_method
def echo_raw(self, data):
return data
@json_method
def echo_json(self, data):
return data
echoclient.py:
from speedysvc import ClientMethodsBase, connect
from echoserver import EchoServer
class EchoClient(ClientMethodsBase):
def __init__(self, client_provider):
ClientMethodsBase.__init__(self, client_provider)
# echo_raw = TestServerMethods.echo_raw.as_rpc()
# can also do the same as the below code.
def echo_raw(self, data):
return self.send(EchoServer.echo_raw, data)
# Note that "data" is actually a list of arguments
# for non-raw serialisers.
def echo_json(self, data):
return self.send(EchoServer.echo_json, [data])
if __name__ == '__main__':
# client can be replaced with NetworkClient(host_address)
# to allow for remote connections. The tcp_bind ini setting
# must be set in this case: see below.
client = connect(EchoServer, 'shm://')
#client = connect(EchoServer, 'tcp://127.0.0.1')
methods = EchoClient(client)
print("Received data:", methods.echo_raw(b"Lorem ipsum"))
service.ini:
[defaults]
#tcp_bind=127.0.0.1
log_dir=/tmp/test_server_logs/
[EchoServer]
import_from=echoserver
Then type python3 -m speedysvc.service service.ini &
from the same directory to start the server; and
python3 echoclient.py
to test a connection to it.