Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Windows中使用Python BaseHttpRequestHandler将图像传输到客户端?_Python_Windows_Http_Python Imaging Library_Basehttprequesthandler - Fatal编程技术网

如何在Windows中使用Python BaseHttpRequestHandler将图像传输到客户端?

如何在Windows中使用Python BaseHttpRequestHandler将图像传输到客户端?,python,windows,http,python-imaging-library,basehttprequesthandler,Python,Windows,Http,Python Imaging Library,Basehttprequesthandler,我知道以前有人问过这个问题,比如这里:,但“将文件打开模式设置为二进制”的典型答案对我来说并不适用 我正在使用Windows。对文本数据(html、js、css)的所有请求都得到满足,没有任何问题。未正确满足图像和字体请求。返回的响应总是由大约150个字节组成(但实际上应该是50k左右)。所以,我想基本上是空的回答,缺少图像和字体。到目前为止,我在使用相同代码的Linux下没有遇到任何问题: content = open(content_path, 'rb') ... self.send_res

我知道以前有人问过这个问题,比如这里:,但“将文件打开模式设置为二进制”的典型答案对我来说并不适用

我正在使用Windows。对文本数据(html、js、css)的所有请求都得到满足,没有任何问题。未正确满足图像和字体请求。返回的响应总是由大约150个字节组成(但实际上应该是50k左右)。所以,我想基本上是空的回答,缺少图像和字体。到目前为止,我在使用相同代码的Linux下没有遇到任何问题:

content = open(content_path, 'rb')
...
self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
self.wfile.write(content.read) 
content.close()  
我最初使用的是“open”的默认模式,在阅读了对类似问题的回答后,我添加了二进制标志,假设它可以解决问题,但它没有。这里可能有编码问题吗?同样,这在Linux下也可以工作。以下是打印到控制台的“内容”的值:

<open file [my_file_path], mode 'rb' at 0x02A9BA18> with mime-type image/jpeg
使用mime类型图像/jpeg

如果相关的话,我可能还应该提到,在应用程序的前面,python PIL库已经调整了所有图像的大小。非常感谢您的帮助。

您正在发送
read
方法的字符串表示。 而是通过附加
()
调用
read
方法:

如果文件很大,
file.read()
会产生问题,因为
read()
会将文件内容加载到内存中。要防止出现这种情况,请使用:


我不相信。这是我对早上5:24编码的惩罚。你赢了这一轮,蟒蛇!谢谢你,先生。
self.wfile.write(content.read()) 
import shutil

...

self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
with open(content_path, 'rb') as content:
    shutil.copyfileobj(content, self.wfile)