-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
37 lines (30 loc) · 1.19 KB
/
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
import os
import daemon
import logging
import socketserver
import struct
from docs import conf
from importlib import import_module as loader
LEVEL = logging.DEBUG if 'DEBUG' in os.environ else logging.INFO
FORMAT = '[%(levelname)s]: %(asctime)-15s %(message)s'
DATEFMT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(level=LEVEL, format=FORMAT, datefmt=DATEFMT)
class RequestHandler(socketserver.BaseRequestHandler):
def setup(self):
self.logger = logging.getLogger()
def handle(self):
while True:
header = self.request.recv(conf.HEADER_LENGTH)
(body_length, protocol) = struct.unpack('>2I', header)
self.logger.info('body_length:%d protocol:%d', body_length, protocol)
data = self.request.recv(body_length)
module = loader('baccarat.handler')
module.handle(self.request, protocol, data, self.logger)
def finish(self):
pass
class BaccaratServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
def handle_error(self, request, client_address):
print('handle_error')
raise ValueError('unknow error')
Server = BaccaratServer((conf.HOST, conf.PORT), RequestHandler)
Server.serve_forever()