Python 属性错误:";计时器“;对象没有属性“;下载「;
我正在做一个C/S项目,其中一个功能是从服务器下载文件。 客户端是用python编写的,服务器是用java编写的。Python 属性错误:";计时器“;对象没有属性“;下载「;,python,python-2.7,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 download
-f filename
是获取文件的命令。
唯一的问题在于
def download(self,filename):
print "Start download file"
self.sock.send("DOWNLOAD"+"sprt"+filename)
downloadData = self.sock.recv();
print downloadData
及
部分
相比之下
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()
您正在使用的文件与您在此发布的文件在一个重要方面不同:缩进。您的
download
方法缩进太多(这将使它在运行upload
时被定义),或者缩进太少(这将使它成为一个模块级函数,而不是与计时器相关联的函数)。确保def download
周围的缩进正确,即4个空格
当你这么做的时候,电话线
downloadData = self.sock.recv();
也需要一些修改。最有可能的情况是,你想要的东西与
downloadData = self.sock.recv(4096)
您正在使用的文件与您在此发布的文件在一个重要方面不同:缩进。您的download
方法缩进太多(这将使它在运行upload
时被定义),或者缩进太少(这将使它成为一个模块级函数,而不是与计时器相关联的函数)。确保def download
周围的缩进正确,即4个空格
当你这么做的时候,电话线
downloadData = self.sock.recv();
也需要一些修改。最有可能的情况是,你想要的东西与
downloadData = self.sock.recv(4096)