- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 pythons ftplib 尝试从远程服务器检索文件(练习项目)。我在发送文件时没有遇到任何问题,但在尝试检索时遇到错误。我正在使用 python 3。这是我的代码:
from ftplib import FTP
user = 'myusername'
passw = 'mypassw'
url = input('FTP url: ').lower().strip()
ftp = FTP(url) #connect to the host, default port
ftp.login(user, passw) # enters login info
def getfile():
ftp.retrlines('LIST') # List directory contents
filename = input('Name of file: ')
localfile = open(filename, 'wb')
ftp.retrbinary('RETR' + filename, localfile.write, 1024)
localfile.close()
getfile()
这是我得到的错误:
Traceback (most recent call last):
File "C:\Users\User\Desktop\School\python\ftpget.py", line 16, in <module>
getfile()
File "C:\Users\User\Desktop\School\python\ftpget.py", line 13, in getfile
ftp.retrbinary('RETR' + filename, localfile.write, 1024)
File "C:\Python34\lib\ftplib.py", line 441, in retrbinary
with self.transfercmd(cmd, rest) as conn:
File "C:\Python34\lib\ftplib.py", line 398, in transfercmd
return self.ntransfercmd(cmd, rest)[0]
File "C:\Python34\lib\ftplib.py", line 364, in ntransfercmd
resp = self.sendcmd(cmd)
File "C:\Python34\lib\ftplib.py", line 272, in sendcmd
return self.getresp()
File "C:\Python34\lib\ftplib.py", line 245, in getresp
raise error_perm(resp)
ftplib.error_perm: 500 ?
知道是什么导致了错误吗?
最佳答案
如果响应的状态代码以“5”开头,
ftplib
会引发错误。这意味着服务器正在返回 5xx 错误:
0.5.1 500 Internal Server Error
The server encountered an unexpected condition which prevented it from fulfilling the request.
10.5.2 501 Not Implemented
The server does not support the functionality required to fulfill the request. This is the appropriate response when the server does not recognize the request method and is not capable of supporting it for any resource.
10.5.3 502 Bad Gateway
The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.
10.5.4 503 Service Unavailable
The server is currently unable to handle the request due to a temporary overloading or maintenance of the server. The implication is that this is a temporary condition which will be alleviated after some delay. If known, the length of the delay MAY be indicated in a Retry-After header. If no Retry-After is given, the client SHOULD handle the response as it would for a 500 response.
Note: The existence of the 503 status code does not imply that a
server must use it when becoming overloaded. Some servers may wish
to simply refuse the connection.10.5.5 504 Gateway Timeout
The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI (e.g. HTTP, FTP, LDAP) or some other auxiliary server (e.g. DNS) it needed to access in attempting to complete the request.
Note: Note to implementors: some deployed proxies are known to
return 400 or 500 when DNS lookups time out.10.5.6 505 HTTP Version Not Supported
The server does not support, or refuses to support, the HTTP protocol version that was used in the request message. The server is indicating that it is unable or unwilling to complete the request using the same major version as the client, as described in section 3.1, other than with this error message. The response SHOULD contain an entity describing why that version is not supported and what other protocols are supported by that server.
根据ftp docs :
FTP.retrbinary(command, callback[, maxblocksize[, rest]])
Retrieve a file in binary transfer mode. command should be an appropriate RETR command: 'RETR filename'.
但是你写了这个:
ftp.retrbinary('RETR' + filename, localfile.write, 1024)
这会产生这样的东西:
ftp.retrbinary('RETRdog.jpg', localfile.write, 1024)
'RETR'后需要加一个空格。
顺便说一下,您可以编写一个简短的 python 程序来充当 ftp 服务器。首先安装模块pyftpdlib
:
$ pip3.4 install pyftpdlib #See note on 3.4 below
如果你的系统上只有一个版本的python,你可以这样写:
$ pip install pyftpdlib
如果您的计算机上有多个版本的 python,请用正确的版本号替换 3.4。您指定的版本号是将安装 pyftpdlib 模块的 python 版本。
下面是一个 ftp 服务器在 python 中的样子:
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
my_authorizer = DummyAuthorizer()
my_authorizer.add_user(
username = '7stud',
password = '1234',
homedir = '.',
perm='elrw' #permissions: 'e'->change dir, 'l'->list files, 'r'->retrieve files, 'w'->store a file to the server
)
my_handler = FTPHandler
my_handler.authorizer = my_authorizer
port = 2121
my_handler.banner = "You connected to my ftp server on port: {}".format(port)
address = ('localhost', port)
server = FTPServer(address, my_handler)
server.max_cons = 256
server.max_cons_per_ip = 5
server.serve_forever()
pyftpdlib
文档和教程 here .add_user()
和权限描述 here .
然后,您可以为您的服务器创建一个目录:
$ mkdir ftp_server
$ cd ftp_server
$ touch my_ftp_server.py
将上面的代码放入文件my_ftp_server.py
。然后将一些文件添加到您要练习“下载”的 ftp_server 目录。然后通过运行 my_ftp_server.py 启动服务器。
在另一个终端窗口中,将目录切换到包含您的 python 程序的目录:
$ cd python_programs
然后对您的 ftp 客户端程序做一些小改动:
from ftplib import FTP
def getfile(conn): #It's good practice not to refer to global variables in your functions.
#Instead, pass in any values your function needs as arguments.
conn.retrlines('LIST') # List directory contents
filename = input('Name of file: ')
localfile = open(filename, 'wb')
ftp_command = 'RETR {}'.format(filename)
conn.retrbinary(ftp_command, localfile.write, 1024)
localfile.close()
user = '7stud'
passw = '1234'
port = 2121
url = input('FTP url: ').lower().strip()
ftp = FTP()
ftp.connect(url, port)
ftp.login(user, passw)
getfile(ftp)
ftp.quit()
这是您的 ftp 客户端程序的示例运行:
~/python_programs$ python3.4 ftp_client.py
FTP url: localhost
drwxr-xr-x 4 7stud staff 136 Feb 11 09:07 cgi-bin
-rw-r--r-- 1 7stud staff 3446 Jun 08 2013 client_socket.py
-rw-r--r-- 1 7stud staff 680 Feb 15 03:03 ftp_server.py
-rw-r--r-- 1 7stud staff 721 Feb 12 03:01 http_server.py
-rw-r--r-- 1 7stud staff 498 Jan 01 07:10 index.html
-rw-r--r-- 1 7stud staff 68 Jan 01 05:03 oneliner.py
-rw-r--r-- 1 7stud staff 954 Feb 11 09:05 socket_server.py
-rw-r--r-- 1 7stud staff 0 Feb 15 02:50 test.png
Name of file: test.png
~/python_programs$ ls *.png
bar_freq.png example.png test.png
~/python_programs$
关于python - 运行 FTP.retrbinary 检索文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28522047/
如果我使用下面的代码,数据将为零 dispatch_async(dispatch_get_global_queue(0,0), ^{ UIImage *img = [[UIImage allo
fread来自 data.table包一般可以在读取文件时自动确定列分隔符( sep )。 例如,这里fread自动检测 |作为列分隔符: library(data.table) fread(past
因此,如果我有一个如下所示的数据框: A B C rowname1 4.5 4 3.2 rowname2 3 23
我有一个汽车模型的搜索数据库:“日产Gtr”,“Huynday Elantra”,“Honda Accord”等。 现在我还有一个用户列表和他们喜欢的汽车类型 user1喜欢:carId:1234,c
我正在使用 Javamail 来获取一些电子邮件数据。我将用户输入作为电子邮件 ID、imap 地址和密码并连接到 imap。然后我监视收件箱的电子邮件并查明此人是否在“收件人”或“抄送”中。 Ema
我有一些数据,我想根据差距统计来评估最佳簇数。 我阅读了 gap statistic 上的页面在 r 中给出了以下示例: gs.pam.RU Number of clusters (method '
我有一个用户名和密码组合,我将使用它通过 java 代码访问安全服务器。 我的想法是: 在外部存储加密凭据 执行时提示用户输入解密密码 在使用前将解密的凭据直接存储在字符数组中 使用凭据连接到数据库
这是 Firebase 数据:[Firebase 数据][1] 我必须从员工那里检索所有字段并将其存储在一个数组中。 现在数据更改 toast 消息即将到来,但已经很晚了。 Firebase.setA
我是 iOS 的新手,正在开发一个基本的应用程序,它目前正在使用 SSKeychain 和 AFNetworking 与 API 进行交互。当您使用我检索的应用程序登录并在我的 CredentialS
编辑:这个问题已经在 apphacker 和 ConcernedOfTunbridgeWells 的帮助下得到解决。我已更新代码以反射(reflect)我将使用的解决方案。 我目前正在编写一个群体智能
我是 C 的新手,我想编写一个程序来检查用户输入的单词是否合法。我已经在 stackoverflow 上搜索了建议,但很多都是针对特定情况的。请在我被激怒之前,我知道这个语法不正确,但正在寻找一些关于
我相信你们中的一些人编写过 C# 类,这些类必须从数据库设置密码/从数据库获取密码。 我假设敏感细节不会以明文形式显示。处理此类数据的推荐程序是什么?检索到的文本是否加密?您是否将 pws 存储在加密
我在 linux 上使用 2.7 之前的 python 版本,想知道如何检索 RUID? 2.7 及更高版本从 os 包中获得了 getresuid,但我似乎找不到 2.6 的等效项 最佳答案 您可以
我已经在 Android 中实现了一个存储对象的标准 LRUCache。每个键都是与存储的对象关联的唯一 ObjectId。我的问题是从缓存中检索对象的唯一方法是通过 ObjectId(无迭代器)。实
这已经被问过很多次了。解决方案(对我有用)是从 packages.config 文件(这就足够了)和 packages 文件夹中删除 *** 包。 这对我来说是一个糟糕的解决方案,因为每次我想安装一些
我有以下文字: #{king} for a ##{day}, ##{fool} for a #{lifetime} 以及以下(损坏的)正则表达式: [^#]#{[a-z]+} 我想匹配所有#{word
我正在寻找一种快速(如高性能,而不是快速修复)解决方案来持久化和检索数千万个小型(大约 1k)二进制对象。每个对象都应该有一个用于检索的唯一 ID(最好是 GUID 或 SHA)。额外的要求是它应该可
有没有办法获取 RegInit 的重置值?通过探测产生的类型的成员?我可以看到 RegInit 将返回类型(例如 UInt )。例如,我将有一个寄存器,我想通过 regmap 对其进行控制。 val
Iv 目前接手了一个项目,其中开发人员在某些表的 json 数组列中存储了 has many 关系。 产品表 ---------------------------- id | product | c
Git 会在任何地方记录推送到远程的历史吗? 我注意到我们能够在 Microsoft VSTS 中查看 Git 存储库的推送历史记录以及每次推送的相关提交。它甚至显示旧的、过时的提交,由于后来的强制推
我是一名优秀的程序员,十分优秀!