quick search:

FTPing OUT of Zope

Submitted by: runyaga
Last Edited: 2001-10-12

Category: Python(External Method)

Average rating is: 4.5 out of 5 (2 ratings)

sometimes you have information you want to push from ZOPE to another machine. LocalFS, ExtFile, and plethora of files help if you want to push them to a local filesystem, but to do it remotely takes a quick External Method to wrap the data into a File object and send it on its way.

Source (Text):
#create file on filesystem in $ZOPE/Extensions called ftpme.py
import ftplib
from string import split
from StringIO import StringIO


def ftp(obj, host=host, login=login, password=password, port=port):
    _ftp = ftplib.FTP()
    _ftp.connect(host, port)
    _ftp.login(login, password)

    if lower(obj.meta_type)=='file':
        if obj.content_type[:4]=='text': binary=0
        ftp_file(_ftp, obj, binary)

    if lower(obj.meta_type)=='image':
        ftp_file(_ftp, obj, 1)


def ftp_file(ftpCon, fileObj, binary):
    if binary:
        ftpCon.storbinary("STOR " + fileObj.getId(), StringIO(fileObj.data), 1024)
        f = StringIO()
        f.readlines(split(fileObj.data, '\n'))
        ftpCon.storlines("STOR " + fileObj.getId(), f)

#create a External Method in ZOPE, called ftp_out
#id: ftp_out, module name: ftpme, function: ftp

#now to use this external method from a Script(Python) its very simple
#create a Script (Python) called ftp_doer

file=getattr(context, 'index.html')

#in DTML
<dtml-call "ftp_out(_.getitem('index.html'))">

#or if you wanted to pass host/login info you can from script
#to port 8021
context.ftp_out(file, 'ftp.python.org', 'anonymous', 'bob@slack.net', 8021)

the ftp function is responsible for figuring out what kinda object your sending it.
if its a file or image object it will send the obj to the ftp_file

you could easily make one for dtml and just make another function
called ftp_dtml() where you would wrap obj.raw into a StringIO object and send it
to appropriate place.

this gives you an example to work off of to FTP to another site and
upload a file contents thats stored in the ZODB


No Comments