Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2925 > unrolled thread

Cool FTP implementation with "minimal" Python

Started by"McHale, Paul" <Paul.McHale@excelitas.com>
First post2011-06-04 10:19 +0000
Last post2011-06-05 11:02 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Cool FTP implementation with "minimal" Python "McHale, Paul" <Paul.McHale@excelitas.com> - 2011-06-04 10:19 +0000
    Re: Cool FTP implementation with "minimal" Python David Bailey <dave@removedbailey.co.uk> - 2011-06-05 11:02 +0000

#2925 — Cool FTP implementation with "minimal" Python

From"McHale, Paul" <Paul.McHale@excelitas.com>
Date2011-06-04 10:19 +0000
SubjectCool FTP implementation with "minimal" Python
Message-ID<isd0rh$1mb$1@smc.vnet.net>
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@Excelitas.com<mailto:Paul.McHale@perkinelmer.com>
www.excelitas.com<http://www.excelitas.com>

[cid:image001.png@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

[toc] | [next] | [standalone]


#2943

FromDavid Bailey <dave@removedbailey.co.uk>
Date2011-06-05 11:02 +0000
Message-ID<isfnnl$bq6$1@smc.vnet.net>
In reply to#2925
On 04/06/2011 11:19, McHale, Paul wrote:
> 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]
> ----------------------------------------------------------------------

>
> 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.

I can't quite understand why you would want to extend Mathematica in 
this particular way, unless perhaps you had not realised that the 
Mathematica Fourier command uses the FFT algorithm if the data length is 
a power of two.

More generally, most of my extensions happen via J/Link and Java, which 
gives a very clean interface. It is also possible in 8.0 to call C code 
in a DLL directly (i.e. with no inter-process overhead) and although I 
have not made use of this yet, this feature obviously has interesting 
possibilities.

David Bailey
http://www.dbaileyconsultancy.co.uk

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web