-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_server.py
52 lines (43 loc) · 1.15 KB
/
push_server.py
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
# coding=utf-8
import zm_chan_bot
import asyncio
# import requests
import sys
import json
import config
def str_to_dict(string):
try:
dict_ = json.loads(string)
except json.decoder.JSONDecodeError as e:
print(e)
return
else:
return dict_
class Handler:
def __init__(self):
self.bot = zm_chan_bot.Bot(config)
async def handle(self, reader_c, writer_c):
try:
req_bytes = b''
while True:
buf = await reader_c.read(1024*16)
if not buf:
break
else:
req_bytes += buf
except ConnectionResetError as e:
print(e)
req_json = req_bytes.decode()
req = str_to_dict(req_json)
if req:
id, msg = req['id'], req['message']
print(msg)
print(self.bot.send(id, msg))
else:
return
if __name__ == '__main__':
loop = asyncio.get_event_loop()
handler = Handler()
core = asyncio.start_server(handler.handle, '0.0.0.0', 8800, loop=loop)
server = loop.run_until_complete(core)
loop.run_forever()