momentan versuche ich die Beschaffung von Lieferanten-Dateien, zu automatisieren.
Mit FTP, konnte ich dies schon verwirklichen, was ja auch nicht all zu schwer ist.
Nun habe ich auch HTTP-Downloads, die ich bisher manuell mache.
Beim Googeln, habe ich zwei Möglichkeiten gefunden, die für die Umsetzung in Frage kommen.
Dies sind urllib3 und requests.
Was mir dabei noch nicht klar ist, wo die Unterschiede zwischen urllib3 und requests bestehen und welches vorzuziehen ist?
Mit urllib3, habe ich dies mal erstellt, was bei HTTP ohne Login auch funktioniert:
Code: Alles auswählen
def http_download(import_order):
"""
Stelle Verbindung zu HTTP-Server her, lade die Datei(en) herunter
und speichere diese im Download-Ordner.
"""
url, path, user, passwd, filename, coding, local_path = import_order
url_file = '{}{}'.format(url, filename)
if local_path[-1] != os.sep:
local_path = '{}{}'.format(local_path, os.sep)
local_file = '{}{}'.format(local_path, filename)
http = urllib3.PoolManager()
if (user, passwd) != ('', ''):
MB().ok('HTTP-Modul für Login noch nicht einsatzbereit!')
return
with http.request('GET', url_file, preload_content=False) as resp:
with open(local_file, 'wb') as out_file:
shutil.copyfileobj(resp, out_file)
resp.release_conn()
if os.path.isfile(os.path.abspath(local_file)):
return local_file
return False
import_order = ('https://www.XXXXXX.de/Dowloads/', '', '', '', 'MyFile.zip',
'', '/pfad/zu/download_path/')
http_download(import_order)
Der Download bei HTTP-Seiten ohne Login, funktioniert hier genauso wie mit obigem Code.
Bei HTTP-Seiten mit Login, habe ich Probleme, dort fehlt der Login fehl.
Code: Alles auswählen
def http_download_request(import_order):
"""
Stelle Verbindung zu HTTP-Server her, lade die Datei(en) herunter
und speicher diese im Ordner import_suppliers_lists.
"""
url, path, user, passwd, filename, coding, local_path = import_order
url_file = '{}{}'.format(url, filename)
if local_path[-1] != os.sep:
local_path = '{}{}'.format(local_path, os.sep)
if filename == '' or len(filename) == 1:
filename = heute
local_file = '{}{}'.format(local_path, filename)
with open(local_file, 'wb') as handle:
if (user, passwd) == ('', ''):
response = requests.get(url_file, stream=True)
else:
files = {'file': ('*.csv', path)}
response = requests.get(
url, auth=(user, passwd), files=files, stream=True)
if not response.ok:
# Something went wrong
print('Fehler: {}'.format(response.ok))
return
else:
print('Juhu: {}'.format(response.ok))
print(response.text)
return
for block in response.iter_content(1024):
handle.write(block)
if os.path.isfile(os.path.abspath(local_file)):
return local_file
return False
import_order = ('https://www.XXXXXX.de', '~/Dowloads/', 'myuser', 'mapasswd', 'MyFile.zip',
'', '/pfad/zu/download_path/')
http_download_request(import_order)
Welche Informationen sind dafür notwendig?
Grüße Nobuddy