0

I am badly stuck up and I'm not able to proceed any further. Trust me, I have looked all over the web to get a CONCRETE solution but all in vain! I have an application justcloud.com where I need to upload a file and verify if it's uploaded. After I login to this application and reach the page where I have the button to select a file to be uploaded, here is my code:

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

register_openers()
fileToUpload = {'files[]':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
datagen, headers = multipart_encode(fileToUpload)
url = "https://upload.backupgrid.net/add"
request = urllib2.Request(url, datagen, headers)
print urllib2.urlopen(request).read()

The error I keep getting every single time is [{"error":true,"error_msg":"Authentication Failed"}]

I know I just need to simulate the actual file upload process which is nothing but an HTTP POST request to the server which also includes some authentication that I need to overcome.

My assumption is that may be cookies can help me in resolving this authentication issue but I am not sure and I do not know how to include it in my python code. Any sample code will be helpful. I request anyone reading this to help me.

This is not my first time on stackoverflow when I have posted this question but I have not received much help. I am still giving it a shot. Thanks anyways...

Praveen Pandey
  • 658
  • 3
  • 16
  • 32
  • 1
    Why have you tagged this post with "selenium" and "webdriver"? You are n't using either – aychedee Sep 07 '13 at 09:30
  • Though I know this problem can be sorted out with python, I just wanted to make sure that anyone who has the same problem can find this question easily... I may sound dumb but that's exactly what I thought.. – Praveen Pandey Sep 07 '13 at 10:36

1 Answers1

0

The error message is telling you exactly what the problem is. You need to be logged into that website before you can POST a file to that URL.

You need to get a logged in session cookie somehow. Typically when you POST to a login form URL you will receive an http response that includes this cookie. Then every time you make another request you keep sending that cookie with each request so that the site is aware you are authenticated.

How would you login to backupgrid? Find the URL for that, login, grab the cookie from the response and then include it with your request.

This stackoverflow answer provides a good explanation of how to add cookies to the request.

Community
  • 1
  • 1
aychedee
  • 24,871
  • 8
  • 79
  • 83
  • Thanks for your response! I am a little naive when it comes to cookies. That's the reason why I mentioned in my question above that cookies may be the answer to resolve my authentication problem. It's just that I do not know how to implement them. Moreover, here are my doubts from your post above: I do not know how to login to backupgrid. I found this URL from the DOM `` – Praveen Pandey Sep 07 '13 at 11:04
  • How do I find out the username and password of the backupgrid URL? – Praveen Pandey Sep 07 '13 at 11:13
  • Umm... I guess you create an account? I'm really confused about what you are trying to do now. Why are you uploading a file to a place that you don't know anything about? – aychedee Sep 07 '13 at 11:29
  • I have a site called justcloud.com. I have an account in it say with username = u and password = p. I log into this site and navigate to the page where I have a button named 'Select Files' to upload files. The backupgrid URL that you saw in the above code is perhaps the URL of the remote server. This is the place where I need to upload my file is what I assume. But how will I know what is the username or password to this server? – Praveen Pandey Sep 07 '13 at 11:41
  • Using requests module,here's a sample code & here's what it prints on the console: `import requests url = "https://upload.backupgrid.net/add" fileToUpload = {'file':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')} res = requests.post(url, fileToUpload) print res.cookies` The output in the console is **<[, ]>** I see 2 cookies.Rite? Which one should I pick? – Praveen Pandey Sep 07 '13 at 11:47
  • Okay, that makes a bit more sense. I think you might want to try using Selenium Webdriver to just drive a browser directly. I think trying to create a request like this is just going to be too complicated. – aychedee Sep 07 '13 at 19:53
  • Sorry but what do you mean when you say _try using Selenium Webdriver to just drive a browser directly_ ? What I understand now is that selenium webdriver is no where related to my situation here. I hope you would not mind reiterating what you meant... – Praveen Pandey Sep 08 '13 at 05:29
  • You can use webdriver to directly control a browser. Navigate it to the page you want, and hit the upload button. – aychedee Sep 08 '13 at 16:34
  • I have already tried this method. The problem is selenium webdriver cannot click on the button since it is an input tag with type attribute value as 'file'. Moreover, the file upload window that opens up is the native OS window which cannot be handled by selenium webdriver. Hence the only option I have is to simulate the HTTP POST method. Thoughts? – Praveen Pandey Sep 08 '13 at 17:06