[Tutor] File upload from python shell

Luke Paireepinart rabidpoobear at gmail.com
Sun Oct 14 19:17:07 CEST 2007


Paulino wrote:
> Hello!
>
>
> How can I upload a file from python?
>
> If it is a form to fill with values it's simple:
>
>
> urlopen("http://site.com/action?key1=value1;key2=value2") and I get the 
> form filled.
>   
Only if the form uses the GET method.
If your file upload form used GET, you could upload your file like this 
as well (after base-64 encoding it.)
I recommend reading the Wikipedia articles on GET and POST so you can 
know the difference.
>
> What about uploading a file programmaticaly?
>   
file uploads usually use the POST method, not GET.
they're generally the same thing as text or radiobutton form entries, 
except their input type is set to file,
so that it's base-64 encoded so that there's no errors in transmission.
basically you'll have to look at a few things:
1. how the POST method is structured.
2. Which keys your file-uploading page wants.
3. how to base-64 encode your file.
4. how to measure the length of all your keys and data so that you can 
set content-length header correctly.
5. how to submit this new http request properly.

What are you trying to upload to?
if it's ImageShack by any coincidence, I have some code that does that 
already.
If it's something else, the code would probably be similar.
Let me know if you want to take a look at that.
-Luke


More information about the Tutor mailing list