diff options
author | mkaay <mkaay@mkaay.de> | 2010-01-27 20:05:23 +0100 |
---|---|---|
committer | mkaay <mkaay@mkaay.de> | 2010-01-27 20:05:23 +0100 |
commit | 6d1ec2baa795b0d90d5da33b0447120b50f10c17 (patch) | |
tree | 5fceb5474c83e709fda396f70121a6923b0be106 /module/web/ServerThread.py | |
parent | First Version, works but doesn't reconnect (diff) | |
parent | fix (diff) | |
download | pyload-6d1ec2baa795b0d90d5da33b0447120b50f10c17.tar.xz |
merge
Diffstat (limited to 'module/web/ServerThread.py')
-rw-r--r-- | module/web/ServerThread.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/module/web/ServerThread.py b/module/web/ServerThread.py new file mode 100644 index 000000000..1276966ef --- /dev/null +++ b/module/web/ServerThread.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python +from __future__ import with_statement +import threading +from os.path import join +from os.path import exists +from subprocess import Popen, PIPE, STDOUT +from time import sleep +from signal import SIGINT +import os + +class WebServer(threading.Thread): + def __init__(self, pycore): + threading.Thread.__init__(self) + self.pycore = pycore + self.running = True + self.setDaemon(True) + + def run(self): + host = self.pycore.config['webinterface']['host'] + port = self.pycore.config['webinterface']['port'] + + if not exists(join(self.pycore.path,"module","web","pyload.db")): + print "########## IMPORTANT ###########" + print "### Database for Webinterface doesnt exitst, it will not be available." + print "### Please run: python %s syncdb" % join(self.pycore.path,"module","web","manage.py") + print "### You have to add at least one User, to gain access to webinterface: python %s createsuperuser" % join(self.pycore.path,"module","web","manage.py") + print "### Dont forget to restart pyLoad if you are done." + print "################################" + raise Exception, "Database doesnt exists, please use syncdb" + + self.pycore.logger.info("Starting Webserver: %s:%s" % (host,port) ) + + if os.name == 'posix': + command = ['python',join(self.pycore.path,"module","web","run_unix.py"), "runserver", "%s:%s" % (host,port)] + self.p = Popen(command, close_fds=True, stderr=PIPE, stdin=PIPE, stdout=PIPE) + #os.system("python " + join(self.pycore.path,"module","web","manage.py runserver %s:%s" % (host,port))) + #@TODO: better would be real python code + sleep(1) + with open("webserver.pid", "r") as f: + self.pid = int(f.read().strip()) + while self.running: + sleep(1) + else: + command = ['python',join(self.pycore.path,"module","web","manage.py"), "runserver", "%s:%s" % (host,port)] + self.p = Popen(command, stderr=PIPE, stdin=PIPE, stdout=PIPE) + while self.running: + sleep(1) + + def quit(self): + if os.name == 'posix': + try: + os.kill(self.pid, SIGINT) + except: + pass + else: + self.p.kill() + + self.running = False |