[Tutor] File upload from python shell

Paulino paulino1 at sapo.pt
Mon Oct 15 00:51:38 CEST 2007


Luke Paireepinart escreveu:
> 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
>
>
I want to upload pdf files, to an intranet server, that I also control.

Yes I think it would be very usefull to have a look at your code.

The urlopen function can take an optional data parameter that is used to 
specify a POST request, but i couldn't find information about how to set 
the data parameter properly.

Thank You
Paulino


More information about the Tutor mailing list