HI,
i'm very new to python, I'm trying to write some ftp modules for reuse.
Here are my modules/files list:
my_ftp.py: contains definition of my ftp functions
test_ftp.py: simple test script to use the ftp functions above
config.ini: a config file containing ftp parameters such as address, username and password
my_ftp.py :
socket.gaierror: [Errno 11001] getaddrinfo failed
Can you give me some hints about "organizing" these variables and this config file in order to reuse my code by simply selecting a different config file in my "test_ftp.py" script?
Note: it all works fine if I explicitly assign the variable values inside my downloadAll function
i'm very new to python, I'm trying to write some ftp modules for reuse.
Here are my modules/files list:
my_ftp.py: contains definition of my ftp functions
test_ftp.py: simple test script to use the ftp functions above
config.ini: a config file containing ftp parameters such as address, username and password
my_ftp.py :
from ftplib import FTP import os import configparser ftpAddress = '' ftpUserName = '' ftpPassword = '' def initFtpParameters(parametersFileName): config = configparser.ConfigParser() config.read(parametersFileName) global ftpAddress global ftpUserName global ftpPassword ftpAddress = config.get("myvars",'ftpAddress') ftpUserName = config.get("myvars",'ftpUserName') ftpPassword = config.get("myvars",'ftpPassword') def downloadAll(localPath): print('user:' + ftpUserName + ' ' + ftpPassword + ' ' + ftpAddress) #here i'm checking if the variable's value are ok ftp = FTP(ftpAddress) ftp.login(user=ftpUserName, passwd=ftpPassword) ftpFileNames = ftp.nlst() for ftpfileName in ftpFileNames: if not isDir(ftp,ftpfileName) : localFileName = os.path.join(localPath, ftpfileName) localFile = open(localFileName, 'wb') ftp.retrbinary('RETR ' + ftpfileName, localFile.write, 1024 ) localFile.close() ftp.quit()test_ftp.py:
import my_ftp my_ftp.initFtpParameters('C:\\Users\\Python\\config.ini') my_ftp.downloadAll('C:\\Users\\Python\\')config.ini
[myvars] ftpAddress: 'ftp.myaddress.it' ftpUserName: 'myUserName' ftpPassword: '123456'It seems there is some issue with the variables scope. I'm getting this error:
socket.gaierror: [Errno 11001] getaddrinfo failed
Can you give me some hints about "organizing" these variables and this config file in order to reuse my code by simply selecting a different config file in my "test_ftp.py" script?
Note: it all works fine if I explicitly assign the variable values inside my downloadAll function