我使用PyQt创建了一个简单的服务器/客户端应用程序。但我得到了一个奇怪的错误:
这是我的服务器端代码:
#! /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 = 9991
SIZEOF_UINT32 = 4
class Form(QDialog):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((HOST, PORT))
self.socket.listen(5)
self.worker = Worker(self.socket)
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")
self.worker.start()
def updateUi(self, text):
self.browser.append(text)
class Worker(QThread):
def __init__(self,socket,parent = None):
super(Worker, self).__init__(parent)
self.socket = socket
self.dir = '/home/jacos/down/'
self.filename = '/home/jacos/down/hello'
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
if totalData.find('f') == 0:
name = totalData.strip()[1:]
self.filename = self.dir + name
print self.filename
else:
self.saveFile(totalData)
print self.filename
self.emit(SIGNAL("received"),QString("received a file"))
def saveFile(self,data):
f = open(self.filename,'wb')
print self.filename
f.write(data)
f.close()
self.conn.close()
def run(self):
while 1:
self.receiveFile()
app = QApplication(sys.argv)
form = Form()
form.show()
app.exec_()
当我运行它时,我得到了这个:
Traceback (most recent call last):
File "/home/jacos/bin/tss.pyw", line 75, in run
self.receiveFile()
File "/home/jacos/bin/tss.pyw", line 61, in receiveFile
self.saveFile(totalData)
File "/home/jacos/bin/tss.pyw", line 66, in saveFile
f = open(self.filename,'wb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
TypeError: updateUi() takes exactly 2 arguments (1 given)
问题是关于self.filename。我似乎无法用正确的值传递它...
这是我的客户端代码:
#! /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 = 9991
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.sendFileName)
self.connectButton.clicked.connect(self.connectToServer)
self.setWindowTitle("Client")
# Update GUI
def updateUi(self, text):
self.browser.append(text)
def sendFileName(self):
filename=QFileDialog.getOpenFileName(self, 'Open File', '.')
name = filename.split('/')[-1]
self.updateUi("Sent file name:" + name)
self.socket.sendall("f" + name)
self.socket.close()
self.connectToServer()
self.sendFile(filename,name)
def sendFile(self,filename,name):
self.socket.sendall(open(filename,'rb').read())
self.updateUi("Sent 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_()
感谢您的帮助。
答案 0 :(得分:2)
NULL
中可能有一个\0
字节(\x00
或self.filename
),如错误所示,您无法打开名称中的文件包含NULL
个字节。事先适当地处理它们(例如:删除,替换等)。
至于其他错误:您正在将两个信号(finished
和terminated
)连接到self.updateUi
。并且这些信号没有传递任何参数,而self.updateUi
期望传递参数,即text
。我不确定您的目标是什么,但您可以考虑为text
中的self.updateUi
参数添加默认参数。