Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
FTP File streaming Posted by chaosowns on 4 Feb 2009 at 9:59 AM
Hello,

I have been asked to try to program a ftp file stream in python. Here's the complete story:

There's a FTP server with a comma seperated file (csv) on it. A device writes log information in to the csv file whenever something log-worthy occures. This csv file is around 22 MB and keeps on growing. I want to read the latest csv entry that's gotten in to the file. So my question is:

Is it possible to connect to the FTP server, open up a stream to the csv file and retrieve the latest data? I have to able to get a constant stream and always get the latest csv entry.

If this is possible wont there be a conflict when the device is writing to the .csv file and i'm reading from it?

If this isn't possible in python, do you have any idea if it is in another programming language?

Hope i explained everything well enough.

Thanks in advance.
Report
Re: FTP File streaming Posted by iblis on 27 Mar 2009 at 6:58 PM
: Hello,
:
: I have been asked to try to program a ftp file stream in python.
: Here's the complete story:
:
: There's a FTP server with a comma seperated file (csv) on it. A
: device writes log information in to the csv file whenever something
: log-worthy occures. This csv file is around 22 MB and keeps on
: growing. I want to read the latest csv entry that's gotten in to the
: file. So my question is:
:
: Is it possible to connect to the FTP server, open up a stream to the
: csv file and retrieve the latest data? I have to able to get a
: constant stream and always get the latest csv entry.
:
: If this is possible wont there be a conflict when the device is
: writing to the .csv file and i'm reading from it?
:
: If this isn't possible in python, do you have any idea if it is in
: another programming language?
:
: Hope i explained everything well enough.
:
: Thanks in advance.
:

Should be possible. Quick question, though: Do you, or perhaps the person asking for this program, have control over the process that creates this csv?

If so, you and/or they could always change the process to create an empty filename.csv.lock file while it's writing, and remove it when it's finished; then code your client to respect it (i.e., not download the file) if it finds it.

I don't have any example code immediately available for parsing csv's, though there's a nice csv module including with python called, amazingly enough, "csv". :D

On most systems a 'pydoc csv' will give you oodles of information on the module, or you can just do
import csv;help(csv)
from an interpreter.

But I do use something like this to pull down data over my lan - since I have gigabit and the files aren't big I don't worry about pulling down the whole thing each time, but if that's not the case for you you'll likely want to look at resuming to save bandwidth.

from ftplib import FTP
import sys

ftp = FTP('hostname')
ftp.login('username', 'password')

filename = 'filename.dat'

ls = ftp.retrlines('LIST %s' % filename).split('\n')[-1]

if ls.startswith('226 0'):
    sys.exit(1)

ls = ftp.retrlines('LIST %s.lock' % filename).split('\n')[-1]

if not ls.startswith('226 0'):
    ftp.quit()
    sys.exit(1)
else:
    ftp.retrbinary('RETR %s' % filename, open(filename , 'wb').write)
    ftp.quit()
    sys.exit(0)



Hope this helps.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.