Skip to content

FTP Uploader (.NET vs Python)

Michele Bersani showed a very simple example of FTP uploader using a third party library (in .NET 1.1 you don’t have any FTP support at all) and hence I wrote a Python version.

Here is his script: [code lang="java"] using System; using System.IO; using EnterpriseDT.Net.Ftp;

namespace SimpleFTP {
class Simple { [STAThread] static void Main(string[] args) { try { FTPClient ftp = new FTPClient(); // Host ftp.RemoteHost = "Host"; // Connect ftp.Connect(); // Login ftp.Login("User", "Password"); // Set up connect and transfer modes ftp.ConnectMode = FTPConnectMode.ACTIVE; ftp.TransferType = FTPTransferType.ASCII; // Put file to remote server ftp.Put("local_file", "remote_file"); // Close connection ftp.Quit(); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); } } } [/code]

and this is mine (1 to 1 example, don’t bother with proper error handling):

[code lang="python"] import os from ftplib import FTP

try: # login to the remote host ftp_client = FTP("host") ftp_client.login("username", "password")

# change dir
ftp_client.cwd("remotepath")

# open the local file in ASCII mode
f = open("localfile", "r")

# upload file in lines mode
ftp_client.storlines('STOR %s' % os.path.basename("localfile"), f)
f.close()

# disconnect
ftp_client.quit()

except e: print e [/code]

In .NET 2.0 (finally) they put some FTP support in: System.Net.FtpWebRequest

One Trackback/Pingback

  1. PDI^2 on Saturday, December 10, 2005 at 2:36 pm

    E’ venerdi sera e io sono un geek del cavolo

    .. la verità è che mi sento uno straccio, e domattina mi devo svegliare presto, quindi mi son rifiutato din andare al concerto di una cover band dei pink floyd. 
    Colgo l’occasione per giocare un po’ con rhymes, perché sono io che l&#82...
    

Additional comments powered by BackType