1

Using Python 3.6 successfully retrieving data from mssqlserver and querying oracle for historical data and exporting it via Pandas to a tab delimited file which I then want to be able to ftp to another mssql server location. Since I have not tried this before, I experimented as follows:

\\\\\\\\\\\\\\\\\\\ This works: passing hardcoded variable \\\\\\\\\\\\\\\\\\\\

from ftplib import FTP

host='myserver'
host_name='group_user_name'
host_pw='hostpassword'
host_dir='myworkingdirectory'

ftp = FTP(host)
ftp.login(user=host_name, passwd =host_pw)
ftp.cwd(host_dir)

filename = 'c:/temp/cigfun.txt'
fn = 'cigfun.txt' 

def placeFile():
    ftp.storbinary('STOR '+fn, open(filename, 'rb'))
    ftp.quit()

placeFile()

\\\\\\\\\\\\\\ This also works: hard coded \\\\\\\\\\\\\\

from ftplib import FTP

ftp = FTP('myserver')
ftp.login(user='group_user_name', passwd ='hostpassword')
ftp.cwd('myworkingdirectory')
ftp.retrlines('LIST')

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

This does not connect: retrieving data from stored procedure in MSSQL

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

if sql_id == str(5): 
        sql_cursor.execute("[dbo].[usp_GetProcess_"+seq_action+"] @sql_id = "+ sql_id)
        table = sql_cursor.fetchall()
        for row in table:
            host = "'"+row[1]+"'"
            host_name = "'"+row[2]+"'"
            host_pw = "'"+row[3]+"'"
            host_dir = "'"+row[4]+"'"
            host_dir = re.sub('[cd ]', '', host_dir)
            ftp_file = row[9]
            f_type = row[10]
            if f_type == "Tab (txt)":
                f_type = '.txt'
            elif f_type == "Comma (csv)":
                f_type = '.csv'
            else: f_type = '.xlsx'

            path_file = "'"+ftp_file + f_type+"'"
            fn="'"+os.path.basename(path_file)
            path_file = path_file.replace(r'\\','/')

            from ftplib import FTP
            ftp = FTP(host)
            ftp.login(user = host_name, passwd = host_pw)
            ftp.cwd(host_dir)

\\\\\\\\\\\\\\ throws the following error:

File "C:\Users\A22021\AppData\Local\Continuum\anaconda332\lib\socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
gaierror: [Errno 11004] getaddrinfo failed

aside from my awkward efforts at coding (only my 3rd production attempt using Python), can you see anything obvious that would cause the failure? error messages are so cryptic. I suspect it might have something to do with coded variable assignments, but I am baffled right now. Any constructive advice appreciated. PLG

Eugene Lisitsky
  • 9,867
  • 4
  • 30
  • 55
plg
  • 51
  • 2
  • 2
    Possible duplicate of ["getaddrinfo failed", what does that mean?](https://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean) – Carcigenicate Nov 27 '17 at 16:36

1 Answers1

0

If everything works when you hard-code the FTP information, then the issue here is most likely the way you are parsing the query results. You need to see exactly what is getting put into your host* variables. To start, you could just pepper your code with some print statements, but I would highly suggest using the python debugger instead. This probably sounds scary, but it's not that bad. Run it like this:

python -m pdb my_script.py

You can then step through each line of your code by pressing n. So for example, when you hit the line that says for row in table: and you press n you will now be inside that loop and can print(row). This will give you some insight into what is going wrong with the parsing.

MrName
  • 1,887
  • 11
  • 23