【问题标题】:HTTP Python - Image not loadingHTTP Python - 图像未加载
【发布时间】:2021-11-17 08:06:31
【问题描述】:

晚安!

我尝试了一些关于这个问题的谷歌搜索,但我没有找到任何能帮助我的东西,所以我在这里玩游戏。问题是:我想将图像加载到我的 HTTP 服务器,但它不能按预期工作。我的代码是:

from http.server import BaseHTTPRequestHandler, HTTPServer
from PIL import Image

im = Image.open(r"calculator.png")

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # processamento
        if self.path in ("/", "Aula12_HTML.html", "/Aula12_2_HTML.html", im):
            # linha de resposta
            self.send_response(200, "OK")

            # linhas de cabeçalhos
            self.send_header("Content-Type", "text/html; charset=utf-8")

            # linha em branco
            self.end_headers()

            if self.path == "/":
                self.path = "/Aula12_HTML.html"

            f = open(self.path[1:])

            # Resposta do Servidor
            self.wfile.write(f.read().encode())
        else:
            self.send_response(404, "NOT FOUND")

我的 HTML 代码之一就是这个简单的代码:

<!DOCTYPE html>

<html>
   <head>
      <meta charset="utf-8">
      <title> Bossbattle </title>
   </head>
   <body>
    <h1> A Calculadora </h1>
    <img src="calculator.png" alt= ">:C" title="U SHAL PERISH"/>
    <p> Olá, seja bem-vi... <b> Você não é bem vindo nessa página!! >:C </b> </p>
    <form action="calcular" method="POST">
        Nível da habílidade      [numb]: <input type="text" name="n1" value="" /> <br>
        Força da habilidade      [numb]: <input type="text" name="n2" value="" /> <br>
        Resisten a habilidade [+-*/]: <input type="text" name="n3" value="" /> <br>
        <input type="submit" name="Enviar" value="Atacar" />
    </form>
   </body>
</html>

【问题讨论】:

  • im 是一个 PIL Image 对象。它不是 URL 字符串。您需要添加要查找的 URL。此外,如果您确实收到了对图像的请求,则需要将Content-Type 设置为image/png,而不是text/html,并且您不想对其进行编码。您需要将其作为二进制文件读取。这里根本不需要 PIL。
  • 你帮了我很多关于这个小费的伙伴,我会把这个命名为答案!在 3 分钟内,代码正在运行。谢谢o/

标签: python html image http


【解决方案1】:

这是一个可行的版本:

from http.server import BaseHTTPRequestHandler, HTTPServer

class RequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        # processamento
        if self.path in ("/", "/Aula12_2_HTML.html"):
            print("html")
            self.send_response(200, "OK")
            self.send_header("Content-Type", "text/html; charset=utf-8")
            self.end_headers()

            document = "Aula12_HTML.html"

            f = open(document)

            self.wfile.write(f.read().encode())
        elif self.path == '/calculator.png':
            self.send_response(200, "OK")
            self.send_header("Content-Type", "image/png")
            self.end_headers()

            self.wfile.write(open('calculator.png','rb').read())
        else:
            self.send_response(404, "NOT FOUND")

httpd = HTTPServer(('127.0.0.1', 8080), RequestHandler )
httpd.serve_forever()

【讨论】:

  • 罗伯茨,我真的要谢谢你!你的两个答案都救了我(我想投赞成票,但由于我没有声望,所以还不能这样做)。
  • 太棒了! http.server 模块非常适合小型实验,但如果您的网站变得更复杂,您将需要研究 Python Web 框架之一:Django、Flask、CherryPy 等。它们有助于自动化几乎所有的乏味的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多