<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 27-Aug-2010, at 1:32 AM, Dave Angel wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Navkirat Singh wrote:<br><blockquote type="cite">Hey guys,<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">I am programming a webserver, I receive a jpeg file with the POST method.The file (.jpeg) is encoded in bytes, I parse the bytes by decoding them to a string. I wanted to know how i could write the file (now a string) as a jpeg image on disk. When I try to encode the same string to a bytes and write them in binary format to disk, the file is not recognized as jpeg. I would be grateful if someone could help me with this.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite">Regards,<br></blockquote><blockquote type="cite">Nav <br></blockquote>If by "decoding them to a string" you mean converting to Unicode, then you've already trashed the data. That's only valid if the bytes had been encoded from valid Unicode characters, and then only if you use the corresponding decoding technique.<br><br>If you mean some other decoding, then the question is meaningless without telling us just what the decoding is, preferably with some code.<br><br>It also might be useful to know what version of Python you're using, when you post the code.<br><br>DaveA<br><br></div></blockquote></div><br><div>Dave,</div><div><br></div><div>I am using Python3 and I receive a byte stream with a jpeg attached sent by the web browser over a socket, which looks like this:</div><div><br></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; ">b': image/jpeg\r\nAccept: text/*\r\nReferer: <a href="http://127.0.0.1:8001/\r\nAccept-Language:">http://127.0.0.1:8001/\r\nAccept-Language:</a> en-us\r\nAccept-Encoding: gzip, deflate\r\nContent-Length: 91783\r\nConnection: keep-alive\r\n\r\n\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00\x84\x00\x03\x02\x02\x03\x02\x02\x03\x03\x03\x03\x04\x03\x03\x04\x05\x08\x05\x05\x04\x04\x05\n\x07\x07\x06\x08\x0c\n\x0c\x0c\x0b\n\x0b\x0b\r\x0e\x12\x10\r\x0e\x11\x0e\x0b\x0b\x10\x16\x10\x11\x13\x14\x15\x15\x15\x0c\x0f</span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; "><br></span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; ">From the above, I need to:</span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; "><br></span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; ">a) Split the header content from the image content, which comes after the keep-alive\r\n\r\n part</span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; "><br></span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; ">b) Then write the image content to file for further use as a jpeg.</span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; "><br></span></div><div><span class="Apple-style-span" style="font-family: LuxiMono, 'Bitstream Vera Sans Mono', Monaco, 'Courier New', monospace; font-size: 12px; ">Nav</span></div></body></html>