-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
113 lines (88 loc) · 2.69 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python3
import sys
import os
import os.path
import importlib
from http.server import HTTPServer, BaseHTTPRequestHandler
import globalvar as gVar
gVar._init()
from config import config
import logger
logger._init(config)
logger.debug("logger inited")
# Modules
modules = {}
gVar.set_value("modules", modules)
modDir = []
sys.path.append("./module")
def loadModules():
global modDir
modDir = os.listdir("./module")
for moduleName in modDir:
if moduleName not in config["module_disable"]: # 不初始化disable列表里的
loadModule(moduleName)
def loadModule(moduleName):
global modDir
global modules
m = importlib.import_module(moduleName)
# 已存在
if existModule(moduleName):
return False
# 处理依赖
if "depend" in dir(m) and len(m.depend) > 0:
for depName in m.depend:
# 文件查找
if depName not in modDir:
logger.error(
"module \"{depName}\" not found".format_map(vars()))
return False
# 排除配置disable
if depName in config["module_disable"]:
logger.warn(
"module \"{m.__name__}\" depend on \"{depName}\", but it's disabled".format_map(vars()))
return False
# log out
logger.info(
"module \"{m.__name__}\" depend on \"{depName}\"".format_map(vars()))
# 跳过已加载的模块
if existModule(depName):
logger.debug("module \"{depName}\" exist. skip".format_map(vars()))
continue
# 递归加载依赖
success = loadModule(depName)
if not success:
# TODO err out
logger.warn("module \""+depName+"\" exist")
return False
# 正常加载
modules[moduleName] = m
logger.info("load module \"{m.__name__}\"".format_map(vars()))
return True
def existModule(moduleName):
global modules
if moduleName in modules:
return True
else:
return False
def initModules():
global modules
for mName in modules:
modules[mName].init()
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
path = self.path.split(os.sep)
print(path)
BaseHTTPRequestHandler.send_response(self, 200, "hello")
def do_POST(self):
pass
logger.debug("loading modules")
loadModules()
logger.debug("modules load complete")
logger.debug("initializing modules")
initModules()
logger.debug("modules initialize complete")
# debug
modules["iptables"].start()
# http
httpd = HTTPServer(("", 5000), RequestHandler)
httpd.serve_forever()