ftplib only posting 52 bytes?
Brent Stroh
bmstroh at cavtel.net
Wed Jun 13 23:49:01 EDT 2001
I've put together a small script to resize digital pictures, rename them,
and upload them to a web server. It also generates a portion of the HTML
necessary to incorporate them into a web page.
This script is installed as a right-click option under WinME. I can right
click a JPG file, and 'Webify' the picture. The resizing (via PIL) works
great; the HTML is generated like it should be.
Today, I added the FTP step, figuring it would save me a few minutes to
kick the transfer off in the background. It appears the first 52 bytes of
each image is transferred to the server; what is sent appears to be the
beginning of a JPG file.
As this is my first useful piece of Python, I'm interested in how to
improve it overall, not just in correcting the FTP section.
(This is ActiveState Python 2.1, build 210, using PIL 1.11 for Python 2.1a,
on WinME.)
I'd appreciate CC's of any usenet response; my news provider has been
having issues with missing posts for a week or so.
#!/usr/bin/python
import os, re, string, sys
import Image
from ftplib import FTP
def write_html(filename, height, width):
widtht = repr(int(width))
heightt = repr(int(height))
fp = open(dest + "../list.html", "a")
fp.write("<IMG SRC=\"images/" + filename + "\" HEIGHT=\"" + heightt +"\"
WIDTH=\"" + widtht + "\"><BR>\n")
fp.write(montht + " " + dayt + ", " + yeart + "<P>\n\n")
fp.close
return
def send_file(filename):
ftp = FTP('server', 'user', 'pass')
ftp.set_pasv(0)
ftp.cwd('/home/bmstroh/public_html/images')
print dest + filename
fp = open(dest + filename)
ftp.storbinary('STOR %s' % filename, fp, 1024 * 16)
ftp.quit()
return
dest = "c:/taylor/web/images/"
# dest = "d:/cd-rom/personal/taylor/web/images/"
thisdir = os.path.basename(os.getcwd())
months = { 'January': '01', 'February': '02', 'March': '03', 'April':
'04', \
'May': '05', 'June': '06', 'July': '07', 'August': '08', \
'September': '09', 'October': '10', 'November': '11', \
'December': '12' }
pattern = re.compile("([A-Z]{1,1}[a-z]*) ([0-9]{2,2}), ([0-9]{4,4})")
result = pattern.match(thisdir)
if result == None:
print "Hmm. No match"
sys.stdin.readline()
sys.exit()
month = result.group(1)
day = result.group(2)
year = result.group(3)
montht = month
dayt = repr(string.atoi(day))
yeart = year
month = months[ month ]
year = year[-2] + year[-1]
prefix = year + month + day + "_"
for infile in sys.argv[1:]:
infile = os.path.basename(infile)
infile = string.upper(infile)
pattern = re.compile("^([0-9]{2,2})$")
result = pattern.match(infile)
if result == None:
pass
else:
infile = "IMAGE" + infile + ".JPG"
pattern = re.compile("^IMAGE([0-9]{2,2})\.JPG")
result = pattern.match(infile)
if result == None:
print "Hmm - can't match either expression"
sys.stdin.readline()
sys.exit()
filename = prefix + result.group(1) + ".jpg"
outfile = dest + filename
if infile != outfile:
try:
im = Image.open(infile)
(width, height) = im.size
if width > height:
scale = width / 320.0
else:
scale = height / 320.0
width = width / scale
height = height / scale
im.thumbnail((width, height))
im.save(outfile, "JPEG")
write_html(filename, height, width)
send_file(filename)
except IOError:
print "cannot create thumbnail for", infile
pause = sys.stdin.readline()
More information about the Python-list
mailing list