1,105,644 Community Members

ftplib and the retrbinary() command

Member Avatar
-=disAbled=-
Newbie Poster
2 posts since Jun 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm trying to finish up my first Python program. The program was built to help me maintain and add content to my webpage (it's nothing fancy...just pure html). Everything was smooth sailing (hey, python kicks some serious butt), until I ran into problems using retrbinary() to download files. The problem lies in the FTPDown() function of my program. When I try to execute the funcition i get a fun error message:

Username and Password successfully verified
220 FTP Server ready

Downloading last.htm
Traceback (most recent call last):
  File "HTMLFixer.py", line 303, in ?
    MainMenu()
  File "HTMLFixer.py", line 212, in MainMenu
    FTPDown()
  File "HTMLFixer.py", line 46, in FTPDown
    myftp.storlines('RETR '+filename,fileOut.write)
  File "C:\Documents and Settings\tbeck\My Documents\tools\pt\lib\ftplib.py", line 428, in storlines
    buf = fp.readline()
AttributeError: 'builtin_function_or_method' object has no attribute 'readline'

I've been hunting around, and everything seems to indicate that there should be nothing wrong with my code. Obviously, though, something is.

I'm running Python 2.4.1 on Win2k.

Could someone explain what I'm doing wrong here? Oh, and also, i'd like to know the os command for clear screen in windows. Seems like 'cls' should work somewhere, but....

code follows:

from ftplib import FTP
import os
import re
import sys
import fnmatch

def FTPDown():
	print
	print
	ftplocation=raw_input("Please Enter the FTP location:  ")
	print "Connecting to "+ftplocation
	try:
		myftp=FTP(ftplocation)
	except:
		print "Could not connect to "+ftplocation
		raw_input("--Press any key to return to main menu--")
		ClearScreen()
		return
	print
	print "Connected to "+ftplocation
	myftp.set_debuglevel(0)
	username=raw_input("Please Enter Username:  ")
	password=raw_input("Please Enter Password:  ")
	print
	print
	try:
		myftp.login(username,password)
	except:
		print "Login Failed: Username or Password is incorrect"
		myftp.close()
		raw_input("--Press any key to return to main menu--")
		ClearScreen()
		return
	ClearScreen()
	print "Username and Password successfully verified"
	print myftp.getwelcome()
	print
	fileList = myftp.nlst() 
	targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
	for filename in targetList:
		print "Downloading "+filename
		try:
			fileOut=open(filename,'wb')
		except:
			print "couldn't open the output file"
		myftp.storlines('RETR '+filename,fileOut.write)
		fileOut.close()
	myftp.close()

	

	
def FTPUp():
	print
	print
	ftplocation=raw_input("Please Enter the FTP location:  ")
	print "Connecting to "+ftplocation
	try:
		myftp=FTP(ftplocation)
	except:
		print "Could not connect to "+ftplocation
		raw_input("--Press any key to return to main menu--")
		ClearScreen()
		return
	print
	print "Connected to "+ftplocation
	myftp.set_debuglevel(0)
	username=raw_input("Please Enter Username:  ")
	password=raw_input("Please Enter Password:  ")
	print
	print
	try:
		myftp.login(username,password)
	except:
		print "Login Failed: Username or Password is incorrect"
		myftp.close()
		raw_input("--Press any key to return to main menu--")
		ClearScreen()
		return
	ClearScreen()
	print "Username and Password successfully verified"
	print myftp.getwelcome()
	print
	filePath = os.getcwd()
	fileList = os.listdir(filePath) 
	targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
	for filename in targetList:
		fileUp=open(filename,'rb')
		print "Uploading "+filename
		myftp.storbinary('STOR '+filename, fileUp) 
		fileUp.close()
	myftp.close() 

def StripServerComments(wholeFile):
	partFile = ""
	curLine="1"
	inBlock=0
	while curLine:
		while inBlock:
			curLine=wholeFile.readline()
			commentPattern = re.compile(r'<!.*preceding.*>')
			if commentPattern.search(curLine):
				inBlock=0
		curLine=wholeFile.readline()
		commentPattern = re.compile(r'<!.*following.*>')
		if commentPattern.search(curLine):
			inBlock=1
		else:
			commentPattern = re.compile(r'<!.*below.*>')
			if commentPattern.search(curLine):
				curLine=""
			else:
				partFile=partFile+curLine
			commentPattern = re.compile(r'<!.*above.*>')
			if commentPattern.search(curLine):
				partFile=""
	partFile=partFile+"</HTML>"
	return partFile
	
def InsertLink(wholeFile,target,newURL,linkName):
	if target=="visitors":
		print "Links may not be inserted after "+target
		return wholeFile.read()
	partFile = ""
	curLine="1"
	notFound=1
	while curLine:
		curLine=wholeFile.readline()
		commentPattern = re.compile(r'^<p.*>-?'+target+'.*')
		if commentPattern.search(curLine):
			notFound=0
			partFile=partFile+curLine
			curLine=wholeFile.readline()
			partFile=partFile+curLine
			partFile=partFile+'<p align=center><FONT size=4>~<a href="'+newURL+'">'+linkName+'</a>~</p>\n<BR>\n'
		else:
			partFile=partFile+curLine
	if notFound:
		print "The insertion point, "+target+", could not be found.  "+wholeFile.name+" was not altered"
	else:
		print linkName+" successfully inserted into "+wholeFile.name
	return partFile

def DeleteLink(wholeFile,target):
	if target=="home" or target=="visitors":
		print "Target "+target+" may not be deleted.  This is a permanent item."
		return wholeFile.read()
	partFile = ""
	curLine="1"
	notFound=1
	while curLine:
		curLine=wholeFile.readline()
		commentPattern = re.compile(r'^<p.*>-?'+target+'.*')
		if commentPattern.search(curLine):
			notFound=0
			curLine=wholeFile.readline()				
		else:
			partFile=partFile+curLine
	if notFound:
		print "The target for deletion, "+target+", was not found.  "+wholeFile.name+" unaltered"
	else:
		print target+" successfully deleted from "+wholeFile.name
	return partFile

def ChangeItem(wholeFile,targetItem,newItem):
	partFile = ""
	curLine="1"
	while curLine:
		curLine=wholeFile.readline()
		commentPattern = re.compile(targetItem)
		newLine=commentPattern.sub(newItem,curLine)
		partFile=partFile+newLine
	return partFile	

def MainMenu():
	print "HtmlFixer 1.1"
	print "  Scripted by Tucker A. Beck"
	print "    For questions/comments email to nax13@yahoo.com"
	print
	print
	print
	print
	print
	print
	print
	print
	print
	print
	print
	menuInput="a"
	while menuInput!="9":
		print
		print "--------------------------------------"
		print "1:  Insert a new side-link"
		print "2:  Delete a side-link"
		print "3:  Clean up server generated comments"
		print "4:  Replace text block"
		print "7:  Download files from FTP"
		print "8:  Upload files to FTP"
		print "9:  Quit"
		print "--------------------------------------"
		menuInput=raw_input(" :  ")
		if menuInput=="1":
			InsertMenu()
		if menuInput=="2":
			DeleteMenu()
		if menuInput=="3":
			StripFiles()
		if menuInput=="4":
			ChangeMenu()
		if menuInput=="7":
			FTPDown()
		if menuInput=="8":
			FTPUp()
	

def InsertMenu():
	print
	print
	target=raw_input("Insert after which item?  ")
	newURL=raw_input("File name for new item?   ")
	linkName=raw_input("Link title for new item?  ")
	print
	print
	filePath=os.getcwd()
	fileList = os.listdir(filePath)
	targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
	for fileName in targetList:
		oldFile=open(fileName)
		newFile=InsertLink(oldFile,target,newURL,linkName)
		oldFile.close()
		saveout=sys.stdout
		fileOut=open(fileName,'w')
		sys.stdout=fileOut
		print newFile
		fileOut.close()
		sys.stdout=saveout

def DeleteMenu():
	print
	print
	target=raw_input("Delete which item?  ")
	print
	print
	filePath=os.getcwd()
	fileList = os.listdir(filePath)
	targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
	for fileName in targetList:
		oldFile=open(fileName)
		newFile=DeleteLink(oldFile,target)
		oldFile.close()
		saveout=sys.stdout
		fileOut=open(fileName,'w')
		sys.stdout=fileOut
		print newFile
		fileOut.close()
		sys.stdout=saveout
		
def StripFiles():
	ClearScreen()
	filePath=os.getcwd()
	fileList = os.listdir(filePath)
	targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
	for fileName in targetList:
		oldFile=open(fileName)
		newFile=StripServerComments(oldFile)
		oldFile.close()
		saveout=sys.stdout
		fileOut=open(fileName,'w')
		sys.stdout=fileOut
		print newFile
		fileOut.close()
		sys.stdout=saveout
		print "Stripped server generated comments from "+fileName

def ChangeMenu():
	print
	print
	targetItem=raw_input("Change what text?  ")
	newItem=raw_input("Change to what?  ")
	print
	print
	filePath=os.getcwd()
	fileList = os.listdir(filePath)
	targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
	for fileName in targetList:
		oldFile=open(fileName)
		newFile=ChangeItem(oldFile,targetItem,newItem)
		oldFile.close()
		saveout=sys.stdout
		fileOut=open(fileName,'w')
		sys.stdout=fileOut
		print newFile
		fileOut.close()
		sys.stdout=saveout		
		print "Updated '"+targetItem+"' to '"+newItem+"' in "+fileName

def ClearScreen():
	for i in range(30):
		print ""

ClearScreen()
MainMenu()
ClearScreen()
print "Thanks for using HTMLFixer by Tucker Beck"
print "For questions or comments, please contact nax13@yahoo.com"
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
print
ext=raw_input("--press enter to exit--")

---
so long and thanks for all the fish

Member Avatar
-=disAbled=-
Newbie Poster
2 posts since Jun 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Maybe in the future, I should look closer at the line where I'm having a problem. It's really hard to get the retrbinary() command to work when you have storlines() where it should be. :o

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article