1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-21 05:31:49 +00:00

Added content server multithreading

This commit is contained in:
Paweł Jastrzębski
2013-10-29 11:00:48 +01:00
parent 75a338304a
commit 95bb070a6b

View File

@@ -35,6 +35,7 @@ import socket
import string import string
from KCC_rc_web import WebContent from KCC_rc_web import WebContent
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
from image import ProfileData from image import ProfileData
from subprocess import call, Popen, STDOUT, PIPE from subprocess import call, Popen, STDOUT, PIPE
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
@@ -78,10 +79,6 @@ class HTMLStripper(HTMLParser):
class WebServerHandler(BaseHTTPRequestHandler): class WebServerHandler(BaseHTTPRequestHandler):
#noinspection PyShadowingBuiltins
def log_message(self, format, *args):
pass
#noinspection PyAttributeOutsideInit #noinspection PyAttributeOutsideInit
def do_GET(self): def do_GET(self):
if self.path == '/': if self.path == '/':
@@ -114,17 +111,11 @@ class WebServerHandler(BaseHTTPRequestHandler):
'alt="KCC Logo" src="' + GUIMain.webContent.logo + '" /> -</p>\n') 'alt="KCC Logo" src="' + GUIMain.webContent.logo + '" /> -</p>\n')
if len(GUIMain.completedWork) > 0 and not GUIMain.conversionAlive: if len(GUIMain.completedWork) > 0 and not GUIMain.conversionAlive:
for key in sorted(GUIMain.completedWork.iterkeys()): for key in sorted(GUIMain.completedWork.iterkeys()):
self.wfile.write('<p style="font-weight: bold">Only one file is available at once.<br/>' self.wfile.write('<p><a href="' + key + '">' + string.split(key, '.')[0] + '</a></p>\n')
'Refresh page after successful download.</p>\n'
'<p><br/><a href="' + key + '">&gt;&gt;&gt; ' + string.split(key, '.')[0] +
' &lt;&lt;&lt;</a></p>\n')
break
else: else:
self.wfile.write('<p style="font-weight: bold">No downloads are available.<br/>' self.wfile.write('<p style="font-weight: bold">No downloads are available.<br/>'
'Convert some files and refresh this page.</p>\n') 'Convert some files and refresh this page.</p>\n')
self.wfile.write('<p><br/><a style="font-weight: bold" href="javascript:history.go(0)">- Refresh page -' self.wfile.write('</div>\n'
'</a></p>\n'
'</div>\n'
'</body>\n' '</body>\n'
'</html>\n') '</html>\n')
elif sendReply: elif sendReply:
@@ -135,17 +126,20 @@ class WebServerHandler(BaseHTTPRequestHandler):
self.send_header('Content-Length', os.path.getsize(outputFile)) self.send_header('Content-Length', os.path.getsize(outputFile))
self.end_headers() self.end_headers()
while True: while True:
chunk = fp.read(1024) chunk = fp.read(8192)
if not chunk: if not chunk:
fp.close() fp.close()
break break
self.wfile.write(chunk) self.wfile.write(chunk)
GUIMain.completedWork.pop(urllib2.unquote(self.path[1:]), None)
return return
except (IOError, LookupError): except (IOError, LookupError):
self.send_error(404, 'File Not Found: %s' % self.path) self.send_error(404, 'File Not Found: %s' % self.path)
class WebServerThreaded(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
class WebServerThread(QtCore.QThread): class WebServerThread(QtCore.QThread):
def __init__(self): def __init__(self):
QtCore.QThread.__init__(self) QtCore.QThread.__init__(self)
@@ -163,7 +157,7 @@ class WebServerThread(QtCore.QThread):
# Sadly it can fail on some Linux configurations # Sadly it can fail on some Linux configurations
lIP = None lIP = None
try: try:
self.server = HTTPServer(('', 4242), WebServerHandler) self.server = WebServerThreaded(('', 4242), WebServerHandler)
self.running = True self.running = True
if lIP: if lIP:
self.emit(QtCore.SIGNAL("addMessage"), '<b><a href="http://' + lIP + self.emit(QtCore.SIGNAL("addMessage"), '<b><a href="http://' + lIP +