How to read a jpg bytearray from a Flash AS3 file

R

rsgalloway

I'm trying to save an image from a Flash AS3 to my server as a jpg
file. I found some PHP code to do this, but I want to do this in
Python. I'm not quite sure how to convert the following code to
Python. It's mainly the $GLOBALS["HTTP_RAW_POST_DATA"] part I don't
know how to convert.

<?php

if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {

// get bytearray
$im = $GLOBALS["HTTP_RAW_POST_DATA"];

// save image
$f = fopen($_GET['name'], 'wb');
fwrite($f, $jpg);
fclose($f);

} else echo 'An error occured.';

?>

source:

http://designreviver.com/tutorials/actionscript-3-jpeg-encoder-revealed-saving-images-from-flash/
 
F

Fredrik Lundh

I'm trying to save an image from a Flash AS3 to my server as a jpg
file. I found some PHP code to do this, but I want to do this in
Python. I'm not quite sure how to convert the following code to
Python. It's mainly the $GLOBALS["HTTP_RAW_POST_DATA"] part I don't
know how to convert.

depends on what framework you're using. if you're using plain CGI, you
should be able to read the posted data from sys.stdin:

import sys

im = sys.stdin.read()

f = open(name, 'wb')
f.write(jpg)
f.close()

to make your code a bit more robust, you may want to check the
content-length before doing the read, e.g.

import os

if os.environ.get("REQUEST_METHOD") != "POST":
... report invalid request ...

bytes = int(os.environ.get("CONTENT_LENGTH", 0))
if bytes > MAX_REQUEST_SIZE:
... report request too large ...

im = sys.stdin.read(bytes)

to deal with query parameters etc, see

http://docs.python.org/lib/module-cgi.html

</F>
 
R

rsgalloway

Thanks! I'm using form = cgi.FieldStorage(). When I print out the
contents of form, I get this:

FieldStorage(None, None, '\xff\xd8\xff\xe0\x00\x10JFIF
\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb
\x00\x84\x00\x05\x03\x04\x04\x04\x03\x05\x04\x04\x04\x05\x05\x05\x06\x07\x0c
\x08\x07\x07\x07\x07\x0f\x0b\x0b\t\x0c\x11\x0f\x12\x12\x11\x0f
\x11\x11\x13\x16\x1c\x17\x13\x14\x1a\x15\x11\x11\x18!\x18\x1a\x1d\x1d
\x1f\x1f\x1f\x13\x17"$"\x1e$\x1c\x1e\x1f\x1e
\x01\x05\x05\x05\x07\x06\x07\x0e\x08\x08\x0e\x1e\x14\x11\x14\x1e\x1e
\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e
\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e
\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\xff\.....

Which is obviously the binary data of the image I want. How do I
access this data? I'm used to getting it like this:

name = form['name'].value

But I don't think this will work in this case. TIA.
 
T

Tim Roberts

Thanks! I'm using form = cgi.FieldStorage(). When I print out the
contents of form, I get this:

FieldStorage(None, None, '\xff\xd8\xff\xe0\x00\x10JFIF
\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb
\x00\x84\x00\x05\x03\x04\x04\x04\x03\x05\x04\x04\x04\x05\x05\x05\x06\x07\x0c
\x08\x07\x07\x07\x07\x0f\x0b\x0b\t\x0c\x11\x0f\x12\x12\x11\x0f
\x11\x11\x13\x16\x1c\x17\x13\x14\x1a\x15\x11\x11\x18!\x18\x1a\x1d\x1d
\x1f\x1f\x1f\x13\x17"$"\x1e$\x1c\x1e\x1f\x1e
\x01\x05\x05\x05\x07\x06\x07\x0e\x08\x08\x0e\x1e\x14\x11\x14\x1e\x1e
\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e
\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e
\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\x1e\xff\.....

Which is obviously the binary data of the image I want. How do I
access this data? I'm used to getting it like this:

name = form['name'].value

But I don't think this will work in this case. TIA.

What led you to ask that here, instead of taking 60 seconds to load cgi.py
in an editor and search for the FieldStorage class?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top