Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Cool FTP implementation with "minimal" Python

  • To: mathgroup at smc.vnet.net
  • Subject: [mg119441] Cool FTP implementation with "minimal" Python
  • From: "McHale, Paul" <Paul.McHale at excelitas.com>
  • Date: Sat, 4 Jun 2011 06:19:31 -0400 (EDT)

I enjoy using Mathematica, but as with any language they can't do everything.  Nor should they.  Here is a way I implemented quick FTP get routine.

Mathematica code:
Note:  All communication to python program is through command line parameters.
Returns {Success} or {Failure}
----------------------------------------------------------------------

SetDirectory[NotebookDirectory[]];
mFileList=ReadList["!c:\\python27\\python.exe " <> NotebookDirectory[] <> "FtpPutFile.py 192.168.192.201 /Test/Coms Test.bin",Record]
----------------------------------------------------------------------


Python code:  (I'm sure there are prettier implementations, but maybe not more simple :) )
NOTE:  All text from Print statements are returned to Mathematica as a list returned from ReadList[] call in Mathematica code.
----------------------------------------------------------------------
from socket import *
from array import *
from struct import *
from string import *
from ftplib import FTP
import sys

def handleDownload(block):
    file.write(block)
#    print ".",

def mFtpPutFile(mHOST,mDirectory, mDirectory, mFileName):
    # This will handle the data being Uploaded

    try:
        #print "Opening connection"
        ftp = FTP(sys.argv[1])   # connect to host, default port
        ftp.login()               # user anonymous, passwd anonymous@
        #print "Changing directory"
        ftp.cwd(mDirectory)

        #print 'Opening local file ' + filename
        file = open(mFileName, 'rb')

        #print 'Storing ' + filename
        ftp.storbinary('STOR ' + mFileName, file)

        # Clean up time
        file.close()

        #print 'Closing FTP connection'
        ftp.close()
    except:
        print 'Failure'

    else:
        print 'Success'

mHOST = sys.argv[1]
mDirectory = sys.argv[2]
mFileName = sys.argv[3]
#mHOST='192.168.192.201'   This is how to test the code without requiring the code to be called from command line
mFtpPutFile(mHOST,mDirectory,mFileName)
----------------------------------------------------------------------

Anyone else have cool and easy ways to extend Mathematica (Stay in Mathematica longer :) )?  So far Python called this way has been a great way to accomplish some interesting things including sending and receiving small Ethernet commands.

Paul




Paul McHale  |  Electrical Engineer, Energetics Systems  |  Excelitas Technologies Corp.

Phone:   +1 937.865.3004   |   Fax:  +1 937.865.5170   |   Mobile:   +1 937.371.2828
1100 Vanguard Blvd, Miamisburg, Ohio 45342-0312 USA
Paul.McHale at Excelitas.com<mailto:Paul.McHale at perkinelmer.com>
www.excelitas.com<http://www.excelitas.com>

[cid:image001.png at 01CB9136.E3D96D90]

Please consider the environment before printing this e-mail.
This email message and any attachments are confidential and proprietary to Excelitas Technologies Corp. If you are not the intended recipient of this message, please inform the sender by replying to this email or sending a message to the sender and destroy the message and any attachments.
Thank you


  • Prev by Date: Re: ContourPlot ColorFunction Question
  • Next by Date: Re: ContourPlot ColorFunction Question
  • Previous by thread: Running P&L
  • Next by thread: Re: Cool FTP implementation with "minimal" Python