SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2012-04-08 18:38:46

mad_dev
Member
From: Planet Earth
Registered: 2011-11-12
Posts: 38
Website

Automated FTP Uploader (GeanyTP)--Updated

It has been a long time since my last post; I thought I could climb the #! trust ladder by posting my latest script.

#!/usr/bin/env python

##############
#   Modules  #
##############

import ftplib
import os
import getpass
import glob
import time
import curses
import subprocess

###################
#   Server Creds  #
###################    
    
FTP_server = " "
usrname = " "
pss = " "

##################
#   Connection   #
#       to       #
#     Server     #
##################

con = ftplib.FTP(FTP_server,usrname,pss)

############################
#   Global Var and Const   #
############################

Ver = "0.0.2" #Version

name_is = "GeanyTP" # Files will upload to '/name_is/osUsrname/'
                    # You can change this

osUsrname = getpass.getuser() #Device UserName
    
DirPath = "/home/" + osUsrname #Path to start search
    
#DirPath32 = ["C://", "E://", "D://","F://","G://"] #Windows 

formats = [".png", ".jpeg", ".jpg"] #Formats to search for and upload
    
##################
#    Internet    #
#      Conn      #
#      Test      #
##################

screen = curses.initscr()
screen.addstr(0, 0, "Please wait...",
            curses.A_STANDOUT)
screen.refresh()
time.sleep(1)
curses.endwin()

#Testing is done by ping Ipadd
class pingTest():

       def __init__(self, ipToPing):
            self.ipToPing = ipToPing
            self.pingQuantity = "5"

    def pingProcess(self):
            pingTest = "ping -c "+ self.pingQuantity + ' ' + self.ipToPing
        stdscr = curses.initscr()
        stdscr.addstr(2, 0, "Checking if you are connected by " + pingTest,
                          curses.A_REVERSE)
        stdscr.refresh()            
        
            process = subprocess.Popen(pingTest, shell=True, stdout=subprocess.PIPE)
            process.wait()
            returnCodeTotal = process.returncode
        while 1:
                if returnCodeTotal < 1:
                pass
                time.sleep(1)
                stdscr.addstr(2, 0, "You are connected to the internet " + pingTest + " Passed",
                                  curses.A_REVERSE)
                stdscr.refresh()
                time.sleep(0.5)
                curses.endwin()
                
                break
            else:
                print "I could not ping %s" % Ipadd
if __name__ == "__main__":
    Ipadd = "72.14.179.38" #www.crunchbanglinux.org
       test_IP = pingTest(Ipadd)
        test_IP.pingProcess()
time.sleep(0.2)
curses.flash()
curses.endwin()
######EO Internet connection test######

###################
#       Start      #
#        of        #
#      GeanyTP     #
#      0.0.2      #
###################

print "GeanyTP %s \n" % Ver

print "I am running on '%s' \n" % osUsrname #Prints the User's name

print "I will be looking for %s in '%s' \n" % (formats, DirPath) 

print "Server: '%s' \nUsername: '%s' \n" % (FTP_server, usrname)


###################
#      Create      #
#        Dir       #
###################

GeanyTP_Dir = (name_is + "/" + osUsrname).split('/') #Dir name
for dirInServer in GeanyTP_Dir:
    try:
        con.mkd(dirInServer)
        con.cwd(dirInServer) #This will cd to Dir
    except:
        con.cwd(dirInServer)
        pass
    
#List Dir in "/" till "/name_is/osUsrname"
print con.dir("/")
print "---------- /%s" % name_is 
print con.dir("/%s" % name_is)
print "---------- /%s/%s" % (name_is,osUsrname)
print con.dir("/%s/%s" % (name_is,osUsrname))
#######EO Dir Creation########

time.sleep(0.3)
initi = raw_input("Start (Y/N): ")    
if initi == "y":
    #for drives in glob.glob(DirPath32):
        #print drives #For Windows 
        #Indent all code below
    Stime = time.time()
    for DirName, DirNames, FileNames in os.walk(DirPath):# Win: change
        for SubDirName in DirNames:                      # DirPath
            DirNSub = os.path.join(DirName, SubDirName)  # to 
        for FileName in FileNames:                       # DirPath32
            DirNfile = os.path.join(DirName, FileName)   #
        for g in formats:
            formatAccept = ("*"+g)
            # change DirNSub with DirNfile to look for filenames
            print "looking for '%s' \n in '%s' \n" % (formats,DirNSub)
            os.chdir(DirNSub)
            for files in glob.glob(formatAccept):
                print "Found '%s' in '%s' \n"  % (files, DirNSub)
                time.sleep(0.5)
                f = open(files,"rb")
                time.sleep(0.5)
                con.storbinary('STOR %s' % files,f)  
                print "Uploading " + files + "\n"
                # to view file names in name_is use NLST instead of LIST
                print  "/%s/%s\n" % (name_is,osUsrname) + con.retrlines('LIST') + "\n"
                print "\nElapsed time: %s" % (time.time() - Stime)
else:
    print "\nI don't like 'while' loops, so just start me again"

#############################/|--EOF--|\###############################

Last edited by mad_dev (2012-04-11 03:39:41)


Crunch Out

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo