How to use FTP with Pythons requests

2024/9/21 21:17:35

Is it possible to use the requests module to interact with a FTP site? requests is very convenient for getting HTTP pages, but I seem to get a Schema error when I attempt to use FTP sites.

Is there something that I am missing in requests that allows me to do FTP requests, or is it not supported?

Answer

For anyone who comes to this answer, as I did, can use this answer of another question in SO.

It uses the urllib.request method.

The snippet for a simple request is the following (in Python 3):

import shutil
import urllib.request as request
from contextlib import closing
from urllib.error import URLErrorurl = "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}"')
https://en.xdnf.cn/q/71904.html

Related Q&A

How to find a best fit distribution function for a list of data?

I am aware of many probabilistic functions builted-in Python, with the random module. Id like to know if, given a list of floats, it would be possible to find the distribution equation that best fits t…

How to use peewee limit()?

With Peewee Im trying to use limit as follows:one_ticket = Ticket.select().limit(1) print one_ticket.count()This prints out 5 however. Does anybody know whats wrong here?

Python (1..n) syntax?

I see in the code on this Sage wiki page the following code:@interact def _(order=(1..12)):Is this (1..n) syntax unique to Sage or is it something in Python? Also, what does it do?

in python , How to load just one time a ML model in a rest service like django or flask?

I have a ML model saved in a pkl (pickel file), I have no problem loading this model and using it for prediction, even I have a rest service that expose it, the only problem is that I load the model i…

Adding a Title or Text to a Folium Map

Im wondering if theres a way to add a title or text on a folium map in python?I have 8 maps to show and I want the user to know which map theyre looking at without having to click on a marker. I attem…

Zombie SharedDataMiddleware on Python Heroku

Im setting up a Flask app on Heroku. Everything is working fine until I added static files. Im using this:from werkzeug import SharedDataMiddleware app = Flask(__name__) app.wsgi_app = SharedDataMiddle…

Python: Urllib.urlopen nonnumeric port

for the following codetheurl = "https://%s:%[email protected]/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG" % (username, password, hostname, theip)conn…

How to unittest the sequence of function calls made inside a python fuction?

I would like to unittest a fuction and assert if the sequence of function calls made inside the function workflow(). Something like,[1st called] fetch_yeargroup_ls()[2nd called] invoke_get_links().....…

OpenCV: Extract SURF Features from user-defined keypoints

I want to compute SURF Features from keypoints that I specify. I am using the Python wrapper of OpenCV. The following is the code I am trying to use, but I cannot find a working example anywhere.surf…

Realtime processing and callbacks with Python and C++

I need to write code to do some realtime processing that is fairly computationally complex. I would like to create some Python classes to manage all my scripting, and leave the intensive parts of the a…