我的 Python 代码有错误,我是 Python 新手,我也是 PyQT 新手。我想做的是用 GUI 构建一个基本的 http 代理服务器。我已经在控制台中完成了它,但是当我尝试实现 GUI 时,我得到了一个错误。
这是我的代码,任何帮助表示赞赏。
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_())