问题描述
- 求助:写了个简单的python通信窗口,为什么刚运行一会就会崩掉呢?
-
代码如下,不知道为什么会崩,在线等大神解答:# -*- coding:utf-8 -*- # file : server.py from PyQt4 import QtGui, QtCore import sys import socket import threading class Window(QtGui.QWidget): def __init__(self): QtGui.QWidget.__init__(self) self.setWindowTitle('Server') self.edit = QtGui.QTextEdit() self.grid = QtGui.QGridLayout() self.button = QtGui.QPushButton('Start Listening') self.connect(self.button, QtCore.SIGNAL('clicked()'), self.Send) self.grid.addWidget(self.button, 0, 0) self.grid.addWidget(self.edit, 1, 0) self.setLayout(self.grid) def Send(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) sock.bind(('', 8033)) sock.listen(5) while True: try: server, addr = sock.accept() data = server.recv(1024) self.edit.append(data) server.send('I got ' + data) server.close() except: self.edit.append('Cut Down') break sock.close() app = QtGui.QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
时间: 2024-10-02 23:12:31