Mar-02-2021, 03:28 PM
(Mar-01-2021, 05:07 PM)JohnnyCoffee Wrote:(Mar-01-2021, 03:48 PM)ndc85430 Wrote: You haven't really said what the problem is with that code. All that does is read the bytes. Does it fail in some way? Is something else not working? You don't show what you do after the read, so it's pretty hard to help. Don't make us guess!
I didn't get it right, but that's okay, follow the complete code:
123456789101112131415161718192021222324from
wsgiref.simple_server
import
make_server
def
hello_world_app(environ, start_response):
o_file
=
open
(
"cloud.png"
,
"rb"
)
o_file
=
o_file.read()
status
=
'200 OK'
# HTTP Status
headers
=
[(
"Content-type"
,
"image/png; charset=utf-8"
)]
start_response(status, headers)
# The returned object is going to be printed
return
[
str
(o_file).encode(
"utf-8"
)]
with make_server('',
8000
, hello_world_app) as httpd:
# Function -> Imprime no terminal reference
(
'Running Kosmos Application\n'
'Browser Access - http://127.0.0.1:8000\n'
'Crl+c for flow command or Crl+z for stop'
)
# Serve until process is killed
httpd.serve_forever()
You seem to be severely confused about what character encodings are useful for.
Probably read Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) before you try to read the rest of this answer.
You are replacing the binary PNG data with mojibake when you encode it. You should not be manipulating the data at all; just read it into a buffer, and send it to the client. End of story.
1 2 3 4 5 6 7 8 9 |
def hello_world_app(environ, start_response): with open ( "cloud.png" , "rb" ) as i_file: o_file = i_file.read() status = '200 OK' # HTTP Status headers = [( "Content-type" , "image/png" )] # no ; charset here! start_response(status, headers) return [o_file] |