Trouble with ftplib uploading to an FTP server
Sky Larking
skylarking11 at gmail.com
Wed Feb 24 20:21:58 EST 2010
On Feb 24, 3:56 pm, MRAB <pyt... at 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://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...
More information about the Python-list
mailing list