Error uploading a file to a ftp server

2019-07-22 17:26发布

问题:

I have a program which sends a text file to a ftp server. The code is:

def sendBug():

    session = ftplib.FTP('ftp://xxxx.xxxx.xxxx.xxxx', " ", " ")
    bugfile = open(bugreport.txt, "r")
    session.storlines("STOR bugreport.txt", bugfile)
    bugfile.close()
    session.quit()

However, when I run that script, I'm getting something like this:

Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
 File "C:\Users\Nitro\Desktop\py2exe stuff\school firewall ui.py", line 46, in getnews
ftp= ftplib.FTP(server)
File "C:\Python27\lib\ftplib.py", line 120, in __init__
self.connect(host)
File "C:\Python27\lib\ftplib.py", line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11004] getaddrinfo failed

any way to fix this? Im hosting a ftp server with IIS. And school firewall ui is my program.

回答1:

for first Server line doen't need the "ftp://"

Basiclly gaierror: [Errno 11004] getaddrinfo failed means that the ftp module can't understand the 'ftp://xxxx.xxxx.xxxx.xxxx' address you gave him - host name.

gaierror from Docs:

This exception is raised for address-related errors, for getaddrinfo() and getnameinfo(). The accompanying value is a pair (error, string) representing an error returned by a library call. string represents the description of error, as returned by the gai_strerror() C function. The error value will match one of the EAI_* constants defined in this module.

You are passing ftplib.FTP() with host, username and password (both user and password are space at your case)

if it's supposed to be anonymous just use:

ftp = ftplib.FTP('you address')     
ftp.login() 

If it still doesn't work try to ping your address or try to connect some known FTP to understated where is the problem.

You can also try this link it's very useful

For more info about python FTP try the docs



标签: python ftp