Home >Backend Development >Python Tutorial >Three ways to use Python as a backend for small programs
Hello, I am Brother Zheng. WeChat's mini program is a very good experience, simple and quick to use. I have been learning to use mini programs these days. I have summarized three ways to use Python as the backend of mini programs for your reference.
Advantages: No need to purchase a server, no domain name registration, billing based on usage, DevOps automation, security authentication, suitable for people without operation and maintenance experience.
Disadvantages: The cost is definitely slightly higher than the cost of building a self-built server. Just like the same model, automatic transmission cars are more expensive than manual transmission cars.
The so-called cloud hosting is a Docker container. You only need to get a warehouse. You can create a warehouse in any of github, gitlab, and gitee, write the Dockerfile, and upload it to Cloud hosting, cloud hosting will automatically build and run the container image, and the way to run the container can be customized. After the deployment is completed, we will get a default domain name address, which is the entrance for the service to provide external services. You can call and access it as a normal service, or you can bind your own domain name.
In the mini program, you can access the container service like this:
// 确认已经在 onLaunch 中调用过 wx.cloud.init 初始化环境(任意环境均可,可以填空) const res = await wx.cloud.callContainer({ config: { env: '填入云环境ID', // 微信云托管的环境ID }, path: '/xxx', // 填入业务自定义路径和参数,根目录,就是 / method: 'POST', // 按照自己的业务开发,选择对应的方法 header: { 'X-WX-SERVICE': 'xxx', // xxx中填入服务名称(微信云托管 - 服务管理 - 服务列表 - 服务名称),在上述实践中是 demo } // 其余参数同 wx.request }); console.log(res);
With the container, you can use any programming language for the backend. It depends on what you are good at. Python is absolutely no problem. And there is an official Django template for one-click deployment.
Advantages: No domain name registration is required, and there is a certain free quota.
Disadvantages: Configure the server yourself.
The so-called cloud function is a Node.js function running on Tencent Cloud. It only has computing logic and can seamlessly access the cloud database for data access.
The applet calls the cloud function like this:
wx.cloud.callFunction({ // 要调用的云函数名称 name: 'dailyexam', // 传递给云函数的event参数 data: { x: 1, y: 2, } }).then(res => { // output: res.result === 3 }).catch(err => { // handle error })
Node.js function is actually an asynchronous javascript function. In the cloud function, we can request services on the self-built server, so that the domain name There is no need to register. For example, in the following code, the domain name somenzz.cn is not registered.
The cloud function terminal calls the self-built API service like this:
The free quota is usually very small, the number of daily reads of the database cannot exceed 500, and the number of writes cannot exceed 300.
Because it is a self-built service, it can naturally be developed in Python.
Advantages: Save money, the greater the number of visits, the more money you will save.
Disadvantages: Domain name registration is required.
If you do not use cloud functions and cloud hosting, you can only use the wx.request function on the mini program to request self-built services:
wx.request({ url: 'example.php', //仅为示例,并非真实的接口地址 data: { x: '', y: '' }, header: { 'content-type': 'application/json' // 默认值 }, success (res) { console.log(res.data) } })
The wx.request function has usage restrictions, as follows :
Usually you need a domestic server and then register the domain name, and registration will usually dissuade a large number of developers.
Because you build your own server, of course you can use Python to develop it.
[1]WeChat’s cloud hosting: https://cloud.weixin.qq.com/cloudrun?utm_source=idecloudconsole
[2]WeChat’s Cloud function transfer: https://developers.weixin.qq.com/miniprogram/dev/wxcloud/basis/capabilities.html#云function
The above is the detailed content of Three ways to use Python as a backend for small programs. For more information, please follow other related articles on the PHP Chinese website!