PyQt界面中用twisted建立server,pyqttwistedserver,这两天在看《Python
PyQt界面中用twisted建立server,pyqttwistedserver,这两天在看《Python
这两天在看《Python网络编程基础》,里面有一个聊天服务器的例子,想给它增加一个界面,但是发现twisted会堵塞主线程。google了一下,发现可以用qt4reactor,试了试,还真行。
当然,这个demo的界面是很简陋的!
参考文献:http://twistedmatrix.com/trac/wiki/QTReactor
#!/usr/bin/env python# -*- coding:utf-8 -*-from PyQt4 import QtGuiimport sys, qt4reactorqt4reactor.install()app = QtGui.QApplication(sys.argv)from twisted.internet.protocol import Factoryfrom twisted.protocols.basic import LineOnlyReceiverfrom twisted.internet import reactorclass Core(LineOnlyReceiver): #or class Core(Protocol): def getId(self): return str(self.transport.getPeer()) def connectionMade(self): self.factory.window.protocol = self self.factory.addClient(self) self.log('New User Login:%s!' % self.getId()) def connectionLost(self, reason): self.log('%s disconnected!' % str(self.transport.client)) self.factory.delClient(self) def dataReceived(self, data): msg = "%s:%s" % (self.transport.client[0], data) self.factory.sendAll(msg) self.log(msg) def log(self, msg): self.factory.window.sendMessage(msg)class CoreFactory(Factory): protocol = Core def __init__(self, window): self.window = window self.clients = [] def addClient(self, client): self.clients.append(client) def delClient(self, client): self.clients.remove(client) def sendAll(self, data): for x in self.clients: x.transport.write(data)class Frame(QtGui.QFrame): def __init__(self): super(Frame, self).__init__() self.btn_listen = QtGui.QPushButton(u'侦听', self) self.btn_listen.clicked.connect(self.btn_listen_click) self.btn_stop = QtGui.QPushButton(u'停止', self) self.btn_stop.clicked.connect(self.btn_stop_click) self.btn_radio = QtGui.QPushButton(u"广播", self) self.btn_radio.clicked.connect(self.btn_radio_click) self.text_port = QtGui.QLineEdit("1250", self) self.text_msg = QtGui.QLineEdit("hello", self) self.view = QtGui.QTextEdit(self) layout = QtGui.QGridLayout() layout.addWidget(self.text_port, 0, 0) layout.addWidget(self.btn_listen, 0, 1) layout.addWidget(self.btn_stop, 0, 2) layout.addWidget(self.view, 1, 0, 1, 0) layout.addWidget(self.text_msg, 2, 0) layout.addWidget(self.btn_radio, 2, 1, 1, 2) self.setLayout(layout) self.core_factory = CoreFactory(self) self.protocol = None def sendMessage(self, msg): self.view.append(msg) def btn_listen_click(self): port = int(self.text_port.text()) reactor.listenTCP(port, self.core_factory) print "start running..." reactor.run() def btn_stop_click(self): reactor.stop() def btn_radio_click(self): msg = str(self.text_msg.text()) self.core_factory.sendAll(msg) def closeEvent(self, event): print('Attempting to close the main window!') self.btn_stop_click() event.accept()if __name__ == '__main__': frm = Frame() frm.show() sys.exit(app.exec_())#该片段来自于http://byrx.net
相关内容
- 用pyui4win给python添加动画界面,,这是用开源项目pyui4
- 使用python下载网页上的flash,pythonflash,'''Created o
- Python 兔子毒药问题,python兔子毒药,大致是这样的:1
- python对字典进行排序,python字典排序,1、 准备知识:在
- python解析远程web页面,python解析web,import htmll
- 打印杨辉三角形,杨辉三角形,Python语言: py
- python计算两个日期相差的天数,python天数,#两个日期相
- 在python中使用tempconv模块转换问题,pythontempconv,from te
- # MYSQL 添加 删除 修改 查询 自己写的一个函数,,#!/us
- Python 操作excel,python操作excel,首先安装python2.
评论关闭