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 selenium.webdriver.support.ui 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 @id='login-submit']").click()

        wait = WebDriverWait(self.driver, 20)
        wait.until(lambda driver: self.driver.title.startswith('Just Cloud'))

        except NoSuchElementException:
            print "HURDLE dialog did not appear"

        url = "https://upload.backupgrid.net/add"
        fileToUpload = {'file':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
        print fileToUpload 
        s = requests.Session()
        s.auth = ('test_usrname', 'test_pswd')
        response = s.post(url, 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.

  • You said what expected result should be, but what is the actual result? What error, stacktrace or output you receive?
    – dzieciou
    Commented Aug 25, 2013 at 15:41
  • @dzieciou: Sorry, I do not get any error but the file is not uploaded. Here is the Console output: {'file': <open file '/home/pinku/Desktop/Test_Upload.odt', mode 'rb' at 0x9267b20>} <requests.packages.urllib3.response.HTTPResponse object at 0x929d5cc> Commented Aug 25, 2013 at 15:43
  • Hey Praveen, your question about the Python requests module isn't QA-related. If I were you, I would narrow down your code fragment to just the requests part, and then post it on StackOverflow.
    – user246
    Commented Aug 26, 2013 at 13:06
  • @user246: but isnt it related to automation QA? That was the thought which brought me here. Nevermind... Let me give you the code excerpt that I have tried. url = "https://upload.backupgrid.net/add" fileToUpload = {'file':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')} print fileToUpload s = requests.Session() s.auth = ('test_usrname', 'test_pswd') response = s.post(url, files=fileToUpload) print response.text Commented Aug 27, 2013 at 12:30
  • Let's continue this in our chat thread at chat.stackexchange.com/rooms/10306/…
    – user246
    Commented Aug 27, 2013 at 13:02

1 Answer 1


@Praveen: 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: http://fiddler2.com/)
  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 = 'https://server.com'
credential = {'username':'user1', 'password':'pass', 'logonBtn':'1'}
#authenticate first
resp = session.post(auth_url, data=credential)
resp.raise_for_status() # -> make sure it is 200

# do your file uploading code here...
resp = session.post(upload_url, files=file_to_upload)
resp.raise_for_status() # -> check it is 200 returned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.