Upload a large XML file with Python Requests library

前端 未结 3 866
猫巷女王i
猫巷女王i 2021-02-10 02:44

I\'m trying to replace curl with Python & the requests library. With curl, I can upload a single XML file to a REST server with the curl -T option. I have been unable to do

3条回答
  •  深忆病人
    2021-02-10 03:09

    To PUT large files, don't read them into memory. Simply pass the file as the data keyword:

    xmlfile = open('trb-1996-219.xml', 'rb')
    headers = {'content-type': 'application/xml'}
    r = requests.put(url, data=xmlfile, headers=headers, auth=HTTPDigestAuth("*", "*"))
    

    Moreover, you were opening the file as unicode (decoding it from UTF-8). As you'll be sending it to a remote server, you need raw bytes, not unicode values, and you should open the file as a binary instead.

提交回复
热议问题