如何与枪支一起返回JPG / JPEG
我可以用Gunicorn返回HTML文件,但是我正在尝试返回< img>
标签中引用的图像文件。该图像没有在浏览器中渲染,实际上,我发现在浏览器工具中以供图像返回响应。
这是我的枪支代码:
def render_jpeg(filename):
encoded_string = ""
with open(filename, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string
def app(environ, start_response):
path = environ.get("PATH_INFO")
data = ""
encode = "utf-8"
content_type = 'text/html'
if path.endswith(".jpeg"):
content_type = 'image/jpeg'
encode = "byte"
data = render_jpeg(path)
else:
context = {"path": path, "pageTitle": "home"}
data = template("template.html", context)
if encode.endswith("utf-8"):
data = data.encode(encode)
status = '200 OK'
response_headers = [
('Content-type', content_type),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
这是我的命令
gunicorn -workers = 2 test:app-reaload -log-file log.txt -log-log-level'debug'
这是我的日志输出
[2022-07-08 14:28:21 -0400] [91974] [INFO] Starting gunicorn 20.1.0
[2022-07-08 14:28:21 -0400] [91974] [DEBUG] Arbiter booted
[2022-07-08 14:28:21 -0400] [91974] [INFO] Listening at: http://127.0.0.1:8000 (91974)
[2022-07-08 14:28:21 -0400] [91974] [INFO] Using worker: sync
[2022-07-08 14:28:21 -0400] [91976] [INFO] Booting worker with pid: 91976
[2022-07-08 14:28:21 -0400] [91977] [INFO] Booting worker with pid: 91977
[2022-07-08 14:28:21 -0400] [91974] [DEBUG] 2 workers
[2022-07-08 14:28:47 -0400] [91977] [DEBUG] GET /media/zombie1.jpeg
[2022-07-08 14:28:47 -0400] [91977] [DEBUG] GET /favicon.ico
[2022-07-08 14:30:33 -0400] [91974] [INFO] Handling signal: int
[2022-07-08 14:30:33 -0400] [91976] [INFO] Worker exiting (pid: 91976)
[2022-07-08 14:30:33 -0400] [91977] [INFO] Worker exiting (pid: 91977)
[2022-07-08 14:30:33 -0400] [91974] [INFO] Shutting down: Master
我要去哪里?
I can return an html file with gunicorn, but I am trying to return an image file being referenced in an <img>
tag. The image is not rendering in the browser, and in fact I see no response being returned in the browser tools for the image.
This is my gunicorn code:
def render_jpeg(filename):
encoded_string = ""
with open(filename, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string
def app(environ, start_response):
path = environ.get("PATH_INFO")
data = ""
encode = "utf-8"
content_type = 'text/html'
if path.endswith(".jpeg"):
content_type = 'image/jpeg'
encode = "byte"
data = render_jpeg(path)
else:
context = {"path": path, "pageTitle": "home"}
data = template("template.html", context)
if encode.endswith("utf-8"):
data = data.encode(encode)
status = '200 OK'
response_headers = [
('Content-type', content_type),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
This is my command
gunicorn --workers=2 test:app --reload --log-file log.txt --log-level 'debug'
This is my log output
[2022-07-08 14:28:21 -0400] [91974] [INFO] Starting gunicorn 20.1.0
[2022-07-08 14:28:21 -0400] [91974] [DEBUG] Arbiter booted
[2022-07-08 14:28:21 -0400] [91974] [INFO] Listening at: http://127.0.0.1:8000 (91974)
[2022-07-08 14:28:21 -0400] [91974] [INFO] Using worker: sync
[2022-07-08 14:28:21 -0400] [91976] [INFO] Booting worker with pid: 91976
[2022-07-08 14:28:21 -0400] [91977] [INFO] Booting worker with pid: 91977
[2022-07-08 14:28:21 -0400] [91974] [DEBUG] 2 workers
[2022-07-08 14:28:47 -0400] [91977] [DEBUG] GET /media/zombie1.jpeg
[2022-07-08 14:28:47 -0400] [91977] [DEBUG] GET /favicon.ico
[2022-07-08 14:30:33 -0400] [91974] [INFO] Handling signal: int
[2022-07-08 14:30:33 -0400] [91976] [INFO] Worker exiting (pid: 91976)
[2022-07-08 14:30:33 -0400] [91977] [INFO] Worker exiting (pid: 91977)
[2022-07-08 14:30:33 -0400] [91974] [INFO] Shutting down: Master
Where am I going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@triplee所指出的那样,我的问题是Base64编码。无需编码!更正的代码:
As @tripleee noted, my issue was the base64 encoding. No encoding required! Corrected code: