File upload from client application (non-form based upload)

Discussion in 'Python' started by stuart@microsoft.com, Nov 22, 2006.

  1. Guest

    Hi

    I'm trying to write a Python script to receive and save a file on a web
    server that has been POST'ed from a client application.

    In essence, this is similar to handling a file upload from an HTML
    form. However, I can't use:

    form = cgi.FieldStorage()
    fileitem = form['file']

    since the file is not coming from a form, and hence I don't have a form
    field called 'file'.

    I have working server-side code in PHP to do this (error handling
    removed):

    $file = "./test.jpg";
    $file_handle = fopen($file,"w");
    $mydata = file_get_contents("php://input");
    fwrite($file_handle, $mydata);
    fclose($file_handle);

    What I need is a Python equivalent of the the above PHP script. The
    content-type in the POST header is currently set to
    "application/octet-stream" which works fine with the php code above.

    Any help, advise, pointers, sample code would be hugely welcome,

    Many thanks in advance,

    Stuart
     
    , Nov 22, 2006
    #1
    1. Advertising

  2. At Wednesday 22/11/2006 09:08, wrote:

    >I'm trying to write a Python script to receive and save a file on a web
    >server that has been POST'ed from a client application.
    >
    >In essence, this is similar to handling a file upload from an HTML
    >form. However, I can't use:
    >
    >form = cgi.FieldStorage()
    >fileitem = form['file']
    >
    >since the file is not coming from a form, and hence I don't have a form
    >field called 'file'.
    >
    >I have working server-side code in PHP to do this (error handling
    >removed):
    >
    >$file = "./test.jpg";
    >$file_handle = fopen($file,"w");
    >$mydata = file_get_contents("php://input");
    >fwrite($file_handle, $mydata);
    >fclose($file_handle);
    >
    >What I need is a Python equivalent of the the above PHP script. The
    >content-type in the POST header is currently set to
    >"application/octet-stream" which works fine with the php code above.


    You got rather close... "file" is not an item, it's an attribute, a
    file-like object already open:

    form = cgi.FieldStorage()
    fileitem = form.file
    data = fileitem.read()


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
     
    Gabriel Genellina, Nov 25, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DiscoStu
    Replies:
    1
    Views:
    577
  2. Faisal
    Replies:
    0
    Views:
    2,660
    Faisal
    Mar 16, 2006
  3. sreenivasan alakappan

    non-form based databse application

    sreenivasan alakappan, Apr 2, 2004, in forum: C++
    Replies:
    6
    Views:
    345
    Christopher Benson-Manica
    Apr 8, 2004
  4. david wolf
    Replies:
    6
    Views:
    596
    Babu Kalakrishnan
    Sep 25, 2006
  5. kitchai yong via .NET 247

    authenticate win32 form client with form based authentication web services

    kitchai yong via .NET 247, May 13, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    228
    Jan Tielens
    May 13, 2004
Loading...

Share This Page