bijomaru
bijomaru

Reputation: 21

Python freezes when PyQT4 mixes with BaseHTTPServer

I have an error in my Python code, I'm new to Python, and I'm also new to PyQT. What I'm trying to do is build a basic http proxy server with a GUI. I've done it in the console, but when I tried to implement a GUI, I'm getting an error.

Here is my code, any help is appreciated.

import BaseHTTPServer, SocketServer,sys
from PyQt4 import QtCore, QtGui
from Ui_MiniGui import Ui_MainWindow

class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
    pass

class webServer(QtCore.QThread):
    log = QtCore.pyqtSignal(object)

    def __init__(self, parent = None):
        QtCore.QThread.__init__(self, parent)

    def run(self):
        self.log.emit("Listening On Port 1805")
        Handler = BaseHTTPServer.BaseHTTPRequestHandler

        def do_METHOD(self):
            method = self.command
#I got some trouble here, how can i emit the signal back to the log?
            #self.log.emit(method) not work, python not crash
            #webServer.log.emit(method) not work, python not crash
        #below one not work and python crashes immediately
            webServer().log.emit(method)

        Handler.do_GET = do_METHOD

        self.httpd = ThreadingHTTPServer(("", 1805), Handler)
        self.httpd.serve_forever()

class Form(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.server = webServer()
        self.server.log.connect(self.write_to_textEdit)
        self.server.start()

    def write_to_textEdit(self, data):
        print data
        self.ui.textEdit.setText(data)

if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    myapp = Form()
    myapp.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 402

Answers (1)

mata
mata

Reputation: 69052

In this line

webServer().log.emit(method)

You create a new webserver instance and emit it's log signal. As this is a new object, this signal hasn't been connected to anything, so it's doing nothing.

To emit the signal on the right object, you could do the following:

def run(self):
    ...
    server = self
    def do_METHOD(self):
        ...
        server.log.emit(self)

Upvotes: 1

Related Questions