是否可以使用requests
与 FTP 站点交互的模块?requests
获取 HTTP 页面非常方便,但当我尝试使用 FTP 站点时,我似乎遇到架构错误。
我有什么遗漏的吗requests
允许我执行 FTP 请求,还是不支持?
对于像我一样得到这个答案的人来说,可以使用这是另一个问题的答案 https://stackoverflow.com/a/11768443/8038345 in SO.
它使用urllib.请求 https://docs.python.org/3.7/library/urllib.request.html#module-urllib.request method.
简单请求的代码片段如下(在 Python 3 中):
import shutil
import urllib.request as request
from contextlib import closing
from urllib.error import URLError
url = "ftp://ftp.myurl.com"
try:
with closing(request.urlopen(url)) as r:
with open('file', 'wb') as f:
shutil.copyfileobj(r, f)
except URLError as e:
if e.reason.find('No such file or directory') >= 0:
raise Exception('FileNotFound')
else:
raise Exception(f'Something else happened. "{e.reason}"')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)