0

I am trying to upload a video through the django admin site using python requests. I am logging in just fine:

import requests

login_url = "http://127.0.0.1:8000/admin"
client = requests.client()
csrftoken = client.cookies['csrftoken']

login_data = {'username': 'username', 'password': 'password',
              'this_is_the_login_form': '1',
              'csrfmiddlewaretoken': csrftoken
             }

r = client.post(login_url, data=login_data)

After logging in I pull the page where I will upload a (video) file:

target_url = "http://127.0.0.1:8000/admin/product/700/"
r = client.get(target_url)

In the HTML I have a script(That is not being pulled in with the .get() request) to listen when a file has been uploaded:

var object_id = 700;
$(document).ready(function() {
    // Upload button handler
    $('.file').on('change', handleFileSelect);
    ...

I have tried setting the file to the object I'm trying to upload, and making another get request hoping the file argument will be set, thus make the script fire and start the upload process.

<input id="file" class="file" type="file" name="file">


data = {'file': open('file.flv', 'rb')}
r = client.get(target_url, data=data)

How do I make a request to the target_url, pull down the upload script, and make the upload script fire in order to upload the file?

1 Answer 1

1

The upload script runs in Javascript and it needs a browser (client-side). Since you are doing the process from python (server-side), the Javascript won't run.

What I'd do is to use Firebug in the browser, and inspect the request that is sent when you actually upload the file (also reading the Javascript function may help understanding what a client browser normally does in order to upload that file).

You then can use python-requests to do an equivalent POST from python (including all necessary parameters). Note, however, that since you are sending a file, you may need to post a "multipart-encoded" file: http://www.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file .

Sign up to request clarification or add additional context in comments.

4 Comments

How harder do you think it'll be to change the contents of the requested page and opening it up with a file browser e.g. webbrowser.open_new_tab('/path/to/page/') ?
Wait never mind on that last attempt, that won't work either since I don't have the js code. The multipart-encoding seems like the best bet. Thanks!
webbrowser simply opens the system browser. You'd still need to hack around to set appropriate cookies on it, you have no easy way to control the response or to fire events on the the browser, and it would require a graphical environment. I usually think that automating things at this level is weaker and harder than doing it at HTTP request level. In both cases, your code will break if the application changes. Imho, you're better off at http request level.
Seems to me that unless the application requests are too difficult to understand/reproduce, you are on the right path with python-requests.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.