pyssb/examples/test_server.py

46 lines
1.1 KiB
Python
Raw Normal View History

2017-07-31 21:17:59 +00:00
import logging
import os
2017-07-31 21:17:59 +00:00
from asyncio import get_event_loop, ensure_future
2017-05-25 10:47:01 +00:00
from base64 import b64decode
import yaml
2017-07-31 21:17:59 +00:00
from colorlog import ColoredFormatter
from nacl.signing import SigningKey
2017-05-25 10:47:01 +00:00
from ssb.packet_stream import PSServer
with open(os.path.expanduser('~/.ssb/secret')) as f:
config = yaml.load(f)
2017-05-25 10:47:01 +00:00
2017-07-31 21:17:59 +00:00
async def on_connect():
2017-08-01 17:32:36 +00:00
ensure_future(packet_stream, loop=loop)
2017-07-31 21:17:59 +00:00
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = ColoredFormatter('%(log_color)s%(levelname)s%(reset)s:%(bold_white)s%(name)s%(reset)s - '
'%(cyan)s%(message)s%(reset)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger = logging.getLogger('packet_stream')
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
2017-05-25 10:47:01 +00:00
loop = get_event_loop()
2017-05-25 10:47:01 +00:00
server_keypair = SigningKey(b64decode(config['private'][:-8])[:32])
packet_stream = PSServer('127.0.0.1', 8008, server_keypair, loop=loop)
2017-05-25 10:47:01 +00:00
packet_stream.on_connect(on_connect)
packet_stream.listen()
2017-05-25 10:47:01 +00:00
loop.run_forever()
loop.close()