def write_file(self):
   output = open('chan_output.htm', 'w')
   for channel in self.main.channels:
     self.logger.log(LOG_DEBUG, "Writing output for %s, topic %s" % (channel, self.topics[channel]))
     output.write("<b>Channel:</b> %s\n<br /><b>Topic:</b> %s\n<br /><b>Users:</b>\n<ul>\n" % (channel, self.topics[channel]))
     for user in self.main.channels[channel]['users']:
       output.write("  <li>%s</li>\n" %(user))
     output.write("</ul>\n\n")
   output.close
   
   output = open('chan_output.htm', 'r')
   self.update_all = 0
   ftp_server = 'whub25.webhostinghub.com'
   ftp_user = '******'
   passfile = open('password.txt','r')
   password = passfile.readline()
   passfile.close()
   password = password.rstrip('\n')
   self.logger.log(LOG_INFO, "Connecting to FTP server %s, username %s" % (ftp_server, ftp_user))
   ftp = FTP_TLS(ftp_server, ftp_user, password)
   ftp.prot_p()
   self.logger.log(LOG_INFO, "Successfully logged in, storing file")
   ftp.storlines('STOR chan_output.htm', output)
   ftp.close()
   output.close()
   self.update_all = 0
   self.logger.log(LOG_INFO, "Done")
def storeFtplib(dataframe, filename="cameliaBalAGKevin.csv", compression = None, toPrint = False):
    """
    function that connects to the remote FTP serveur and upload a pandas dataframe
    the upload file must be a pandasDataframe and will be written in a csv file.
    It can be uploaded as a bz2, gz encoded or not encoded at all
    if it is encoded, the right extension must be present in the name
    -- IN
    dataframe : the dataframe to upload (pandas.Dataframe)
    filename : the filename with its extension to be downloaded from the remote ftp server (string)
    compression : string that specifies the encoding of the file (string in [None,"gz","bz2"] default: None
    toPrint : boolean that settles if the function should print its progress and results (boolean) default: False
    -- OUT
    flag : boolean that settles if everything was successful (True: no problem, False: an error occured)
    """
    startTime = time.time()
    if toPrint:
        print ""
        print ""
        print "==========================================="
        print "=== Connection to the remote FTP server ==="
        print "==========================================="
        print ""
        print "using ftplib"
        print "loading :",filename
        print "" 
    ftp = FTP_TLS()
    # retrieving information about account on ftp server
    (user, password, host, port) = getAccount()
    if user==None:
        print "error : coudn't read the account information"
        return False
    # connecting and logging in
    try:
        ftp.connect(host,port)
        ftp.login(user,password)
    except:
        print "error : unable to connect to the ftp server"
        return False
    # establishing the security protocol
    ftp.prot_p()
    if toPrint:
        print "connected to the FTP server"
    try:
        lines = dataframe.to_csv(path_or_buff = None,sep="\t",columns=dataframe.columns)
    except:
        print "error : impossible to convert the dataframe into csv lines"
        return False
    sio = StringIO.StringIO(lines)
    ftp.storlines(cmd="STOR "+filename, fp=sio)
#     try:
#         ftp.storlines(cmd="STOR "+filename, file=lines)
#     except:
#         print "error : impossible to upload the file"
#         return False
    interval = time.time() - startTime 
    if toPrint:
        print 'Dataframe uploaded :', interval, 'sec'
    return True
    
示例#3
0
def send_to_ftp(filepath):
    ftps = FTP_TLS('ftpes.learn.inf.puc-rio.br') # Define Ftp server
    ftps.login('learn', 'LRepoAdm18!!') # Login to ftp server
    ftps.prot_p() # Enable data encryption
    ftps.retrlines('LIST') # List Directory
    ftps.cwd('/projeto/01_tarefas_de_geofisica/QualiSismo/CV_results') # Change Directory
    ftps.storlines("STOR " + filepath, open(filepath,'rb'))
    
示例#4
0
 def whitelistUpdate(self):
     ftp = FTP_TLS()
     #ftp.set_debuglevel(2)
     ftp.connect(os.getenv("FTP_IP"), int(os.getenv("FTP_PORT")))
     ftp.login(os.getenv("FTP_LOGIN"), os.getenv("FTP_PASSWORD"))
     whitelist = open(self.whitelistJSON, 'rb')
     ftp.storlines('STOR whitelist.json', whitelist)
     ftp.quit()
     return
示例#5
0
def ftp_to_ml(localfile, remote_file):
    (user, acct, passwd) = netrc.netrc().authenticators('ftps.b2b.ml.com')
    ftp = FTP_TLS('ftps.b2b.ml.com')
    remote_dir = 'incoming/gmigefs2'
    ftp.login(user, passwd, acct)
    ftp.prot_p()
    ftp.cwd(remote_dir)
    local_fd = open(localfile, 'r')
    ftp.storlines('STOR ' + remote_file, local_fd)
    local_fd.close()
示例#6
0
def TimerCallback():
	data = getConfig()
	
  # loop processes list and kill each one
	processes = data["processes"]
	for line in processes.splitlines():
		Log("Killing Process %s " % line[line.rindex('\\')+1:])
		os.system('taskkill /f /im %s' % line[line.rindex('\\')+1:])
	
	
	# Connect to  SFTP 
	ftps = FTP_TLS('fuge.it')
	ftps.login('testuser', 'testpass')           # login anonymously before securing control channel
	ftps.prot_p()          # switch to secure data connection.. IMPORTANT! Otherwise, only the user and password is encrypted and not all the file data.
	ftps.retrlines('LIST')

		
		
  # Loop directories/files and sftp each one
	directories = data["directories"]
	for line in directories.splitlines():
		# If nothing after last slash then this is a directory we need to loop for files
		if line[line.rindex('\\')+1:] == "": 
			for fn in os.listdir(line):
				 if os.path.isfile(fn):
					# upload file to public/ on remote
					myfile = open(fn, 'r')
					ftps.storlines('STOR ' + fn, myfile)
					myfile.close()

		else: # otherwise it's a single file
			if os.path.isfile(line):
				# upload file to public/ on remote
				localpath = line
				myfile = open(line, 'r')
				ftps.storlines('STOR ' + filename, myfile)
				myfile.close()

				
	ftps.close()
	
	# reset daemon for tomorrow's run
	try: win32api.WinExec('daemon.exe %d %d %d %d' % (day, hr, min, sec)) # Works seamlessly
	except: pass
	
	# loop processes list and kill each one
	processes = data["processes"]
	for line in processes.splitlines():
		Log("Restarting Process %s " % line)
		try: win32api.WinExec(line) # Works seamlessly
		except: pass
示例#7
0
ax.format_coord = Formater
fig.canvas.mpl_connect('button_press_event', Click)
fig.tight_layout()
plt.show()

print("Save data to cloud?\n")
while True:
    if keyboard.is_pressed('\r'):
        OriginalPNG = Image.open(DIR + "Color" + FN + ".png")
        ConvertedJPG = OriginalPNG.convert('RGB')
        ConvertedJPG.save(DIR + "Color" + FN + ".jpg")

        ftp = FTP_TLS('FTP server goes gere')
        ftp.login('Username', 'Password')
        ftp.cwd("site/wwwroot/")
        ftp.storlines('STOR ' + "DepthMapData.dat",
                      open(DIR + "RGBD" + FN + ".dat", 'rb'))
        ftp.storbinary('STOR ' + "ReferenceFrame.jpg",
                       open(DIR + "Color" + FN + ".jpg", 'rb'))
        ftp.quit()

        os.remove(DIR + "Color" + FN + ".jpg")
        print("Data from Frame " + str(FN) + " saved to cloud.\n")
        break
    elif keyboard.is_pressed(' '):
        print("Exited without uploading to cloud.\n")
        break
    else:
        pass

# Press Enter to upload to cloud, Space to exit.
class DiagnosticsResponderScript(OCPPScript):
    def __init__(self, nr_idle_before_upload=1):
        super().__init__("DiangosticsResponder")
        self.is_active = True
        self.location = None
        self.retries = 1
        self.state = None
        self.next_time_to_send = 0
        self.idle_count = 0
        self.nr_idle_before_upload = nr_idle_before_upload
        self.ftp_conn = None
        self.file_name = None

    def on_msg_received(self, msg):
        super().on_msg_received(msg)
        parsed = self.parse_ocpp_message(msg)
        if parsed.type == "GetDiagnostics":
            if self.state:
                logger.warning(
                    "Will not start second script is currently in state {}".
                    format(self.state))
                filename = None
            else:
                self.state = "Starting"
                self.next_time_to_send = time.time() + 2
                self.location = parsed.payload["location"]
                self.retries = parsed.payload.get("retries", 1)
                self.file_name = "diagnostics_test_{}.txt".format(time.time())
                filename = self.file_name

            self.send_ocpp_response(parsed.uuid, {"fileName": filename})

    def on_update(self, dt):
        now = time.time()
        if self.is_active and self.next_time_to_send < now and self.state is not None:
            try:
                self._state_running()
            except Exception as e:
                self.state = None
                self.ftp_conn = None
                self._create_status_update("UploadFailed")
                logger.error(
                    "Failure in processing of diagnostic. Will fail operation",
                    exc_info=e,
                )

    def _state_running(self):
        now = time.time()
        diff_to_next_tick = 10
        if self.state == "Starting":
            self._create_status_update("Idle")
            self.state = "Idle"
            self.idle_count = 0
        elif self.state == "Idle":
            self._create_status_update("Idle")
            self.idle_count = self.idle_count + 1
            if self.idle_count >= self.nr_idle_before_upload:
                self.state = "StartUpload"
        elif self.state == "StartUpload":
            try:
                parsed_url = urlparse(self.location)
                port = parsed_url.port if parsed_url.port else 0
                if parsed_url.scheme == "ftp":
                    self.ftp_conn = FTP()
                elif parsed_url.scheme == "ftps":
                    self.ftp_conn = FTP_TLS()
                    # https://stackoverflow.com/a/12568289
                    self.ftp_conn.prot_p()
                self.ftp_conn.connect(host=parsed_url.hostname, port=port)
                self.ftp_conn.login(user=parsed_url.username,
                                    passwd=parsed_url.password)
            except Exception as e:
                logger.error("Error on ftp connection. Will stop script.",
                             exc_info=e)
                self._create_status_update("UploadFailed")
                self.state = None
                self.ftp_conn = None
                return
            diff_to_next_tick = 5
            self.state = "Upload"
        elif self.state == "Upload":
            self._create_status_update("Uploading")

            ftp_cmd = ("STOR {}").format(self.file_name)
            with open("/tmp/{}".format(self.file_name), "w") as text_file:
                for _ in range(0, 1000):
                    text_line = "{}\n\t".format(str(uuid.uuid4()))
                    text_file.write(text_line)
            with open("/tmp/{}".format(self.file_name), "rb") as text_file:
                self.ftp_conn.storlines(ftp_cmd, text_file)
            diff_to_next_tick = 20
            self.state = "Finishing"
        elif self.state == "Finishing":
            self.ftp_conn.close()
            self.state = None
            self._create_status_update("Uploaded")
        self.next_time_to_send = now + diff_to_next_tick

    def _create_status_update(self, status):
        return self.send_ocpp_request("DiagnosticsStatusNotification",
                                      {"status": status})

    def toggle_status(self):
        """Send a upload failed on deactivation."""
        super().toggle_status()
        if self.is_active and self.state:
            self._create_status_update("UploadFailed")
            if self.ftp_conn:
                self.ftp_conn.close()
                self.ftp_conn = None
            self.state = None