forked from jerrywang1981/python-nest
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_server.py
More file actions
54 lines (41 loc) · 1.29 KB
/
Copy pathtest_server.py
File metadata and controls
54 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from nest import NestMsServer, MessagePatternBaseHandler
app = NestMsServer()
class TestHanlder(MessagePatternBaseHandler):
def __init__(self):
pass
def get_message_pattern(self):
return 'TEST_PATTERN'
def handle(self, payload):
print(payload)
return None, ['this is test', 'another test result']
class TestDictHanlder(MessagePatternBaseHandler):
def __init__(self):
pass
def get_message_pattern(self):
'''return the message pattern
it can be string or dict
e.g. 'TEST' or {'cmd': 'test'}
'''
return {'cmd': 'TEST_PATTERN' }
def handle(self, payload):
'''handler function to process payload
It should returns
err - any error or None
result - the processed result
'''
print(payload)
return None, ['this is test dict', 'another test dict result']
@app.message_pattern({'cmd': 'test_decorator'})
def test_decorator(payload):
'''test decorator'''
print(payload)
return None, payload
if __name__ == '__main__':
HOST ='localhost'
PORT = 7086
app.add_handler(TestHanlder)
app.add_handler(TestDictHanlder)
print(f'started to run and listed to port {PORT}')
app.run(HOST, PORT)