简体   繁体   中英

Convert this curl command to Python 3

How can I send this curl out using Python? I did find similar requests, but cannot adapt my code to suite the query I have also tried using pycurl , following this example but without luck.

$ curl -v -X GET  -H "Accept: text/csv" -H "Authorization: Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==" "https://epc.opendatacommunities.org/api/v1/domestic/search"

Thanks

If you are using the Python Requests package the following code snippet should work:

import requests

headers = {
    'Accept': 'text/csv',
    'Authorization': 'Basic YW5kcmVhLmJvdHRpQHdzcC5jb206OWY5N2E5YTY2ZWU1MTMxZjdmNjk4MDcwZTFkODEwMjU0M2I0NTg1ZA==',
}

response = requests.get('https://epc.opendatacommunities.org/api/v1/domestic/search', headers=headers)

response.status_code  # 200
response.text # "lmk-key,address1,address2,address3,postcode,buildi ..."

(Note: I used the https://curl.trillworks.com/ website to automatically make the conversion)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM