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 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()
            self.driver.maximize_window()
    
        def test_Upload(self):
            self.driver.get("http://www.justcloud.com")
            self.driver.find_element_by_link_text("Login").click()
            self.driver.find_element_by_css_selector("input[name='email'][id='login-email']").send_keys("test_usrname")
            self.driver.find_element_by_css_selector("input[name='password'][id='login-password']").send_keys("test_pswd")
            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'))
    
            try:
                self.driver.find_element_by_xpath("//div[@id='not-installed-container']//span[text()='X']").click()
            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):
            driver.close()
    

    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: 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
    


Suggested Topics

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