-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.py
36 lines (34 loc) · 1.2 KB
/
start.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 common
import sys
import socket
def client(conn, addr, conf):
print "client connected from " + str(addr)
common.log(conf, "client connected from " + str(addr))
while (1):
data = conn.recv(4096) # more than the maximum length of an URL/http request
if not data: break
print str(addr) + " : request " + data
conn.send("Salut LOL")
conn.close()
print "client on " + str(addr) + "disconnected"
common.log(conf, "client on " + str(addr) + "disconnected")
def start(conf):
if (common.exists(conf['pidfile']) == False
or os.path.getsize(conf['pidfile']) == 0):
pid = os.fork()
if (pid == 0):
os.setsid()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', int(conf['port'])))
s.listen(int(conf['maxconnection']))
while (1):
conn, addr = s.accept()
if (os.fork() == 0):
client(conn, addr, conf)
else:
open(conf['pidfile'], "w").write(str(pid))
print "Daemon started"
common.log(conf, "Daemon started")
else:
print >> sys.stderr, "Daemon is already running"