是否可以使用requests
模块与FTP站点进行交互?requests
非常方便地获取HTTP页面,但是当我尝试使用FTP站点时似乎会出现模式错误。
requests
是否有一些我所不知道的功能来允许我进行FTP请求,或者它不支持FTP?
是否可以使用requests
模块与FTP站点进行交互?requests
非常方便地获取HTTP页面,但是当我尝试使用FTP站点时似乎会出现模式错误。
requests
是否有一些我所不知道的功能来允许我进行FTP请求,或者它不支持FTP?
对于像我一样查看这个答案的人,可以使用SO的另一个问题的此答案。
它使用了urllib.request方法。
一个简单请求的片段如下(使用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}"')