无法从应用程序引擎中的FTP服务器下载csv文件 [英] unable to dowload csv file from FTP server in app engine

查看:255
本文介绍了无法从应用程序引擎中的FTP服务器下载csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从AppEngine中的ftp服务器读取CSV文件,我可以连接到ftp服务器。但它返回错误,当我试图检索文件。
这是我从服务器读取CSV文件的代码:

  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,enumerate(csvfile)中的行:
print row
/ pre>

这里是我得到的错误的回溯:

  Traceback(最近一次调用):
文件/home/vikas/apps/myproj/django/core/handlers/base.py,第111行,在get_response
response = callback请求,* callback_args,** callback_kwargs)
文件/home/vikas/apps/myproj/admin/admin_actions.py,第3528行,在get_ftp_file
session.retrbinary('RETR myfile.csv' ,output.write)
文件/usr/lib/python2.7/ftplib.py,行414,在retrbinary
conn = self.transfercmd(cmd,rest)
文件 /usr/lib/python2.7/ftplib.py,行376,在transfercmd
中返回self.ntransfercmd(cmd,rest)[0]
文件/usr/lib/python2.7/ ftplib.py,行354,在ntransfercmd
sock = self.makeport()
文件/usr/lib/python2.7/ftplib.py,行283,在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,第318行,在getaddrinfo
中提高gaierror(EAI_NONAME,'nodename或servname提供或不知道')
gaierror:[Errno 8] nodename或servname提供或不知道

我不知道我做了什么错误, code> dir() nlst() etc正在运行,上述错误发生在我添加它们。 >

解决方案

唉,App Engine当前提供的套接字模拟并不足以覆盖所有的用例。



这里有一个访问一个着名的公共匿名FTP服务器并从中获取一个小文本文件的示例,只是为了让每个人都可以重现和实验...:in file getit.py ,我们有:

  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()

如果__name__ =='__main__':
print(getit())

这个可以独立运行, python getit.py ,您是否留下 set_pasv 在这里注释,或删除注释。



GAE应用程式,例如:

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

这可以与 set_pasv 左注释,但如果你de



因此,对强制您进入活动模式(不支持被动模式)的服务器进行FTP不是以这种方式工作。但是,是一个相当破碎的服务器 - 你能把它固定,以便它支持流行的默认被动模式吗?然后您的GAE应用程序可以与它快乐工作...!


Im trying to read CSV files from ftp server in AppEngine, and I am able to connect to the ftp server. But it returned error when I tried to retrieve files. Here is my code to read the CSV files from 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 traceback of the error 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 have done wrong, none of the commands like dir() nlst() etc is working, and the above error occurred as soon as I added them .

解决方案

Alas, the socket simulation currently offered by App Engine isn't quite good enough to cover all use cases.

Here's a sample accessing a well-known public anonymous FTP server and fetching a small text file from it, just so everybody can reproduce and experiment...: in file getit.py, 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 fine as stand-alone, python getit.py, whether you leave the set_pasv commented as here, or remove the comment.

To embed this in a GAE app, e.g:

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 left commented, but if you de-comment it, you get essentially the same exception you received.

So doing FTP to a server that forces you to active mode (doesn't support passive mode) is not going to work this way. However, that is a rather broken server -- could you get it fixed so that it supports the popular, default passive mode? Then your GAE app could work happily with it...!

这篇关于无法从应用程序引擎中的FTP服务器下载csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆