File: ppserver.py

package info (click to toggle)
parallelpython 1.6.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 340 kB
  • ctags: 291
  • sloc: python: 1,485; makefile: 13
file content (402 lines) | stat: -rwxr-xr-x 14,067 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python
# Parallel Python Software: http://www.parallelpython.com
# Copyright (c) 2005-2012, Vitalii Vanovschi
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#    * Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#    * Neither the name of the author nor the names of its contributors
#      may be used to endorse or promote products derived from this software
#      without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
"""
Parallel Python Software, Network Server

http://www.parallelpython.com - updates, documentation, examples and support
forums
"""

import atexit
import logging
import errno
import getopt
import sys
import socket
import threading
import random
import string
import signal
import time
import os

import pp
import ppauto
import ppcommon
import pptransport

copyright = "Copyright (c) 2005-2012 Vitalii Vanovschi. All rights reserved"
version = "1.6.4"

LISTEN_SOCKET_TIMEOUT = 20

# compatibility with Python 2.6
try:
    import hashlib
    sha_new = hashlib.sha1
except ImportError:
    import sha
    sha_new = sha.new


class _NetworkServer(pp.Server):
    """Network Server Class
    """

    def __init__(self, ncpus="autodetect", interface="0.0.0.0",
                broadcast="255.255.255.255", port=None, secret=None,
                timeout=None, restart=False, proto=2, socket_timeout=3600, pid_file=None):
        pp.Server.__init__(self, ncpus, (), secret, restart,
                proto, socket_timeout)
        if pid_file:
          with open(pid_file, 'w') as pfile:
            print >>pfile, os.getpid()
          atexit.register(os.remove, pid_file)
        self.host = interface
        self.bcast = broadcast
        if port is not None:
            self.port = port
        else:
            self.port = self.default_port
        self.timeout = timeout
        self.ncon = 0
        self.last_con_time = time.time()
        self.ncon_lock = threading.Lock()

        self.logger.debug("Strarting network server interface=%s port=%i"
                % (self.host, self.port))
        if self.timeout is not None:
            self.logger.debug("ppserver will exit in %i seconds if no "\
                    "connections with clients exist" % (self.timeout))
            ppcommon.start_thread("timeout_check",  self.check_timeout)

    def ncon_add(self, val):
        """Keeps track of the number of connections and time of the last one"""
        self.ncon_lock.acquire()
        self.ncon += val
        self.last_con_time = time.time()
        self.ncon_lock.release()

    def check_timeout(self):
        """Checks if timeout happened and shutdowns server if it did"""
        while True:
            if self.ncon == 0:
                idle_time = time.time() - self.last_con_time
                if idle_time < self.timeout:
                    time.sleep(self.timeout - idle_time)
                else:
                    self.logger.debug("exiting ppserver due to timeout (no client"\
                            " connections in last %i sec)", self.timeout)
                    os._exit(0)
            else:
                time.sleep(self.timeout)

    def listen(self):
        """Initiates listenting to incoming connections"""
        try:
            self.ssocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            # following allows ppserver to restart faster on the same port
            self.ssocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.ssocket.settimeout(LISTEN_SOCKET_TIMEOUT)
            self.ssocket.bind((self.host, self.port))
            self.ssocket.listen(5)
        except socket.error, e:
            self.logger.error("Cannot create socket for %s:%s, %s", self.host, self.port, e)

        try:
            while 1:
                csocket = None
                # accept connections from outside
                try:
                    (csocket, address) = self.ssocket.accept()
                except socket.timeout:
                    pass
                # don't exit on an interupt due to a signal
                except socket.error as e: 
                    if e.errno == errno.EINTR:
                      pass
                if self._exiting:
                    return                
                # now do something with the clientsocket
                # in this case, we'll pretend this is a threaded server
                if csocket:
                    ppcommon.start_thread("client_socket",  self.crun, (csocket,  ))
        except KeyboardInterrupt:
            pass
        except:
            self.logger.debug("Exception in listen method (possibly expected)", exc_info=True)
        finally:
            self.logger.debug("Closing server socket")
            self.ssocket.close()            

    def crun(self, csocket):
        """Authenticates client and handles its jobs"""
        mysocket = pptransport.CSocketTransport(csocket, self.socket_timeout)
        #send PP version
        mysocket.send(version)
        #generate a random string
        srandom = "".join([random.choice(string.ascii_letters)
                for i in xrange(16)])
        mysocket.send(srandom)
        answer = sha_new(srandom+self.secret).hexdigest()
        clientanswer = mysocket.receive()
        if answer != clientanswer:
            self.logger.warning("Authentication failed, client host=%s, port=%i"
                    % csocket.getpeername())
            mysocket.send("FAILED")
            csocket.close()
            return
        else:
            mysocket.send("OK")

        ctype = mysocket.receive()
        self.logger.debug("Control message received: " + ctype)
        self.ncon_add(1)
        try:
            if ctype == "STAT":
                #reset time at each new connection
                self.get_stats()["local"].time = 0.0
                mysocket.send(str(self.get_ncpus()))
                while 1:
                    mysocket.receive()
                    mysocket.send(str(self.get_stats()["local"].time))
            elif ctype=="EXEC":
                while 1:
                    sfunc = mysocket.creceive()
                    sargs = mysocket.receive()
                    fun = self.insert(sfunc, sargs)
                    sresult = fun(True)
                    mysocket.send(sresult)
        except:
            if self._exiting:
                return
            if pp.SHOW_EXPECTED_EXCEPTIONS:
                self.logger.debug("Exception in crun method (possibly expected)", exc_info=True)
            self.logger.debug("Closing client socket")
            csocket.close()
            self.ncon_add(-1)

    def broadcast(self):
        """Initiaates auto-discovery mechanism"""
        discover = ppauto.Discover(self)
        ppcommon.start_thread("server_broadcast",  discover.run,
                ((self.host, self.port), (self.bcast, self.port)))


def parse_config(file_loc):
    """
    Parses a config file in a very forgiving way.
    """
    # If we don't have configobj installed then let the user know and exit
    try:
        from configobj import ConfigObj
    except ImportError, ie:
        print >> sys.stderr, ("ERROR: You must have config obj installed to use"
                "configuration files. You can still use command line switches.")
        sys.exit(1)

    if not os.access(file_loc, os.F_OK):
        print >> sys.stderr, "ERROR: Can not access %s." % arg
        sys.exit(1)

    # Load the configuration file
    config = ConfigObj(file_loc)
    # try each config item and use the result if it exists. If it doesn't
    # then simply pass and move along
    try:
        args['secret'] = config['general'].get('secret')
    except:
        pass

    try:
        autodiscovery = config['network'].as_bool('autodiscovery')
    except:
        pass

    try:
        args['interface'] = config['network'].get('interface',
                                                  default="0.0.0.0")
    except:
        pass

    try:
        args['broadcast'] = config['network'].get('broadcast')
    except:
        pass

    try:
        args['port'] = config['network'].as_int('port')
    except:
        pass

    try:
        args['loglevel'] = config['general'].as_bool('debug')
    except:
        pass

    try:
        args['ncpus'] = config['general'].as_int('workers')
    except:
        pass

    try:
        args['proto'] = config['general'].as_int('proto')
    except:
        pass

    try:
        args['restart'] = config['general'].as_bool('restart')
    except:
        pass

    try:
        args['timeout'] = config['network'].as_int('timeout')
    except:
        pass

    try:
        args['socket_timeout'] = config['network'].as_int('socket_timeout')
    except:
        pass

    try:
        args['pid_file'] = config['general'].get('pid_file')
    except:
        pass
    # Return a tuple of the args dict and autodiscovery variable
    return args, autodiscovery


def print_usage():
    """Prints help"""
    print "Parallel Python Network Server (pp-" + version + ")"
    print "Usage: ppserver.py [-hdar] [-f format] [-n proto]"\
            " [-c config_path] [-i interface] [-b broadcast]"\
            " [-p port] [-w nworkers] [-s secret] [-t seconds]"\
            " [-k seconds] [-P pid_file]"
    print
    print "Options: "
    print "-h                 : this help message"
    print "-d                 : set log level to debug"
    print "-f format          : log format"
    print "-a                 : enable auto-discovery service"
    print "-r                 : restart worker process after each"\
            " task completion"
    print "-n proto           : protocol number for pickle module"
    print "-c path            : path to config file"
    print "-i interface       : interface to listen"
    print "-b broadcast       : broadcast address for auto-discovery service"
    print "-p port            : port to listen"
    print "-w nworkers        : number of workers to start"
    print "-s secret          : secret for authentication"
    print "-t seconds         : timeout to exit if no connections with "\
            "clients exist"
    print "-k seconds         : socket timeout in seconds"
    print "-P pid_file        : file to write PID to"
    print
    print "To print server stats send SIGUSR1 to its main process (unix only). "
    print 
    print "Due to the security concerns always use a non-trivial secret key."
    print "Secret key set by -s switch will override secret key assigned by"
    print "pp_secret variable in .pythonrc.py"
    print
    print "Please visit http://www.parallelpython.com for extended up-to-date"
    print "documentation, examples and support forums"


def create_network_server(argv):
    try:
        opts, args = getopt.getopt(argv, "hdarn:c:b:i:p:w:s:t:f:k:P:", ["help"])
    except getopt.GetoptError:
        print_usage()
        raise

    args = {}
    autodiscovery = False

    log_level = logging.WARNING
    log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print_usage()
            sys.exit()
        elif opt == "-c":
            args, autodiscovery = parse_config(arg)
        elif opt == "-d":
            log_level = logging.DEBUG
            pp.SHOW_EXPECTED_EXCEPTIONS = True
        elif opt == "-f":
            log_format = arg
        elif opt == "-i":
            args["interface"] = arg
        elif opt == "-s":
            args["secret"] = arg
        elif opt == "-p":
            args["port"] = int(arg)
        elif opt == "-w":
            args["ncpus"] = int(arg)
        elif opt == "-a":
            autodiscovery = True
        elif opt == "-r":
            args["restart"] = True
        elif opt == "-b":
            args["broadcast"] = arg
        elif opt == "-n":
            args["proto"] = int(arg)
        elif opt == "-t":
            args["timeout"] = int(arg)
        elif opt == "-k":
            args["socket_timeout"] = int(arg)
        elif opt == "-P":
            args["pid_file"] = arg

    log_handler = logging.StreamHandler()
    log_handler.setFormatter(logging.Formatter(log_format))
    logging.getLogger("pp").setLevel(log_level)
    logging.getLogger("pp").addHandler(log_handler)

    server = _NetworkServer(**args)
    if autodiscovery:
        server.broadcast()
    return server    
    
def signal_handler(signum, stack):
    """Prints server stats when SIGUSR1 is received (unix only). """
    server.print_stats()

if __name__ == "__main__":
    server = create_network_server(sys.argv[1:])
    if hasattr(signal, "SIGUSR1"):
      signal.signal(signal.SIGUSR1, signal_handler)
    server.listen()
    #have to destroy it here explicitly otherwise an exception
    #comes out in Python 2.4
    del server
    

# Parallel Python Software: http://www.parallelpython.com