python - 如何提供来自 Google App Engine 的 GZIP 编码图像
问题描述
我正在尝试获取链接共享以包含 Facebook 上的图片。Facebook 共享调试器表示图像没有使用 gzip 编码,这显然是一项要求。
使用 curl,我可以确认 Facebook 在说什么。我能找到的所有文档都说 gzip 编码应该自动发生,所以我不确定如何解决这个问题。
这是卷曲输出:
$ curl -v -D /tmp/h -H "Accept-Encoding: gzip" http://m.kaon.com/ss/5907657076506624.jpg > /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 172.217.3.115...
* TCP_NODELAY set
* Connected to m.kaon.com (172.217.3.115) port 80 (#0)
> GET /ss/5907657076506624.jpg HTTP/1.1
> Host: m.kaon.com
> User-Agent: curl/7.54.0
> Accept: */*
> Accept-Encoding: gzip
>
< HTTP/1.1 200 OK
< Cache-Control: no-cache
< Content-Type: image/jpeg
< X-Cloud-Trace-Context: 5b0c467df46cb34732e5721733742742
< Date: Wed, 13 Mar 2019 15:11:08 GMT
< Server: Google Frontend
< Content-Length: 128384
<
{ [3516 bytes data]
100 125k 100 125k 0 0 620k 0 --:--:-- --:--:-- --:--:-- 623k
* Connection #0 to host m.kaon.com left intact
$ cat /tmp/h
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Type: image/jpeg
X-Cloud-Trace-Context: 5b0c467df46cb34732e5721733742742
Date: Wed, 13 Mar 2019 15:11:08 GMT
Server: Google Frontend
Content-Length: 128384
这是提供图像的代码:
class SSHandler(webapp2.RequestHandler):
def get(self, id):
a = ScreenShotModel.get_by_id(int(id))
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(a.image)
是否缺少一些 app.yaml 设置,或者我需要在标题中添加其他内容?或者我应该自己寻找接受编码标头并在我的 python 代码中执行 gzip?
解决方案
对 jpeg 进行 gzip 压缩是没有意义的。它已经被压缩了。
有一组允许的 mime 类型用于压缩。JPEG 不是其中之一。
白名单中典型的有:
文本/纯文本、文本/html、应用程序/javascript等
推荐阅读
- javascript - React Native TypeError:无法读取未定义的属性'createClient'
- wordpress - 我想在 Google Cloud 中托管 wordpress 网站。我现有的 G Suite 邮件会照常工作吗?
- java - 两个参数列表中的 JPQL 条件
- php - 如何在 Laravel Eloquent 中实现嵌套的 If 语句?
- reactjs - 如何在 React 中嵌入视频?
- javascript - let 声明的变量可以声明也可以不声明
- c# - 其他选项卡正在下载文件时无法访问网站(来自同一网站)
- flask - Celery 在与主(烧瓶)应用程序不同的模块中
- r - 将来自 shinyFiles::shinyFileChoose 的文件路径传递给另一个函数
- python - Python 将 CSV 导入数组 TypeError:无法根据规则“安全”将数组数据从 dtype('float64') 转换为 dtype('S32')