Failed to download csv file from FTP server in application

I am trying to read CSV files from an ftp server in AppEngine and I can connect to an ftp server. But when he tried to restore the files, he returned an error. Here is my code for reading CSV files from the server:

import ftplib import cStringIO import csv session = ftplib.FTP('myftpserver.com') session.login('username','pwd') session.set_pasv(False) output = cStringIO.StringIO() session.retrbinary('RETR myfile.csv', output.write) csvfile = csv.reader(output.getvalue().splitlines(), delimiter=',') for i, row in enumerate(csvfile): print row 

And here is the error trace I am getting:

  Traceback (most recent call last): File "/home/vikas/apps/myproj/django/core/handlers/base.py", line 111, in get_response response = callback(request, *callback_args, **callback_kwargs) File "/home/vikas/apps/myproj/admin/admin_actions.py", line 3528, in get_ftp_file session.retrbinary('RETR myfile.csv', output.write) File "/usr/lib/python2.7/ftplib.py", line 414, in retrbinary conn = self.transfercmd(cmd, rest) File "/usr/lib/python2.7/ftplib.py", line 376, in transfercmd return self.ntransfercmd(cmd, rest)[0] File "/usr/lib/python2.7/ftplib.py", line 354, in ntransfercmd sock = self.makeport() File "/usr/lib/python2.7/ftplib.py", line 283, in makeport for res in socket.getaddrinfo(None, 0, self.af, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): File "/home/vikas/gcloud/google-cloud-sdk/platform/google_appengine/google/appengine/api/remote_socket/_remote_socket.py", line 318, in getaddrinfo raise gaierror(EAI_NONAME, 'nodename nor servname provided, or not known') gaierror: [Errno 8] nodename nor servname provided, or not known 

I have no idea what I did wrong, none of the commands, such as dir() nlst() , etc., work, and the indicated error occurred as soon as I added them.

+1
python google-app-engine csv ftp
source share
1 answer

Alas, the socket simulation offered by App Engine is not good enough to cover all use cases.

Here is an example of accessing a well-known open source anonymous FTP server and extracting a small text file from it so that everyone can play and experiment ...: in the getit.py file we have:

 import ftplib import cStringIO def getit(): session = ftplib.FTP('ftp.mozilla.org') session.login('anonymous','') # session.set_pasv(False) session.cwd('/pub/mozilla.org') output = cStringIO.StringIO() session.retrbinary('RETR README', output.write) return output.getvalue() if __name__ == '__main__': print(getit()) 

This runs as standalone, python getit.py , leaving the set_pasv comment here, or delete the comment.

Paste this into a GAE application, for example:

 import getit class GetitPage(webapp2.RequestHandler): def get(self): # pylint:disable-msg=invalid-name try: result = getit.getit() except Exception as e: result = 'Error {}: {}'.format(type(e), e) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(result) 

this works fine with the set_pasv comment, but if you comment on it, you will get essentially the same exception you received.

Thus, an FTP server that makes you work in active mode (does not support passive mode) will not work in this way. However, this is a pretty broken server - can you set it up to support the popular default passive mode? Then your GAE application can work with it with pleasure ...!

+2
source share

All Articles