Hi,
I'm trying to read IP and login info from a text file params.txt. The contents of the text file are:
IP = '1.2.3.4'
UID = 'anonymous'
PWD = 'my@my.com'
Upload Directory = '/'
Home Directory = 'C:/Users/MyDocuments/Python/FilesUpload/'

Can someone help me please?

import ftplib
import re
import os
import sys
import traceback  

param = open("params.txt", "r")
    for line in param:
        if re.match("(.*)IP(.*)", line):
            print ("" + re.sub('IP = ','',line))
            FtpIP = re.sub('IP = ','',line)
            print ("FTPIP = " + FtpIP)

ftp = ftplib.FTP()
print ("" + FtpIP)
ftp.connect(FtpIP)

File "C:\Python32\lib\socket.py", line 386, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11004] getaddrinfo failed

lines 8-12 are misindented, quotes are not allowed in IP addresses (FtpIP is also against the Python naming convention suggesting it is class, not variable):

import ftplib
import re

with open("params.txt", "r") as param:
    for line in param:
        if re.match("(.*)IP(.*)", line):
            print ("" + re.sub('IP = ','',line))
            FtpIP = re.sub('IP = ','',line).strip("' \n")
            print ("FTPIP = " + repr(FtpIP))

ftp = ftplib.FTP()
print(FtpIP) # no quotes
ftp.connect(FtpIP)

Edited 4 Years Ago by pyTony

I made the changes but it still is not working. Now getting

socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

This article has been dead for over six months. Start a new discussion instead.