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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stuart@microsoft.com

    #1

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

    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.FieldStorag e()
    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_conten ts("php://input");
    fwrite($file_ha ndle, $mydata);
    fclose($file_ha ndle);

    What I need is a Python equivalent of the the above PHP script. The
    content-type in the POST header is currently set to
    "applicatio n/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

  • Gabriel Genellina

    #2
    Re: File upload from client application (non-form based upload)

    At Wednesday 22/11/2006 09:08, stuart@microsof t.com 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.FieldStorag e()
    >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_conten ts("php://input");
    >fwrite($file_h andle, $mydata);
    >fclose($file_h andle);
    >
    >What I need is a Python equivalent of the the above PHP script. The
    >content-type in the POST header is currently set to
    >"applicatio n/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.FieldStorag e()
    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

    Comment

    Working...