How to use options of wget and curl in Python to download a file?

Viewed 603

I would like yo use this Adyen request

$ wget --http-user='[YourReportUser]@Company.[YourCompanyAccount]' --http-password='[YourReportUserPassword]' --quiet --no-check-certificate https://ca-test.adyen.com/reports/download/MerchantAccount/[YourMerchantAccount]/[ReportFileName]

in Python to download a file. How to put the options of the wget into a request in urllib2 or requests ?

Many thanks,

1 Answers

Requests makes this fairly easy:

import requests

r = requests.get('https://ca-test.adyen.com/reports/download/MerchantAccount/[YourMerchantAccount]/[ReportFileName]', auth=('[YourReportUser]@Company.[YourCompanyAccount]', '[YourReportUserPassword]'), verify=False)
r.raise_for_status() #fail here if we got something other than 200
#for binary payloads:
with f as open('my file.bin', 'wb'):
    f.write(r.content)
#or for text:
with f as open('my file.txt', 'wt'):
    f.write(r.text)

This assumes your endpoint is using Basic Auth. If it's Digest Auth, change to:

r = requests.get('https://ca-test.adyen.com/reports/download/MerchantAccount/[YourMerchantAccount]/[ReportFileName]', auth=requests.HTTPDigestAuth('[YourReportUser]@Company.[YourCompanyAccount]', '[YourReportUserPassword]'), verify=False)

Note the verify=False parameter to tell Requests to not check the TLS certificate. You can also set verify=/path/to/certfile if you have a certificate you need to use for verification. See https://requests.readthedocs.io/en/master/user/advanced/#ssl-cert-verification for details.

The Requests documentation is excellent: https://requests.readthedocs.io/en/master/

Related