Trouble with ftplib uploading to an FTP server

MRAB python at mrabarnett.plus.com
Wed Feb 24 21:56:06 CET 2010


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://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)
> 
> *****************************************************************************
> 




More information about the Python-list mailing list