1 | """WSGI unit test package |
---|
2 | |
---|
3 | NERC DataGrid Project |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "23/02/09" |
---|
7 | __copyright__ = "(C) 2010 Science and Technology Facilities Council" |
---|
8 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
9 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
10 | __revision__ = '$Id$' |
---|
11 | |
---|
12 | import paste.httpserver |
---|
13 | from threading import Thread |
---|
14 | from paste.deploy import loadapp |
---|
15 | from paste.script.util.logging_config import fileConfig |
---|
16 | |
---|
17 | import logging |
---|
18 | logging.basicConfig(level=logging.DEBUG) |
---|
19 | |
---|
20 | class PasteDeployAppServer(object): |
---|
21 | """Wrapper to paste.httpserver to enable background threading""" |
---|
22 | |
---|
23 | def __init__(self, app=None, cfgFilePath=None, port=7443, host='0.0.0.0', |
---|
24 | ssl_context=None): |
---|
25 | """Load an application configuration from cfgFilePath ini file and |
---|
26 | instantiate Paste server object |
---|
27 | """ |
---|
28 | self.__thread = None |
---|
29 | |
---|
30 | if cfgFilePath: |
---|
31 | fileConfig(cfgFilePath) |
---|
32 | app = loadapp('config:%s' % cfgFilePath) |
---|
33 | |
---|
34 | elif app is None: |
---|
35 | raise KeyError('Either the "cfgFilePath" or "app" keyword must be ' |
---|
36 | 'set') |
---|
37 | |
---|
38 | self.__pasteServer = paste.httpserver.serve(app, host=host, port=port, |
---|
39 | start_loop=False, |
---|
40 | ssl_context=ssl_context) |
---|
41 | |
---|
42 | @property |
---|
43 | def pasteServer(self): |
---|
44 | return self.__pasteServer |
---|
45 | |
---|
46 | @property |
---|
47 | def thread(self): |
---|
48 | return self.__thread |
---|
49 | |
---|
50 | def start(self): |
---|
51 | """Start server""" |
---|
52 | self.pasteServer.serve_forever() |
---|
53 | |
---|
54 | def startThread(self): |
---|
55 | """Start server in a separate thread""" |
---|
56 | self.__thread = Thread(target=PasteDeployAppServer.start, args=(self,)) |
---|
57 | self.thread.start() |
---|
58 | |
---|
59 | def terminateThread(self): |
---|
60 | self.pasteServer.server_close() |
---|
61 | |
---|