forked from petrjasek/dsus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
executable file
·101 lines (78 loc) · 2.72 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
#!/usr/bin/python
""" Debian Smart Upload Server runtime
@copyright: 2010 Petr Jasek <[email protected]>
@license: GNU General Public License version 2 or later
"""
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
################################################################################
import sys
import getopt
import signal
from BaseHTTPServer import HTTPServer
from daklib.config import Config
from handler import DSUSHandler
class DSUServer(HTTPServer):
"""
Debian Smart Upload Server class
"""
STATE_INIT = 0
STATE_ACTIVE = 1
STATE_SHUTDOWN = 2
STATE_RECONFIG = 3
def __init__(self):
self.cnf = Config()
self.address = ('', int(self.cnf["DSUS::port"]))
HTTPServer.__init__(self, self.address, DSUSHandler)
self.state = self.STATE_INIT
def run(self):
"""
Server routine.
"""
signal.signal(signal.SIGUSR1, self.handle_signal)
signal.signal(signal.SIGHUP, self.handle_signal)
while self.state != self.STATE_SHUTDOWN:
self.state = self.STATE_ACTIVE
while self.state == self.STATE_ACTIVE:
self.handle_request()
def handle_signal(self, signum, frame):
"""
Change state with signals.
"""
if signum == signal.SIGUSR1:
self.state = self.STATE_SHUTDOWN
print "Server shutting down"
elif signum == signal.SIGHUP:
self.state = self.STATE_RECONFIG
self.cnf.initialised = False
self.cnf = Config()
print "Server reconfigured"
def usage():
""" Print usage message. """
print "usage: dsus.py [-h|--help]"
def main(argv):
""" Handles arguments and runs server. """
try:
opts, args = getopt.getopt(argv, "h", ["help"])
except getopt.GetoptError:
usage()
sys.exit(2)
# parse options
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
# start server
server = DSUServer()
server.run()
if __name__ == "__main__":
main(sys.argv[1:])