inicio mail me! sindicaci;ón

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:

  1. using System;
  2. using System.IO;
  3. using EnterpriseDT.Net.Ftp;
  4.  
  5. namespace SimpleFTP
  6. {   
  7.     class Simple
  8.     {
  9.         [STAThread]
  10.         static void Main(string[] args)
  11.         {
  12.             try
  13.             {
  14.                 FTPClient ftp = new FTPClient();
  15.                 // Host
  16.                 ftp.RemoteHost = “Host”;
  17.                 // Connect
  18.                 ftp.Connect();
  19.                 // Login
  20.                 ftp.Login(“User”, “Password”);
  21.                 // Set up connect and transfer modes
  22.                 ftp.ConnectMode = FTPConnectMode.ACTIVE;
  23.                 ftp.TransferType = FTPTransferType.ASCII;
  24.                 // Put file to remote server
  25.                 ftp.Put(“local_file”, “remote_file”);
  26.                 // Close connection
  27.                 ftp.Quit();
  28.             }
  29.             catch (Exception e)
  30.             {
  31.                 Console.WriteLine(e.Message);
  32.             }
  33.             Console.ReadLine();
  34.         }
  35.     }
  36. }

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

  1. import os
  2. from ftplib import FTP
  3.  
  4. try:
  5.     # login to the remote host
  6.     ftp_client = FTP(“host”)
  7.     ftp_client.login(“username”, “password”)
  8.  
  9.     # change dir
  10.     ftp_client.cwd(“remotepath”)
  11.  
  12.     # open the local file in ASCII mode
  13.     f = open(“localfile”, “r”)
  14.  
  15.     # upload file in lines mode
  16.     ftp_client.storlines(‘STOR %s’ % os.path.basename(“localfile”), f)
  17.     f.close()
  18.  
  19.     # disconnect
  20.     ftp_client.quit()
  21. except e:
  22.     print e

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

Related posts

  • Updates from Python SVN, Part 9
  • Updates from Python SVN, part 4
  • Implementing Python with .NET
  • Thinking about the future (and mine too)
  • Ready to roll on the summer of code
  • Gravatar

    PDI^2 said,

    December 10, 2005 @ 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…

    RSS feed for comments on this post · TrackBack URI

    Leave a Comment