Tho*_*s W 6 encode image http python-3.x server
我想在我的后续处理程序上提供一些帮助:
class MyHandler(http.server.BaseHTTPRequestHandler):
def do_HEAD(client):
client.send_response(200)
client.send_header("Content-type", "text/html")
client.end_headers()
def do_GET(client):
if client.path == "/":
client.send_response(200)
client.send_header("Content-type", "text/html")
client.end_headers()
client.wfile.write(load('index.html'))
def load(file):
with open(file, 'r') as file:
return encode(str(file.read()))
def encode(file):
return bytes(file, 'UTF-8')
Run Code Online (Sandbox Code Playgroud)
我有这个,该功能load()是文件中的其他人.通过我的HTTP处理程序发送HTML页面似乎正在工作,但我如何发送图像?我如何编码它以及我Content-type应该使用什么?
非常感谢帮助!
(PS:如果我连接到我的httpserver,我希望在浏览器中看到发送的图像)
对于PNG图像,您必须将内容类型设置为"image/png".对于jpg:"image/jpeg".
其他内容类型可在此处找到.
编辑:是的,我在第一次编辑时忘了编码.
答案是:你没有!从文件加载图像时,它已经是正确的编码.
我读到了你的编解码器问题:问题是,我在你的加载函数中看到了.不要尝试编码文件内容.
您可以使用二进制数据:
def load_binary(file):
with open(file, 'rb') as file:
return file.read()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14578 次 |
| 最近记录: |