属性错误:“计时器”对象没有属性“下载”

时间:2016-05-16 11:45:42

标签: python python-2.7

我正在做一个C / S项目,其中一个功能是来自服务器的下载文件。 客户端是用python编写的,服务器是用java编写的。 -f filename是一个获取文件的命令。 唯一的问题是

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()

1 个答案:

答案 0 :(得分:2)

您使用的文件与您在此处发布的文件的一个重要方面不同:缩进。您的download方法要么缩进太多(会在运行upload时定义它),要么太少(这会使它成为模块级函数,而不是与timer关联的函数)。确保def download周围的缩进正确,即4个空格。

当你在它时,行

downloadData = self.sock.recv();

也需要一些修改。最有可能的是,你想要的东西是

downloadData = self.sock.recv(4096)