[INC] FTP

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1090
Gender:
United States of America

[INC] FTP

Post by Archive-Poster »

Image

.: Description

This include provides the ability to upload files, download files, and retrieve a directory-list of files from a FTP server. Using my ISP provided FTP file server (Comcast, cable) and a 6.42mb file, I was able to download in 18 seconds and upload in 35 seconds. Currently, only one transaction can take place at a time. There is currently no error trapping for remote-side errors, make sure directories are correct/accessible and server-name\port\login\password are accurate, etc. The previous version of this include used a new connection for each file transaction. The new version uses one connection for multiple transactions. There is no 'keep-alive' functionality in place so do all transactions shortly after opening a connection.

.: Commands
FTP_Open - Establish connection and login to a FTP server.
FTP_Close - Log-out and close connection to FTP server.
FTP_Ready - Returns FTP status; true if ready for a transaction.
FTP_SendFile - Transfer a file to FTP server.
FTP_GetFile - Retrieve a file from FTP server.
FTP_GetList - Retrieve directory contents from FTP server.

.: Command Usage
FTP_Open( szServer[] , iPort , szUser[] , szPassword[] , szForward[] )
szServer - FTP server address.
iPort - FTP server port.
szUser - Username to login with.
szPassword - Password for login.
szForward - Forward function for monitoring status of login attempt. The forward takes 1 param which is a bool to indicate whether or not the login attempt was successful.
Forward( bool:bLoginSuccessful )

FTP_Close()

FTP_Ready()

FTP_SendFile( szLocalFile[] , szRemoteFile[] , szForward[] )
szLocalFile - Local file to be transferred to FTP server.
szRemoteFile - Remote file for storing transferred file.
szForward - Forward function for monitoring status of file transfer. The forward takes 3 params; first is file name being transferred, second is bytes transferred, third is total bytes.
Forward( szFile[] , iBytesTransferred , iTotalBytes )

FTP_GetFile( szLocalFile[] , szRemoteFile[] , szForward[] )
szLocalFile - Local file to save file that was retrieved from FTP server.
szRemoteFile - Remote file to retrieve from FTP server.
szForward - Forward function for monitoring status of file transfer. The forward takes 3 params; first is file name being transferred, second is bytes transferred, third is total bytes.
Forward( szFile[] , iBytesTransferred , iTotalBytes )

FTP_GetList( szLocalFile[] , szRemoteDirectory[] , szForward[] )
szLocalFile - Local file to save directory list.
szRemoteFile - Remote directory to retrieve a list of files from.
szForward - Forward function for monitoring status of list retrieval. The forward takes 2 params; first is file name of the local file list, second is bytes retrieved.
Forward( szFile[] , iBytesRetrieved )

.: Requirements
sockets
engine

.: Example plugin
The test server provided will work for all commands except ftp_send.

Code: Select all

/*    
    FTP Test Plugin
         v0.1
       by bugsy
    
    http://forums.alliedmods.net/showthread.php?t=142850
*/

#include <amxmodx>
#include <ftp>

new FTP_Server[] = "ftp.secureftp-test.com";
new FTP_Port = 21;
new FTP_User[] = "test";
new FTP_Pass[] = "test";
new FTP_LocalFile[] = "hamlet.zip"
new FTP_RemoteFile[] = "hamlet.zip";
new FTP_ListFile[] = "DirList.txt";
new FTP_RemoteDir[] = "//";

new g_iStartTime;

public plugin_init() 
{
    register_plugin( "FTP Test" , "0.1" , "bugsy" );
    
    register_concmd( "ftp_open" , "Open" );
    register_concmd( "ftp_close" , "Close" );
    register_concmd( "ftp_send" , "Send" );
    register_concmd( "ftp_get" , "Get" );
    register_concmd( "ftp_list" , "List" );
}

//Commands
public Open()
{
    FTP_Open( FTP_Server , FTP_Port , FTP_User , FTP_Pass , "FwdFuncOpen" );
}

public Close()
{
    FTP_Close();
}

public Send()
{
    if ( FTP_Ready() )
    {
        FTP_SendFile( FTP_LocalFile , FTP_RemoteFile , "FwdFuncTransfer" );    
        g_iStartTime = get_systime();
    }
}

public Get()
{
    if ( FTP_Ready() )
    {
        FTP_GetFile( FTP_LocalFile , FTP_RemoteFile , "FwdFuncTransfer" );    
        g_iStartTime = get_systime();
    }
}

public List()
{
    if ( FTP_Ready() )
    {
        FTP_GetList( FTP_ListFile , FTP_RemoteDir , "FwdFuncList" );
    }
}

//Forward functions
public FwdFuncOpen( bool:bLoggedIn )
{
    server_print( "Login was %ssuccessful!" , bLoggedIn ? "" : "un" );
}

public FwdFuncTransfer( szFile[] , iBytesComplete , iTotalBytes )
{
    server_print( "[%.1f%%] [%s] [ %d of %d bytes ][ %dkB/s ]" , ( floatdiv( float( iBytesComplete ) , float( iTotalBytes ) ) * 100.0 ) , 
                                    szFile , 
                                    iBytesComplete ,
                                    iTotalBytes , 
                                    ( ( iBytesComplete ) / 1000 ) / ( get_systime() - g_iStartTime ) );
                                    
    
    if ( iBytesComplete == iTotalBytes )
        server_print( "File transfer completed in %d seconds!" , get_systime() - g_iStartTime );
}

public FwdFuncList( szFile[] , iBytesComplete )
{
    server_print( "[%s] [ %d bytes ]" , szFile , iBytesComplete );
} 
Attachments
ftp.inc.zip
(2.95 KiB) Downloaded 4 times
ftp_test.sma.zip
(947 Bytes) Downloaded 6 times

Who is online

Users browsing this forum: No registered users and 0 guests