2016-05-16 50 views
0

機能の1つがサーバーからのダウンロードファイルであるC/Sプロジェクトを実行しています。 クライアントはPythonで書かれており、サーバーはjavaで書かれています。 -f filenameは、ファイルを取得するコマンドです。 唯一の問題は属性エラー:「timer」オブジェクトに属性「ダウンロード」がありません

def download(self,filename): 
    print "Start download file" 
    self.sock.send("DOWNLOAD"+"sprt"+filename) 
    downloadData = self.sock.recv(); 
    print downloadData 

if message[0:message.find(" ")] == "-f": 
    if not (message.split(" ")[1]) or len(message.split(" "))>2 : 
     print "Usage -f filename\n" 
    else: 
     client.download(message[message.find(" ")+1:]) 

一部です。対照的に

AttributeError: 'timer' object has not attribute 'download'. 

def upload(self,filename): 
    print "server ready , now client sending file~~" 
    try: 
     f = open(filename,'rb') 
     while (True): 
      data = f.read(); 
      #if file is none 
      if data is None: 
       break; 
      #Notify the java server that a file is going to be sent.   
      #sprt stands for seperator 
      self.sock.sendall("FILE"+"sprt"+filename+"sprt"+data+'\n') 
      break; 
     f.close();  
     time.sleep(1) 
     #Notify the java server that the file is complete 
     self.sock.send("EOF\n") 
     print "send file success!" 
    except IOError: 
     print "No such file or Directory" 

方法が正常に動作します。

何が原因ですか?ありがとう

ここにファイル全体があります。

import threading 
import sys 
import time 
import socket 

class timer(threading.Thread): 
    def __init__(self): 
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
     self.sock.connect(('localhost', 9991)) 
     self.isrun = True 
     threading.Thread.__init__(self); 

    def run(self): 
     while self.isrun: 
      revice = self.sock.recv(1024); 
      print ("recv> " + revice); 
     self.sock.close() 

    def send(self,str): 
     self.sock.send(str + "\n") 

    def close(self): 
     self.isrun=False 
    def upload(self,filename): 
     print "server ready , now client sending file~~" 
    try: 
     f = open(filename,'rb') 
     while (True): 
      data = f.read(); 
      #if file is none 
      if data is None: 
       break; 
      #Notify the java server that a file is going to be sent.   
      #sprt stands for seperator 
      self.sock.sendall("FILE"+"sprt"+filename+"sprt"+data+'\n') 
      break; 
     f.close();  
     time.sleep(1) 
     #Notify the java server that the file is complete 
     self.sock.send("EOF\n") 
     print "send file success!" 
    except IOError: 
     print "No such file or Directory" 

    def download(self,filename): 
     print "Start download file" 
     self.sock.send("DOWNLOAD"+"sprt"+filename) 
     downloadData = self.sock.recv(); 
     print downloadData 





def main(): 
    client = timer() 
    client.start() 
    print "Welcome:\n","Command to be used:\n","-a filename\n" "-c number\n", "-f filename\n","-h hostname:port\n","-n name\n","-u certificate\n","-v filename certificate\n","otherwise input will be treated as normal message" 

    while (True): 
     # get input from user 


     message = str(raw_input("send> ")); 

     #Space exists and not occupies the first place 
     if ((message.find(" "))!= -1 and message.find(" ")>0): 

      if message[0:message.find(" ")] == "-a": 
       #if there is a space but there is nothing following -a "-a " 
       #or if there are more than one space following -a "-a j" or "-a h j" len(message.split(" ") return the size of array after token, need to be exactly 2; 
       if not message.split(" ")[1] or len(message.split(" "))>2 : 
        print "Usage -a filename\n"     
       #normal execution 
       else: 
        client.upload(message[message.find(" ")+1:])    

      if message[0:message.find(" ")] == "-c": 
       if not (message.split(" ")[1]) or len(message.split(" "))>2 : 
        print "Usage -c number\n" 
       else:  
        print "provide the required circumference (length) of a circle of trust" 

      if message[0:message.find(" ")] == "-f": 
       if not (message.split(" ")[1]) or len(message.split(" "))>2 : 
        print "Usage -f filename\n" 
       else: 
        client.download(message[message.find(" ")+1:]) 

      if message[0:message.find(" ")] == "-h": 
       if not (message.split(" ")[1]) or len(message.split(" "))>2 : 
        print "Usage- h hostname:port\n"      
       else: 
        print "provide the remote address hosting the oldtrusty server" 


      if message[0:message.find(" ")] == "-n": 
       if not (message.split(" ")[1]) or len(message.split(" "))>2 : 
        print "Usage -n name\n"     
       else: 
        print "require a circle of trust to involve the named person (i.e. their certificate)" 


      if message[0:message.find(" ")] == "-u": 
       if not (message.split(" ")[1]) or len(message.split(" "))>2 : 
        print "Usage -u certificate\n"     
       else: 
        print "upload a certificate to the oldtrusty server" 


      if message[0:message.find(" ")] == "-v": 
       #if there are exactly two spaces "-v a b" , normal execution 
       if(len(message.split(" ")) == 3): 
        print "vouch for the authenticity of an existing file in the oldtrusty server using the indicated certificate" 
       else: 
        print "Usage: -v filename certificate\n" 

     elif (message == "-l"): 
      print "list all stored files and how they are protected" 

     elif(message=="-a") or (message=="-c") or (message=="-f")or (message=="-h") or (message=="-n")or (message=="-u") or (message=="-u") or (message=="-v"): 
      print"Usage :\n","-a filename\n" "-c number\n", "-f filename\n","-h hostname:port\n","-n name\n","-u certificate\n","-v filename certificate\n" 

     # exit if the input is 'exit'  
     elif (message == "exit"): 
      client.send("EXIT"+"sprt"); 
      client.close(); 
      time.sleep(0.01); 

     #Normal Commmunication 
     else: 
      print "Other situation"  
      print message; 
      client.send("NORMAL"+"sprt"+message); 



if __name__=='__main__': 
    main() 

答えて

2

あなたが使用しているファイルは、ここで1つの重要な面でインデントされているファイルとは異なります。 downloadメソッドがインデントされすぎて(実行中に定義されるようになる)、少なすぎる(timerに関連するモジュールレベルの機能ではなくモジュールレベルの機能になります)。 def downloadの字下げが正しいことを確認してください(例:4スペース)。

あなたはそれに取り組んでいる一方で、

downloadData = self.sock.recv(); 

ラインはあまりにもいくつかの変更が必要になります。ほとんどの場合、線に沿って何かが必要です

downloadData = self.sock.recv(4096) 
+0

ありがとうございます。私はタブを使用しています...なぜ動作しているのかわかりません。 – bslqy

関連する問題