[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Trouble with ftplib uploading to an FTP server

Sky Larking

2/24/2010 8:16:00 PM

This bit of code is designed to get the external IP address and
hostname of a client , write it locally to a file, then upload to an
FTP server. It works locally( one can see the IP number and hostname
with a time stamp in /Users/admin/Documents) , but when the file is
opened on the server after it's FTP'ed , it's blank....I think my
issue is something with the way I am utilizing the ftplib commands...
I am pretty sure instead of FTPing the file, it's just creating a new
one with the same name and uploading that?

This script is running on some OS X 10.5 clients and reporting back to
an Ubuntu 8.04 server...I am using the version of python bundled with
10.5.x ( 2.5.1 )

****************************************************************
import os
import datetime
import ftplib
import urllib2


currdate = datetime.datetime.now()
formatdate = currdate.strftime("%m-%d-%Y %H%M")

def log():

fqn = os.uname()[1]
ext_ip = urllib2.urlopen('http://whatismyi...).read()
log = open ('/Users/admin/Documents/locatelog.txt','w')
log.write(str("Asset: %s " % fqn))
log.write(str("Checking in from IP#: %s" % ext_ip))
smush = str(fqn +' @ ' + formatdate)
os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/
Documents/%s.txt' % smush )
s = ftplib.FTP('10.7.1.71','username','password')
fd = open('/Users/admin/Documents/%s.txt' % smush,'rb')
s.storbinary("STOR %s.txt" % smush , fd)

*****************************************************************************

4 Answers

MRAB

2/24/2010 8:56:00 PM

0

Sky Larking wrote:
> This bit of code is designed to get the external IP address and
> hostname of a client , write it locally to a file, then upload to an
> FTP server. It works locally( one can see the IP number and hostname
> with a time stamp in /Users/admin/Documents) , but when the file is
> opened on the server after it's FTP'ed , it's blank....I think my
> issue is something with the way I am utilizing the ftplib commands...
> I am pretty sure instead of FTPing the file, it's just creating a new
> one with the same name and uploading that?
>
> This script is running on some OS X 10.5 clients and reporting back to
> an Ubuntu 8.04 server...I am using the version of python bundled with
> 10.5.x ( 2.5.1 )
>
> ****************************************************************
> import os
> import datetime
> import ftplib
> import urllib2
>
>
> currdate = datetime.datetime.now()
> formatdate = currdate.strftime("%m-%d-%Y %H%M")
>
> def log():
>
> fqn = os.uname()[1]
> ext_ip = urllib2.urlopen('http://whatismyi...).read()
> log = open ('/Users/admin/Documents/locatelog.txt','w')
> log.write(str("Asset: %s " % fqn))
> log.write(str("Checking in from IP#: %s" % ext_ip))
> smush = str(fqn +' @ ' + formatdate)
> os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/
> Documents/%s.txt' % smush )

You're renaming the file while it's still open. What you've written is
still in the buffer, not on disk.

> s = ftplib.FTP('10.7.1.71','username','password')
> fd = open('/Users/admin/Documents/%s.txt' % smush,'rb')
> s.storbinary("STOR %s.txt" % smush , fd)
>
> *****************************************************************************
>

Sky Larking

2/25/2010 1:22:00 AM

0

On Feb 24, 3:56 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:
> Sky Larking wrote:
> > This bit of code is designed to get the external IP address and
> > hostname of a client , write it locally to a file, then upload to an
> > FTP server. It works locally( one can see the IP number and hostname
> > with a time stamp in /Users/admin/Documents) , but when the file is
> > opened on the server after it's FTP'ed , it's blank....I think my
> > issue is something with the way I am utilizing the ftplib commands...
> > I am pretty sure instead of FTPing the file, it's just creating a new
> > one with the same name and uploading that?
>
> > This script is running on some OS X 10.5 clients and reporting back to
> > an Ubuntu 8.04 server...I am using the version of python bundled with
> > 10.5.x ( 2.5.1 )
>
> > ****************************************************************
> > import os
> > import datetime
> > import ftplib
> > import urllib2
>
> > currdate = datetime.datetime.now()
> > formatdate = currdate.strftime("%m-%d-%Y %H%M")
>
> > def log():
>
> >     fqn = os.uname()[1]
> >     ext_ip = urllib2.urlopen('http://whatismyi...).read()
> >     log = open ('/Users/admin/Documents/locatelog.txt','w')
> >     log.write(str("Asset: %s " % fqn))
> >     log.write(str("Checking in from IP#: %s" % ext_ip))
> >     smush = str(fqn +' @ ' + formatdate)
> >     os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/
> > Documents/%s.txt' %  smush )
>
> You're renaming the file while it's still open. What you've written is
> still in the buffer, not on disk.
>
> >     s = ftplib.FTP('10.7.1.71','username','password')
> >     fd = open('/Users/admin/Documents/%s.txt' % smush,'rb')
> >     s.storbinary("STOR %s.txt" % smush , fd)
>
> > *****************************************************************************
>
>

@ MRAB Thanks ...

I can see the file in /Users/admin/Documents after the following line
runs though?

os.rename('/Users/admin/Documents/locatelog.txt','/Users/admin/
Documents/%s.txt' % smush )

For instance I just ran the script and os.rename() renamed it to:

TestMachine.local @ 02-24-2010 2020.txt

in that .txt file it reads:

Asset: TestMachine.local Checking in from IP#: xx.xxx.xx.xxx

This is what I want, but the FTP piece doesn't seem to work...

Dennis Lee Bieber

2/25/2010 6:10:00 AM

0

On Wed, 24 Feb 2010 17:21:58 -0800 (PST), Sky Larking
<skylarking11@gmail.com> declaimed the following in
gmane.comp.python.general:

> For instance I just ran the script and os.rename() renamed it to:
>
> TestMachine.local @ 02-24-2010 2020.txt
>
> in that .txt file it reads:
>
> Asset: TestMachine.local Checking in from IP#: xx.xxx.xx.xxx
>
> This is what I want, but the FTP piece doesn't seem to work...

But since you haven't CLOSED the file before invoking the FTP
operation, the contents are probably in a write buffer that hasn't been
flushed. Renaming a file only changes the directory entry -- doesn't do
anything for what may or may not be in the file.

After the program exits, the file get closed, which means the
buffers are flushed.
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Sky Larking

2/25/2010 4:34:00 PM

0

On Feb 25, 1:10 am, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Wed, 24 Feb 2010 17:21:58 -0800 (PST), Sky Larking
> <skylarkin...@gmail.com> declaimed the following in
> gmane.comp.python.general:
>
> > For instance I just ran the script and os.rename() renamed it to:
>
> > TestMachine.local @ 02-24-2010 2020.txt
>
> > in that .txt file it reads:
>
> > Asset: TestMachine.local Checking in from IP#: xx.xxx.xx.xxx
>
> > This is what I want, but the FTP piece doesn't seem to work...
>
>         But since you haven't CLOSED the file before invoking the FTP
> operation, the contents are probably in a write buffer that hasn't been
> flushed. Renaming a file only changes the directory entry -- doesn't do
> anything for what may or may not be in the file.
>
>         After the program exits, the file get closed, which means the
> buffers are flushed.
> --
>         Wulfraed         Dennis Lee Bieber               KD6MOG
>         wlfr...@ix.netcom.com      HTTP://wlfraed.home.netcom.com/

I see my mistake now, I closed the file ...then did the upload ... and
the data was there --
thanks for the help