Python Localhost无法显示图像

Python Localhost无法显示图像,python,image,localhost,cgi-bin,Python,Image,Localhost,Cgi Bin,我试图在localhost上显示图像。作为第一步,我用python创建了一个服务器脚本 #!/usr/bin/env python import BaseHTTPServer import CGIHTTPServer import cgitb; cgitb.enable() ## This line enables CGI error reporting server = BaseHTTPServer.HTTPServer handler = CGIHTTPServer.CGIHTTPRe

我试图在
localhost
上显示图像。作为第一步,我用python创建了一个服务器脚本

#!/usr/bin/env python

import BaseHTTPServer
import CGIHTTPServer
import cgitb; cgitb.enable()  ## This line enables CGI error reporting

server = BaseHTTPServer.HTTPServer
handler = CGIHTTPServer.CGIHTTPRequestHandler
server_address = ("", 8000)
handler.cgi_directories = ["/"]

httpd = server(server_address, handler)
httpd.serve_forever()
该映像位于执行此脚本的同一目录中。 随后
http://localhost:8000/test.jpg
在浏览器中键入

浏览器无法渲染图像,出现错误:
图像”http://localhost:8000/test.jpg“无法显示,因为它包含错误。

服务器脚本抛出一个错误,如

  File "/usr/lib/python2.7/CGIHTTPServer.py", line 253, in run_cgi
    os.execve(scriptfile, args, env)
OSError: [Errno 8] Exec format error
我已经尝试过显示文本,服务器可以很好地使用很多示例。但它无法加载图像。我哪里做错了


问题解决了。我将test.jpg移动到
服务器目录

CGI用于执行服务器端脚本,而不是提供静态内容。因此,它将尝试执行正在提供的文件(在本例中,尝试执行映像):
os.execve(scriptfile,args,env)

执行相当于在shell中运行文件


用于静态内容,如图像。

您的代码正试图以cgi脚本的形式执行test.jpg。如果删除CGIHttpRequestHandler而改用SimpleHTTPServer.SimpleHTTPRequestHandler,您将恢复图像。如果两者都需要,则需要将图像放在其他位置。

我认为错误是因为您没有html页面设置

虽然python脚本正在本地主机上运行服务器,但您需要html/css页面
实际显示图片和文字。

我创建了一个html页面,并显示文本。test.jpg在本地,但不在浏览器上呈现。我不确定您到底想做什么,但要简单地显示图像,您必须使用图像标记将其嵌入html代码中。我肯定需要显示图像,以及html上的一些文本消息。图像是在执行python脚本后生成的,因此需要CGIHttpServer。如何处理这种情况?SimpleHTTP在浏览器上显示图像,但我需要CGIHttp第一手生成它。将CGI脚本放在子目录中并从那里提供服务?默认情况下,如果你把它放在cgi箱中,它会做你想做的事情。