Trouble with ftplib uploading to an FTP server

S

Sky Larking

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://whatismyip.org').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)

*****************************************************************************
 
M

MRAB

Sky said:
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://whatismyip.org').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

Sky Larking

Sky said:
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://whatismyip.org').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...
 
D

Dennis Lee Bieber

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

Sky Larking

        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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top