0

我使用 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_()

谢谢你的帮助。

4

1 回答 1

2

您可能有一个NULL字节(\0\x00),self.filename并且如错误所示,您无法打开名称包含NULL字节的文件。事先适当地处理它们(例如:移除、更换等)。

至于另一个错误:您将两个信号 (finishedterminated) 连接到self.updateUi. 并且这些信号没有传递任何参数,而self.updateUi期望传递一个参数,即text. 我不确定您的目标是什么,但您可以考虑为textin 中的参数添加默认参数self.updateUi

于 2012-04-04T18:31:39.030 回答