forked from xcme/swtoolz-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswtoolz-core.py
executable file
·66 lines (54 loc) · 2.03 KB
/
swtoolz-core.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
#!/usr/bin/env python3
import asyncio
import logging
import sys
from os import sep
from aiohttp import web
from daemon import Daemon
from handlers import handle_get
from swconfig import logfile, port, debug_mode
if debug_mode:
logging_level = logging.DEBUG
else:
logging_level = logging.INFO
logging.basicConfig(filename=logfile, level=logging_level, format='%(levelname)s %(asctime)s %(message)s')
# Добавляем директорию 'devices' в список path. Это нужно, чтобы демон мог находить модули в этой директории
sys.path.append('%s%sdevices' % (sys.path[0], sep))
async def main():
app = web.Application()
app.add_routes([web.get('/{user}/{target_ip}/{comm_index}/{commands:.*}', handle_get)])
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, port=port)
await site.start()
app.logger.info(f"Serving app on {site._host}:{site._port} ...") # Да, я знаю, что лучше так не делать
return runner, site
class MyDaemon(Daemon):
def run(self):
main()
if __name__ == "__main__":
daemon = MyDaemon('/var/run/swtoolz-core.pid', '/dev/null', logfile, logfile)
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'faststart' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
elif 'nodaemon' == sys.argv[1]:
loop = asyncio.get_event_loop()
runner, site = loop.run_until_complete(main())
try:
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete(runner.cleanup())
loop.close()
else:
print("swtoolz-core: " + sys.argv[1] + " - unknown command")
sys.exit(2)
sys.exit(0)
else:
print("usage: %s start|stop|restart|nodaemon" % sys.argv[0])
sys.exit(2)