Пример #1
0
def return_dir(host,remote_path,otest_path):
	client_path=join_path(otest_path,remote_path)
	print "searching",client_path,remote_path
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	sock.connect((host, 10001))
	sock.sendall(encode_for_tcp(remote_path))
	for path, dirs, files in os.walk(client_path):
		print path
		for file_name in files:
			#print file_name,"rod",host
			if file_name.endswith(".dat")==True or file_name.endswith(".inp")==True :
				local_file=os.path.join(path,file_name)
				target=os.path.join(path,file_name)
				if target.startswith(otest_path):
	    				target=target[len(otest_path):]

				if os.path.isfile(local_file):
					got_data=False
					try:
						sendfile = open(local_file, 'rb')
						#print "I will send",local_file,target,host
						data = sendfile.read()
						zip_data=data.encode("zlib")
						sendfile.close()
						got_data=True
					except:
						print "I could not open the file:"+local_file
						got_data=False

					if len(data)!=0 and got_data==True:
						try:
							sock.sendall(encode_for_tcp(target)+encode_for_tcp(len(zip_data)))
							sock.sendall(zip_data)
						except:
							print "I could not send the data!!!"
							return False
							#sock.close()

	sock.close()
	return True
	def handle(self):
		global output_path
		print "Receiving files"
		buf=""
		while True:
			data = self.request.recv(1024)
	  		if data:
				buf=buf+data
	    		else:
				#print "zero length packet",len(buf)
				break

			#print len(buf)
		print "buffer len",len(buf)
		if len(buf)==0:
			print "I have received a zero length buffer"

		#self.request.close()

		pos=0
		job_name=buf[pos:pos+200].rstrip()

		pos=pos+200
		n=0
		frag_error=False
		#frag_error_text=""
		while True:
			if pos+200<len(buf):
				name=buf[pos:pos+200].rstrip()
			else:
				frag_error=True
				break

			#print "Name",buf[pos:pos+200]
			pos=pos+200
			#print "Length",buf[pos:pos+200]

			if pos+200<len(buf):
				length=int(buf[pos:pos+200].rstrip())
			else:
				frag_error=True
				break

			pos=pos+200

			if pos+length>len(buf):
				frag_error=True
				break

			#print "Joining ", output_path,name
			output_file=join_path(output_path,name)
			#print "Writing file:"+output_file+"<"
			#print "Output path:"+output_path+"<"
			#path=os.path.dirname(output_file)
			#print "name:"+name+"<"
			#print "test:"+os.path.join(output_path,name)+"<"
			ensure_dir(output_file)

			out=open(output_file, 'wb')
			read_in=buf[pos:pos+length]
			de_comp=read_in.decode("zlib")
			out.write(de_comp)
			out.close()
			if n==0:
 				print "{} wrote:".format(self.client_address[0])
				print "Rx file:"+name

			n=n+1

			if output_file.endswith("go.o"):
				os.chmod(output_file, 0777)

			pos=pos+length
			if pos>=len(buf):
				break

		del(buf)

		if frag_error==True:
			print "Data fragmentation error!!!!!!!!!!!!!!!!!!!!!!!!"

		#sleep(0.5)
		if job_name!="":
			if respond_ip!="":
				s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
				s.sendto("iwanttorun#"+job_name, (respond_ip, respond_port))
				s.close()