Cannot upload a file using requests module in python

  • After two days of continuous trial, I thought it's better to ask you folks here. I want to upload a file on my desktop to a remote server. For this I am making use of python and its module requests. I get good help from the internet but even then when I execute my piece of code, I am not able to upload it. So here is the code I have written:

    import unittest
    from Browser import Browser
    from selenium.common.exceptions import NoSuchElementException
    from import WebDriverWait
    import requests
    class Upload(unittest.TestCase):
        def setUp(self):
            print "Setting up before test case execution..."
            self.driver = Browser().initialiseBrowser()
        def test_Upload(self):
            self.driver.find_element_by_xpath("//input[@value='Login' and @rielyn 
            wait = WebDriverWait(self.driver, 20)
            wait.until(lambda driver: self.driver.title.startswith('Just Cloud'))
            except NoSuchElementException:
                print "HURDLE dialog did not appear"
            url = ""
            fileToUpload = {'file':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
            print fileToUpload 
            s = requests.Session()
            s.auth = ('test_usrname', 'test_pswd')
            response =, files=fileToUpload)
            print response.text
        def tearDown(self):

    Please help me point out the mistake I might have made in the code above. 2 days back I had absolutely no clue of the approach to upload a file. I am sure I am very near to this solution. So I request you guys to help me get it. Trust me, I have put a lot of effort to get this done.

  • @rielyn your problem lies with the way you have authenticated to the website. You cannot just put the s.auth in request and hope it will work. The website probably is not using HTTP-Basic Auth.

    This is how you should do it (on windows machine):

    1. List item
    2. install a HTTP/Web tracer (Fiddler is available here:
    3. The site is in HTTPS, so you have enable Fiddler to be the proxy to capture the traffic
    4. do the test manually on IE
    5. save the trace
    6. write your python code to mimic the browser call especially the authentication part.

    It should look like this:

    session = requests.Session()
    auth_url = ''
    credential = {'username':'user1', 'password':'pass', 'logonBtn':'1'}
    #authenticate first
    resp =, data=credential)
    resp.raise_for_status() # -> make sure it is 200
    # do your file uploading code here...
    resp =, files=file_to_upload)
    resp.raise_for_status() # -> check it is 200 returned

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2