我想使用python套接字编程和PyQt创建一个简单的服务器/客户端应用程序。客户端可以将文件发送到服务器端。它工作。但我看不到我的服务器端GUI!
在接受 Dikei 的建议后,我通过创建一个新线程来处理套接字部分来更改我的服务器端代码。
这是我的服务器端代码:
#! /usr/bin/python
import sys
import socket
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
HOST = '127.0.0.1'
PORT = 9996
SIZEOF_UINT32 = 4
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.worker = Worker()
self.connect(self.worker, SIGNAL("received"), self.updateUi)
self.connect(self.worker, SIGNAL("finished()"), self.updateUi)
self.connect(self.worker, SIGNAL("terminated()"), self.updateUi)
# Create widgets/layout
self.browser = QTextBrowser()
self.selectButton = QPushButton('Close server')
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.selectButton)
self.setLayout(layout)
self.setWindowTitle("Server")
def updateUi(self, text):
self.browser.append(text)
class Worker(QThread):
def __init__(self,parent = None):
super(Worker, self).__init__(parent)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def receiveFile(self):
self.conn, self.addr = self.socket.accept()
totalData = ''
while 1:
data = self.conn.recv(1024)
if not data: break
totalData += data
print totalData
f = open('/home/jacos/downfrom','w')
f.write(totalData)
f.close()
self.emit(SIGNAL("received"),"received a file")
self.conn.close()
def run(self):
self.socket.bind((HOST, PORT))
self.socket.listen(5)
while 1:
self.receiveFile()
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
这是我的客户端代码:
#! /usr/bin/python
# -*- coding: utf8 -*-
import sys
import socket
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
HOST = '127.0.0.1'
PORT = 9996
SIZEOF_UINT32 = 4
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
# Create widgets/layout
self.browser = QTextBrowser()
self.selectButton = QPushButton('Send a File')
self.connectButton = QPushButton("Connect")
self.connectButton.setEnabled(True)
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.selectButton)
layout.addWidget(self.connectButton)
self.setLayout(layout)
# Signals and slots for line edit and connect button
self.selectButton.clicked.connect(self.sendFile)
self.connectButton.clicked.connect(self.connectToServer)
self.setWindowTitle("Client")
# Update GUI
def updateUi(self, text):
self.browser.append(text)
def sendFile(self):
filename=QFileDialog.getOpenFileName(self, 'Open File', '.')
self.socket.sendall(open(filename,'rb').read())
self.updateUi("Sent a file:" + filename)
self.socket.close()
self.connectButton.setEnabled(True)
def connectToServer(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((HOST, PORT))
self.connectButton.setEnabled(False)
self.updateUi("Connected")
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
问题是:
- 我的服务器端 GUI 根本没有出现!我不明白为什么。我应该使用线程吗?
- 此外,服务器仅将文件以固定名称存储在固定位置。有什么方法可以指定服务器端的位置并从客户端获取原始文件名?
编辑:现在#1已解决,但线程发出的信号似乎根本没有被捕获,服务器的浏览器根本没有更新。
#2仍未解决。
谢谢你的帮助!