ftplib and the retrbinary() command

Reply

Join Date: Jun 2005
Posts: 2
Reputation: -=disAbled=- is an unknown quantity at this point 
Solved Threads: 0
-=disAbled=- -=disAbled=- is offline Offline
Newbie Poster

ftplib and the retrbinary() command

 
0
  #1
Jun 28th, 2005
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:

  1. Username and Password successfully verified
  2. 220 FTP Server ready
  3.  
  4. Downloading last.htm
  5. Traceback (most recent call last):
  6. File "HTMLFixer.py", line 303, in ?
  7. MainMenu()
  8. File "HTMLFixer.py", line 212, in MainMenu
  9. FTPDown()
  10. File "HTMLFixer.py", line 46, in FTPDown
  11. myftp.storlines('RETR '+filename,fileOut.write)
  12. File "C:\Documents and Settings\tbeck\My Documents\tools\pt\lib\ftplib.py", line 428, in storlines
  13. buf = fp.readline()
  14. 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:

  1. from ftplib import FTP
  2. import os
  3. import re
  4. import sys
  5. import fnmatch
  6.  
  7. def FTPDown():
  8. print
  9. print
  10. ftplocation=raw_input("Please Enter the FTP location: ")
  11. print "Connecting to "+ftplocation
  12. try:
  13. myftp=FTP(ftplocation)
  14. except:
  15. print "Could not connect to "+ftplocation
  16. raw_input("--Press any key to return to main menu--")
  17. ClearScreen()
  18. return
  19. print
  20. print "Connected to "+ftplocation
  21. myftp.set_debuglevel(0)
  22. username=raw_input("Please Enter Username: ")
  23. password=raw_input("Please Enter Password: ")
  24. print
  25. print
  26. try:
  27. myftp.login(username,password)
  28. except:
  29. print "Login Failed: Username or Password is incorrect"
  30. myftp.close()
  31. raw_input("--Press any key to return to main menu--")
  32. ClearScreen()
  33. return
  34. ClearScreen()
  35. print "Username and Password successfully verified"
  36. print myftp.getwelcome()
  37. print
  38. fileList = myftp.nlst()
  39. targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
  40. for filename in targetList:
  41. print "Downloading "+filename
  42. try:
  43. fileOut=open(filename,'wb')
  44. except:
  45. print "couldn't open the output file"
  46. myftp.storlines('RETR '+filename,fileOut.write)
  47. fileOut.close()
  48. myftp.close()
  49.  
  50.  
  51.  
  52.  
  53. def FTPUp():
  54. print
  55. print
  56. ftplocation=raw_input("Please Enter the FTP location: ")
  57. print "Connecting to "+ftplocation
  58. try:
  59. myftp=FTP(ftplocation)
  60. except:
  61. print "Could not connect to "+ftplocation
  62. raw_input("--Press any key to return to main menu--")
  63. ClearScreen()
  64. return
  65. print
  66. print "Connected to "+ftplocation
  67. myftp.set_debuglevel(0)
  68. username=raw_input("Please Enter Username: ")
  69. password=raw_input("Please Enter Password: ")
  70. print
  71. print
  72. try:
  73. myftp.login(username,password)
  74. except:
  75. print "Login Failed: Username or Password is incorrect"
  76. myftp.close()
  77. raw_input("--Press any key to return to main menu--")
  78. ClearScreen()
  79. return
  80. ClearScreen()
  81. print "Username and Password successfully verified"
  82. print myftp.getwelcome()
  83. print
  84. filePath = os.getcwd()
  85. fileList = os.listdir(filePath)
  86. targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
  87. for filename in targetList:
  88. fileUp=open(filename,'rb')
  89. print "Uploading "+filename
  90. myftp.storbinary('STOR '+filename, fileUp)
  91. fileUp.close()
  92. myftp.close()
  93.  
  94. def StripServerComments(wholeFile):
  95. partFile = ""
  96. curLine="1"
  97. inBlock=0
  98. while curLine:
  99. while inBlock:
  100. curLine=wholeFile.readline()
  101. commentPattern = re.compile(r'<!.*preceding.*>')
  102. if commentPattern.search(curLine):
  103. inBlock=0
  104. curLine=wholeFile.readline()
  105. commentPattern = re.compile(r'<!.*following.*>')
  106. if commentPattern.search(curLine):
  107. inBlock=1
  108. else:
  109. commentPattern = re.compile(r'<!.*below.*>')
  110. if commentPattern.search(curLine):
  111. curLine=""
  112. else:
  113. partFile=partFile+curLine
  114. commentPattern = re.compile(r'<!.*above.*>')
  115. if commentPattern.search(curLine):
  116. partFile=""
  117. partFile=partFile+"</HTML>"
  118. return partFile
  119.  
  120. def InsertLink(wholeFile,target,newURL,linkName):
  121. if target=="visitors":
  122. print "Links may not be inserted after "+target
  123. return wholeFile.read()
  124. partFile = ""
  125. curLine="1"
  126. notFound=1
  127. while curLine:
  128. curLine=wholeFile.readline()
  129. commentPattern = re.compile(r'^<p.*>-?'+target+'.*')
  130. if commentPattern.search(curLine):
  131. notFound=0
  132. partFile=partFile+curLine
  133. curLine=wholeFile.readline()
  134. partFile=partFile+curLine
  135. partFile=partFile+'<p align=center><FONT size=4>~<a href="'+newURL+'">'+linkName+'</a>~</p>\n<BR>\n'
  136. else:
  137. partFile=partFile+curLine
  138. if notFound:
  139. print "The insertion point, "+target+", could not be found. "+wholeFile.name+" was not altered"
  140. else:
  141. print linkName+" successfully inserted into "+wholeFile.name
  142. return partFile
  143.  
  144. def DeleteLink(wholeFile,target):
  145. if target=="home" or target=="visitors":
  146. print "Target "+target+" may not be deleted. This is a permanent item."
  147. return wholeFile.read()
  148. partFile = ""
  149. curLine="1"
  150. notFound=1
  151. while curLine:
  152. curLine=wholeFile.readline()
  153. commentPattern = re.compile(r'^<p.*>-?'+target+'.*')
  154. if commentPattern.search(curLine):
  155. notFound=0
  156. curLine=wholeFile.readline()
  157. else:
  158. partFile=partFile+curLine
  159. if notFound:
  160. print "The target for deletion, "+target+", was not found. "+wholeFile.name+" unaltered"
  161. else:
  162. print target+" successfully deleted from "+wholeFile.name
  163. return partFile
  164.  
  165. def ChangeItem(wholeFile,targetItem,newItem):
  166. partFile = ""
  167. curLine="1"
  168. while curLine:
  169. curLine=wholeFile.readline()
  170. commentPattern = re.compile(targetItem)
  171. newLine=commentPattern.sub(newItem,curLine)
  172. partFile=partFile+newLine
  173. return partFile
  174.  
  175. def MainMenu():
  176. print "HtmlFixer 1.1"
  177. print " Scripted by Tucker A. Beck"
  178. print " For questions/comments email to nax13@yahoo.com"
  179. print
  180. print
  181. print
  182. print
  183. print
  184. print
  185. print
  186. print
  187. print
  188. print
  189. print
  190. menuInput="a"
  191. while menuInput!="9":
  192. print
  193. print "--------------------------------------"
  194. print "1: Insert a new side-link"
  195. print "2: Delete a side-link"
  196. print "3: Clean up server generated comments"
  197. print "4: Replace text block"
  198. print "7: Download files from FTP"
  199. print "8: Upload files to FTP"
  200. print "9: Quit"
  201. print "--------------------------------------"
  202. menuInput=raw_input(" : ")
  203. if menuInput=="1":
  204. InsertMenu()
  205. if menuInput=="2":
  206. DeleteMenu()
  207. if menuInput=="3":
  208. StripFiles()
  209. if menuInput=="4":
  210. ChangeMenu()
  211. if menuInput=="7":
  212. FTPDown()
  213. if menuInput=="8":
  214. FTPUp()
  215.  
  216.  
  217. def InsertMenu():
  218. print
  219. print
  220. target=raw_input("Insert after which item? ")
  221. newURL=raw_input("File name for new item? ")
  222. linkName=raw_input("Link title for new item? ")
  223. print
  224. print
  225. filePath=os.getcwd()
  226. fileList = os.listdir(filePath)
  227. targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
  228. for fileName in targetList:
  229. oldFile=open(fileName)
  230. newFile=InsertLink(oldFile,target,newURL,linkName)
  231. oldFile.close()
  232. saveout=sys.stdout
  233. fileOut=open(fileName,'w')
  234. sys.stdout=fileOut
  235. print newFile
  236. fileOut.close()
  237. sys.stdout=saveout
  238.  
  239. def DeleteMenu():
  240. print
  241. print
  242. target=raw_input("Delete which item? ")
  243. print
  244. print
  245. filePath=os.getcwd()
  246. fileList = os.listdir(filePath)
  247. targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
  248. for fileName in targetList:
  249. oldFile=open(fileName)
  250. newFile=DeleteLink(oldFile,target)
  251. oldFile.close()
  252. saveout=sys.stdout
  253. fileOut=open(fileName,'w')
  254. sys.stdout=fileOut
  255. print newFile
  256. fileOut.close()
  257. sys.stdout=saveout
  258.  
  259. def StripFiles():
  260. ClearScreen()
  261. filePath=os.getcwd()
  262. fileList = os.listdir(filePath)
  263. targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
  264. for fileName in targetList:
  265. oldFile=open(fileName)
  266. newFile=StripServerComments(oldFile)
  267. oldFile.close()
  268. saveout=sys.stdout
  269. fileOut=open(fileName,'w')
  270. sys.stdout=fileOut
  271. print newFile
  272. fileOut.close()
  273. sys.stdout=saveout
  274. print "Stripped server generated comments from "+fileName
  275.  
  276. def ChangeMenu():
  277. print
  278. print
  279. targetItem=raw_input("Change what text? ")
  280. newItem=raw_input("Change to what? ")
  281. print
  282. print
  283. filePath=os.getcwd()
  284. fileList = os.listdir(filePath)
  285. targetList= [fileName for fileName in fileList if fnmatch.fnmatch(fileName,'*.htm')]
  286. for fileName in targetList:
  287. oldFile=open(fileName)
  288. newFile=ChangeItem(oldFile,targetItem,newItem)
  289. oldFile.close()
  290. saveout=sys.stdout
  291. fileOut=open(fileName,'w')
  292. sys.stdout=fileOut
  293. print newFile
  294. fileOut.close()
  295. sys.stdout=saveout
  296. print "Updated '"+targetItem+"' to '"+newItem+"' in "+fileName
  297.  
  298. def ClearScreen():
  299. for i in range(30):
  300. print ""
  301.  
  302. ClearScreen()
  303. MainMenu()
  304. ClearScreen()
  305. print "Thanks for using HTMLFixer by Tucker Beck"
  306. print "For questions or comments, please contact nax13@yahoo.com"
  307. print
  308. print
  309. print
  310. print
  311. print
  312. print
  313. print
  314. print
  315. print
  316. print
  317. print
  318. print
  319. print
  320. print
  321. print
  322. print
  323. print
  324. print
  325. print
  326. print
  327. print
  328. print
  329. ext=raw_input("--press enter to exit--")

---
so long and thanks for all the fish
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2
Reputation: -=disAbled=- is an unknown quantity at this point 
Solved Threads: 0
-=disAbled=- -=disAbled=- is offline Offline
Newbie Poster

Re: ftplib and the retrbinary() command

 
0
  #2
Jun 29th, 2005
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
Last edited by -=disAbled=-; Jun 29th, 2005 at 7:34 pm. Reason: Figured out the problem
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC