[Tutor] File upload from python shell

Luke Paireepinart rabidpoobear at gmail.com
Mon Oct 15 03:57:03 CEST 2007


Paulino wrote:
> 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.
Well, give us more information on the server.
If you have control over it, why bother using HTTP?  Why not use FTP or SSH?
They'd have more flexibility and probably robustness as well.
Is the OS Windows or Linux or something else?
-Luke


More information about the Tutor mailing list