- Python 3+
- selectors
- msgpack
- cryptography
import sys
sys.path.append("/path/to/fpnn")
from fpnn import *
client = TCPClient(host, port, auto_reconnect = True)
- auto_reconnect means establishing the connection in implicit or explicit. NOT keep the connection.
client.connect()
client.close()
client.reconnect()
client.destory()
client.set_quest_timeout(milliseconds)
- timeout in milliseconds, this api can set the global quest timeout, you can also set a timeout for each request individually
client.set_connection_callback(callback)
- the callback is a sub-class of ConnectionCallback, you can use it like this:
class MyConnectionCallback(ConnectionCallback):
def connected(self, connection_id, endpoint, connected):
print(connected)
print(connection_id)
print(endpoint)
def closed(self, connection_id, endpoint, caused_by_error):
print("closed")
print(connection_id)
print(endpoint)
print(caused_by_error)
client.set_connection_callback(MyConnectionCallback())
client.enable_encryptor_by_pem_file(pem_pub_file, curve_name, strength)
- RTM Server-End Python SDK using ECC/ECDH to exchange the secret key, and using AES-128 or AES-256 in CFB mode to encrypt the whole session in stream way.
client.set_quest_processor(processor)
- set the server quest processor to get the Duplex Push from the FPNN-Server, you can see the detailed usage below
# create Quest
quest = Quest("method_name")
quest.param("p1", 12345)
quest.param("p2", "abc")
quest.param("p3", 0.25)
quest.param("p4", True)
# send quest in async:
# define async quest callback:
class MyQuestCallback(QuestCallback):
def callback(self, answer):
if answer.is_error():
print(answer.error_code)
print(answer.error_message)
else:
try:
k1 = answer.want("k1)
except:
pass
k2 = answer.get("k2", None)
# send async quest:
client.send_quest(quest, MyQuestCallback())
# send quest in sync:
answer = client.send_quest(quest)
if answer.is_error():
print(answer.error_code)
print(answer.error_message)
else:
try:
k1 = answer.want("k1)
except:
pass
k2 = answer.get("k2", None)
def send_quest(self, quest, callback = None, timeout = 0)
- quest: (Required | instance of Quest ) the quest
- callback: (Optional | a sub-class of QuestCallback ) used in async implementation
- timeout: (Optional | int ) timeout in milliseconds, if not set, a global timeout will be used
def __init__(self, method, oneway = False, params = None)
# you can init Quest and set param like this:
quest = Quest('method_name')
quest.param('key', 'value')
# or init param when init:
quest = Quest('method_name', params = {'key':'value'})
def __init__(self, params = None)
# you can init Answer and set param like this:
answer = Answer()
answer.param('key', 'value')
# or init param when init:
answer = Answer(params = {'key':'value'})
# you can get the param from an answer return by a send_quest:
try:
k1 = answer.want("k1)
except:
pass
k2 = answer.get("k2", None)
def want(self, key)
- key: (Required | Str ) answer key
- the value
- Exception('get param error')
def get(self, key, default)
- key: (Required | Str ) answer key
- default: (Required ) default value if not exist the key
- the value
if answer.is_error():
print(answer.error_code)
print(answer.error_message)
class MyQuestProcessor(QuestProcessor):
def method_name(self, connection, quest):
try:
k1 = quest.want("k1")
except:
pass
k2 = quest.get("k2", None)
return Answer()
client.set_quest_processor(MyQuestProcessor())
class MyQuestProcessor(QuestProcessor):
def method_name(self, connection, quest):
connection.send_answer(Answer())
# ...
return None