简体   繁体   中英

Not able to upload a file through python

After several attempts and repeated failures, I am posting my code excerpt here. I keep getting Authentication failure. Can somebody point out what is it that I am doing wrong here?

import requests

fileToUpload = {'file': open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
res = requests.post('https://upload.backupgrid.net/add', fileToUpload)
print res.headers
cookie = {'PHPSESSID': 'tobfr5f31voqmtdul11nu6n9q1'}
requests.post('https://upload.backupgrid.net/add', cookie, fileToUpload)

By print res.headers, I get the following:

CaseInsensitiveDict({'content-length': '67', 'access-control-allow-methods': 'OPTIONS, HEAD, GET, POST, PUT, DELETE', 'x-content-type-options': 'nosniff', 'content-encoding': 'gzip', 'set-cookie': 'PHPSESSID=ou8eijalgpss204thu7ht532g1; path=/, B100Serverpoolcookie=4281246842.1.973348976.502419456; path=/', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'vary': 'Accept-Encoding', 'server': 'Apache/2.2.15 (CentOS)', 'pragma': 'no-cache', 'cache-control': 'no-store, no-cache, must-revalidate', 'date': 'Mon, 09 Sep 2013 09:13:08 GMT', 'access-control-allow-origin': '*', 'access-control-allow-headers': 'X-File-Name, X-File-Type, X-File-Size', 'content-type': 'text/html; charset=UTF-8'})

It contains the cookies also. Am I passing the cookies correctly? Please help!

You are not passing cookies correctly, should be:

requests.post('https://upload.backupgrid.net/add', 
              files=fileToUpload, 
              cookies=cookie)

See also documentation:

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