Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f14e552

Browse files
committed
added daemon and server module
1 parent 6242245 commit f14e552

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

‎daemon.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/python
2+
#local imports
3+
from modules.worker import Worker
4+
from modules.service import Service
5+
from modules.utils import *
6+
from modules.server import Server
7+
from modules.commands import quit
8+
#externam imports
9+
from importlib import import_module
10+
from sys import stdout
11+
import logging as log
12+
import config
13+
14+
LOG_FILENAME = 'python_manager.log'
15+
worker = Worker()
16+
server = None
17+
18+
def __init__():
19+
global server
20+
# log.basicConfig(filename=LOG_FILENAME,level=log.DEBUG)
21+
log.basicConfig(level=log.DEBUG)
22+
check_config()
23+
for service_name in config.run.keys():
24+
service_conf = config.run[service_name]
25+
worker.add(Service(service_name, service_conf))
26+
server = Server(config.daemon, worker)
27+
28+
def main():
29+
__init__()
30+
worker.start()
31+
server.start()
32+
log.info('All tasks done. Stopping')
33+
return 0
34+
35+
if __name__ == '__main__':
36+
try:
37+
main()
38+
except KeyboardInterrupt as e:
39+
quit(worker, [], server)

‎modules/server.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from modules.commands import handle_input
2+
import socket
3+
import select
4+
5+
class Server():
6+
def __init__(self, config, worker):
7+
self.config = config
8+
self.is_running = True
9+
self.port = config['port']
10+
self.host = config['host']
11+
self.inputs = []
12+
self.outputs = []
13+
self.BUFFER_SIZE = 1024
14+
self.worker = worker
15+
self.hello = "### Python Manager\nHello !\n#> "
16+
17+
def start(self):
18+
print("Server starting")
19+
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20+
self.socket.bind((self.host, self.port))
21+
self.socket.listen(5)
22+
self.inputs.append(self.socket)
23+
while self.is_running:
24+
_in, _out, exception = select.select(self.inputs,
25+
self.outputs,
26+
[])
27+
for sock in _in:
28+
if sock == self.socket:
29+
client, address = sock.accept()
30+
print(address)
31+
self.inputs.append(client)
32+
client.send(self.hello.encode())
33+
else:
34+
data = sock.recv(self.BUFFER_SIZE)
35+
if data:
36+
command = data.decode("UTF-8")
37+
success, output = handle_input(command, self.worker, self)
38+
sock.send(output.encode()+b"\n#> ")
39+
else:
40+
if sock in self.outputs:
41+
self.outputs.remove(sock)
42+
if sock in self.inputs:
43+
self.inputs.remove(sock)
44+
if sock not in self.outputs:
45+
self.outputs.append(sock)
46+
47+
def stop(self):
48+
print("Exiting...")
49+
self.socket.close()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /