2012-04-04 107 views
0

我创建使用PyQt.But我有奇怪的错误一个简单的服务器/客户端应用程序:为什么我无法更改self.filename?

这里是我的服务器端代码:

#! /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.It看来我不能用正确的值传递...

这里是我的客户端代码:

#! /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_() 

感谢您的任何帮助。

回答

2

你可能在self.filenameNULL字节(\0\x00),并为错误指示,你不能打开一个文件包含NULL字节的名称。事先妥善处理(例如:删除,替换等)。

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

+0

你太棒了!但是'\ 0'是如何进入我的代码?这很奇怪。我意识到我的第二个错误〜。非常感谢! – Gnijuohz 2012-04-05 00:10:46

+0

@Gnijuohz:恩,它来自你的插座。我不确定问题是什么,但是看看你的客户端代码:'self'socket.sendall(“f”+ name)''中的'name'变量是'QString'或'unicode'。这可能有问题。尝试编码/将其转换为字符串。 – Avaris 2012-04-05 01:07:59

+0

我发现文件名也不能有中文字,我该怎么解决呢? – Gnijuohz 2012-04-05 01:11:07

相关问题