56

当微信小程序遇上 TensorFlow:Server 端实现补充

 5 years ago
source link: https://mp.weixin.qq.com/s/Jl-4wfZ6Zl_bRnfdLvO2Fw?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VvqEVrN.jpg!web

在前面一篇文章《 当微信小程序遇上TensorFlow:Server端实现 》中,我们探讨了微信小程序server端的实现。今天在调试微信小程序时才发现一个问题,那就是: 微信小程序要求HTTP请求走更加安全的HTTPS协议

修改Simple TensorFlow Serving

为此我研究了Simple TensorFlow Serving: https://github.com/tobegit3hub/simple_tensorflow_serving 的源码,发现其WEB框架采用了flask。flask要增加https支持相当容易,因此我增加了三个参数:

parser.add_argument(
    "--enable_ssl", default=False, help="If enable RESTfull API over https")
parser.add_argument(
    "--secret_pem", default="secret.pem", help="pem file")
parser.add_argument(
    "--secret_key", default="secret.key", help="key file")

第一个参数指定是否启用HTTPS协议,后面两个参数为证书文件名。这个修改我已经向原作者提了一个PR,等待合并。在合并之前,你可以访问 https://github.com/mogoweb/simple_tensorflow_serving 获取最新代码。

证书申请

上阿里云可以申请一个免费的证书,虽然有效期只有一年,但对于我来说足够用。需要注意的是,阿里云上申请证书很隐蔽,我也是尝试了好几个组合,那个免费证书的选项才出现。因为我的主机就是托管在阿里云上,域名也是使用阿里云的DNS服务,所以申请完后,很快就审核通过,然后就可以下载证书。证书下载时我选择 证书for Nginx ,下载下来就是两个文件:一个pem文件,一个key文件。

VJ3URvZ.jpg!web

测试客户端

测试客户端倒不需要怎么修改,只需要将URL的 http:// 修改为 https:// 即可。为此,我也增加了一个参数enable_ssl:

 if enable_ssl :
    endpoint = "https://ilego.club:8500"
  else:
    endpoint = "http://ilego.club:8500"

完整代码请参考: https://github.com/mogoweb/aiexamples/blob/master/AIDog/serving/test_client.py

小结

真正在做项目时,总会碰到意想不到的问题,这远不是照着书本敲几个demo可以获得的经验。在开始做这个微信小程序时,觉得很简单,但做着做着,发现一路碰到的问题还不少。这也是我经常建议新手自己动手做点小项目的原因,即使这个小项目并没有什么产品价值,即使是在现有的代码上修改,但这个过程中积累的经验对于个人来说,还是很宝贵的。

VBreEnM.jpg!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK