[Gambas-user] ftp Client (correction)
Caveat
Gambas at ...1950...
Sat Sep 5 10:52:01 CEST 2009
Sorry Lars, a couple of errors got introduced into my mail just after I
hit the "Send" button ;-)
Obviously, you need a 4th param on the call to getFileAsString, which is
the name of a temporary dir to use (so something like /tmp), thus:
allData =
myRequestManager.getFileAsString("192.168.1.2//var/share/dvb2001/services.dat", "root", "password", "/tmp")
In the getFileAsString function, the line:
fileName = tmpDir &/ "drag_cont_tmp_" & userName & ".tmp"
constructs a path to a temporary file, which in your case (if you use
"/tmp" as the temporary dir, and root as the user) would be:
/tmp/drag_cont_tmp_root.tmp
This may not be what you want, so feel free to change this line to suit
your needs (drag_cont comes from the program this class is used in:
DragonController).
And in the CATCH at the end of getFileAsString, the call to my logging
function should read:
Logger.logMessage("getFileAsString: " & Error.Text & " @ " &
Error.Where, TRUE)
In the end, you may not need getFileAsString at all, as getFileByFTP is
all you need to actually get a file off of an FTP server and onto your
local machine, so you could just make that a PUBLIC SUB and call it
directly.
Regards,
Caveat
P.S. In case you're interested, as a free bonus, here's also the code
for my logging module:
' Gambas module file
PRIVATE debugMode AS Integer
PRIVATE logIndex AS Integer
PRIVATE logLines AS Collection
PUBLIC CONST NO_DEBUG AS Integer = 0
PUBLIC CONST DEBUG_LOG_ONLY AS Integer = 1
PUBLIC CONST DEBUG_LOG_AND_PRINT AS Integer = 2
PUBLIC CONST DEBUG_MESSAGE_LOG_AND_PRINT AS Integer = 3
PUBLIC SUB setDebugMode(newMode AS Integer)
debugMode = newMode
END
PUBLIC SUB logMessage(msgText AS String, showAsError AS Boolean)
IF debugMode = NO_DEBUG THEN
RETURN
END IF
getLogLines().add(msgText, logIndex)
logIndex = logIndex + 1
IF debugMode = DEBUG_LOG_AND_PRINT THEN
PRINT msgText
ELSE IF debugMode = DEBUG_MESSAGE_LOG_AND_PRINT THEN
message(msgText)
PRINT msgText
END IF
IF showAsError THEN
message(msgText)
END IF
END
PUBLIC FUNCTION getLogLines() AS Collection
IF logLines = NULL THEN
logLines = NEW Collection
END IF
RETURN logLines
END
PUBLIC SUB displayLogLines()
DIM aLogLine AS String
FOR EACH aLogLine IN logLines
PRINT aLogLine
NEXT
END
On Sat, 2009-09-05 at 10:21 +0200, Caveat wrote:
> Hi Lars,
>
> Here's some code that should be enough to get you started. The code is
> from a Gambas class which I called RequestManager. I have cut out some
> of the code out as it wasn't relevant to FTP (there was stuff for doing
> an HTTP GET and some code for querying services on a particular port).
>
> ISTR there was some flakiness with the FTP client where it bombs out if
> you send a blank (empty string) password, so if your FTP server doesn't
> require one, just set the FTP client's password to "dummy" or something
> and your FTP server *should* ignore it... failing that, you may have to
> set a dummy password.
>
> Note the difference between the local file name and the name of the file
> you want to get off the server. The name of the file on the server is
> normally included in the url set on FTPClient.URL (see the calling
> instructions) and the name of the file on your local machine is passedc
> to the FTPClient.get method. If you want to PUT files on the FTP server
> instead of GETting them, I'm sure you can adapt the code.
>
> Bear in mind that getting a huge binary file as a string is probably not
> a great idea. My getFileAsString method is really designed to work for
> a small textual line-based file. You'll just need to wrap getFileByFTP
> in a different way if you want to deal with binary or larger files.
> Give me a shout if you get stuck or something is not clear.
>
> Enough rambling... to use the code, just instantiate a new
> RequestManager (or whatever you call the class) and use it as follows:
>
> PRIVATE myRequestManager AS NEW RequestManager
>
> ...
> DIM allData AS String
> allData =
> myRequestManager.getFileAsString("192.168.1.2//var/share/dvb2001/services.dat", "root", "password")
>
>
> Now here follows (some of) the code for the RequestManager class itself:
>
> ' Gambas class file
> PRIVATE theFTPClient AS NEW FtpClient
> PRIVATE eggTimer AS NEW Timer
> PRIVATE timedOut AS Boolean
> PRIVATE busy AS Boolean
> PRIVATE allStates AS Collection
>
> PRIVATE SUB getFileByFTP(url AS String, userName AS String, password AS
> String, localFile AS String)
>
> DIM fileName AS String
>
> theFTPClient.URL = url
> theFTPClient.User = userName
> theFTPClient.Password = password
> secondsBeforeTimeout(10)
> theFTPClient.get(localFile)
> DO UNTIL theFTPClient.Status <= 0 OR timedOut = TRUE
> Logger.logMessage("Requ Logger.logMessage("getFileByFTPAsString: " & Error.Text & " @ " &
> Error.Where, TRUE)
> estManager.getFileByFTP() status: " &
> theFTPClient.Status, FALSE)
> WAIT 0.01
> LOOP
> IF theFTPClient.Status <> 0 THEN
> message("Error in FTP call! Check password?")
> END IF
> busy = FALSE
> CATCH
> Logger.logMessage("getFileByFTP: " & Error.Text & " @ " & Error.Where,
> TRUE)
> END
>
> PUBLIC FUNCTION getFileAsString(url AS String, userName AS String,
> password AS String, tmpDir AS String) AS String
>
> DIM result AS String
> DIM fileName AS String
> DIM hFile AS File
> DIM aLine AS String
> fileName = tmpDir &/ "drag_cont_tmp_" & userName & ".tmp"
> Logger.logMessage("RequestManager.getFileAsString() fileName: " &
> fileName, FALSE)
> getFileByFTP(url, userName, password, fileName)
> result = ""
> OPEN fileName FOR READ AS #hFile
> WHILE NOT Eof(hFile)
> LINE INPUT #hFile, aLine
> IF aLine <> NULL THEN
> result = result & aLine & gb.NewLine
> END IF
> WEND
> CLOSE #hFile
> RETURN result
> CATCH
> Logger.logMessage("getFileByFTPAsString: " & Error.Text & " @ " &
> Error.Where, TRUE)
> END
>
>
> PUBLIC SUB eggTimer_Timer()
>
> eggTimer.Enabled = FALSE
> timedOut = TRUE
> Object.Detach(eggTimer)
>
> END
>
> PUBLIC FUNCTION isBusy() AS Boolean
>
> Logger.logMessage("RequestManager checked for busy state and was: " &
> booleanToString(busy), FALSE)
> RETURN busy
>
> END
>
> PRIVATE SUB secondsBeforeTimeout(seconds AS Integer)
>
> Object.Attach(eggTimer, ME, "eggTimer")
> timedOut = FALSE
> eggTimer.Delay = seconds * 1000
> busy = TRUE
> eggTimer.Enabled = TRUE
>
> END
>
> PRIVATE FUNCTION booleanToString(value AS Boolean) AS String
>
> IF value THEN
> RETURN "TRUE"
> ELSE
> RETURN "FALSE"
> END IF
>
> END
>
> PRIVATE FUNCTION getState(aState AS Integer) AS String
>
> RETURN getAllStates()[aState]
>
> END
>
> PRIVATE FUNCTION getAllStates() AS Collection
>
> IF allStates = NULL THEN
> allStates = NEW Collection
> allStates.Add("Accepting", Net.Accepting)
> allStates.Add("Active", Net.Active)
> allStates.Add("CannotBindSocket", Net.CannotBindSocket)
> allStates.Add("CannotCreateSocket", Net.CannotCreateSocket)
> allStates.Add("CannotListen", Net.CannotListen)
> allStates.Add("CannotRead", Net.CannotRead)
> allStates.Add("CannotWrite", Net.CannotWrite)
> allStates.Add("Connected", Net.Connected)
> allStates.Add("Connecting", Net.Connecting)
> allStates.Add("ConnectionRefused", Net.ConnectionRefused)
> allStates.Add("HostNotFound", Net.HostNotFound)
> allStates.Add("Inactive", Net.Inactive)
> allStates.Add("Internet", Net.Internet)
> allStates.Add("IPv4", Net.IPv4)
> allStates.Add("Local", Net.Local)
> allStates.Add("Pending", Net.Pending)
> allStates.Add("ReceivingData", Net.ReceivingData)
> allStates.Add("Searching", Net.Searching)
> allStates.Add("Unix", Net.Unix)
> END IF
> RETURN allStates
>
> END
>
> On Thu, 2009-09-03 at 14:24 +0200, Lars Hoeger wrote:
> > Hello,
> >
> > I would like to use the ftpclient component, but there is no documentation. Has anyone some code-examples?
> >
> > Thanks,
> > Lars
> > gambas 2.15
> >
> >
> > ------------------------------------------------------------------------------
> > Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> > trial. Simplify your report design, integration and deployment - and focus on
> > what you do best, core application coding. Discover what's new with
> > Crystal Reports now. http://p.sf.net/sfu/bobj-july
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now. http://p.sf.net/sfu/bobj-july
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
More information about the User
mailing list