如何与枪支一起返回JPG / JPEG

发布于 2025-02-14 02:01:58 字数 2041 浏览 2 评论 0原文

我可以用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2025-02-21 02:02:00

正如@triplee所指出的那样,我的问题是Base64编码。无需编码!更正的代码:

def render_jpeg(filename):
    data = ""
    with open(filename, "rb") as image_file:
        data = image_file.read()
        return data

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 = ""
        filename = get_filename_from_path(path)
        data = render_jpeg(filename)
    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])

As @tripleee noted, my issue was the base64 encoding. No encoding required! Corrected code:

def render_jpeg(filename):
    data = ""
    with open(filename, "rb") as image_file:
        data = image_file.read()
        return data

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 = ""
        filename = get_filename_from_path(path)
        data = render_jpeg(filename)
    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])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文