Problems displaying image via http using Python

I am trying to download an image file from a local drive and output it via http when a script is available on my web server using cgi.

For example, I want http://example.com/getimage.py?imageid=foo to return the corresponding image. However, only opening the file and printing imgfile.read () after the corresponding content headers causes the image to be scrambled. Obviously, the solution is wrong, but I have no idea why.

What would be the best way to do this using the 2.6 built-in modules?

Edit:

The code essentially boils down to the following:

imgFile=open(fileName,"rb")
print "Content-type: image/jpeg"
print
print imgFile.read()
imgFile.close()

It displays an image, only one that seems random.

+3
source share
2

, () :

imgfile = open("%s.png" % imageid, "rb")
print imgfile.read()

a >

Windows stdout :

import sys
if sys.platform == "win32":
    import os, msvcrt
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)

CR + LF (. RFC 2616 6):

imgFile = open(fileName, "rb")
sys.stdout.write("Content-type: image/jpeg\r\n")
sys.stdout.write("\r\n")
sys.stdout.write(imgFile.read())
imgFile.close()
+3

Python HTTP- SimpleHTTPServer - SimpleHTTPServer.py /Lib Python , ?

0

Source: https://habr.com/ru/post/1774763/


All Articles