I hope I'm doing something bad but it seems that I can't perform a FTP login in appex mode...
Forum Archive
FTP in appex mode
What kind of error are you getting? Does your code work outside of the app extension?
I suppose I did something bad because I just try this short code with success.
I don't know which error, I just have a "try/except.
I don't understand your question "outside of the app extension"
Thanks to help me
# coding: utf-8
import console
from ftplib import FTP
user = 'Christian'
pwd = 'Xxxxxxx'
ftpok = True
try:
ftp = FTP('iMac.local') #connect
ftp.login(user,pwd)
except:
ftpok = False
console.alert('Mac not accessible','','Ok')
if ftpok:
console.alert('Mac accessible','','Ok')
source='Google Drive/Livres/Romans/'
# Get all authors sub-dirctories
authors = ftp.nlst(source)
print authors[0]
ftp.quit()
My real code is long, it would be called by iBooks with a book Info passed via the share extension.
Then I extract the author and title of the book, then I FTP to my Mac to get the ePub file, extract the cover and display it.
Why the try/except? When an exception happens, Python will automatically stop the program and display the full error message and traceback, so there's no need for a ftpok variable or a manual console.alert.
The issue with catching exceptions by hand (especially with a bare except or an except Exception) is that you're hiding Python's information about the error and displaying your own error message instead. This isn't very useful when you're trying to find out what's going wrong.
I don't want that my program stops, I want to continue, warn the user there is a problem accessing the Mac, perhaps access of my backup on a NAS etc...
I'm sure I do something bad, please explain me what is "outside the extension"
I've removed my try/except to get the errors info:
Traceback (most recent call last):
File "/private/var/mobile/Containers/Shared/AppGroup/BC53E549-355D-4E77-BC46-64C3D3E0BDAF/Documents/MesApps/Epubs.py", line 265, in
main()
File "/private/var/mobile/Containers/Shared/AppGroup/BC53E549-355D-4E77-BC46-64C3D3E0BDAF/Documents/MesApps/Epubs.py", line 240, in main
get_authors()
File "/private/var/mobile/Containers/Shared/AppGroup/BC53E549-355D-4E77-BC46-64C3D3E0BDAF/Documents/MesApps/Epubs.py", line 65, in get_authors
ftp.login(user,pwd)
File "/var/containers/Bundle/Application/46D4F46D-99D6-4950-9EB8-993E92B02425/Pythonista.app/Frameworks/PythonistaKit.framework/pylib/ftplib.py", line 389, in login
if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
File "/var/containers/Bundle/Application/46D4F46D-99D6-4950-9EB8-993E92B02425/Pythonista.app/Frameworks/PythonistaKit.framework/pylib/ftplib.py", line 245, in sendcmd
return self.getresp()
File "/var/containers/Bundle/Application/46D4F46D-99D6-4950-9EB8-993E92B02425/Pythonista.app/Frameworks/PythonistaKit.framework/pylib/ftplib.py", line 220, in getresp
raise error_perm, resp
ftplib.error_perm: 530 Login incorrect.
@cvp said:
I don't understand your question "outside of the app extension"
What I meant was: Does your script work when you run it in the Pythonista app, and not from the share sheet.
Yes, it functions.
And my little test script functions in the appex mode
Are you using the keychain module to get the password by any chance? The keychains of the app extension and the main app are separate, so that could be the reason...
I just tried without keychain and discovered that. I was busy to write this text when I received your advice... In French, we say "Les grands esprits se rencontrent" 😀
Thanks and sorry for the disturbance
"Les grands esprits se rencontrent" == "Great minds think alike"
@cvp The traceback is useful, as you see :) It's best to first make sure that your program works correctly in all cases, and then perhaps add more friendly error messages using try/except. And you should almost never use except without an exception name or except Exception - only catch the exception that you want. For example, to display a nice error message when the password is incorrect, use except ftplib.error_perm. Otherwise you'll catch other exceptions by accident that you didn't expect, which makes debugging very hard.
Thanks for your advices
It's sure I'll stay a beginner for some time 😉