Three ways to use Python as a backend for small programs

WBOY
Release: 2023-04-12 21:10:14
forward
2601 people have browsed it

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.

Method 1. WeChat cloud hosting[1].

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.

Three ways to use Python as a backend for small programs

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);
Copy after login

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.

Method 2. WeChat’s cloud function transfer [2]

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
})
Copy after login

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:

Three ways to use Python as a backend for small programs

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.

Three ways to use Python as a backend for small programs

Because it is a self-built service, it can naturally be developed in Python.

Method 3: Build your own server and register the domain name

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)
}
})
Copy after login

The wx.request function has usage restrictions, as follows :

Three ways to use Python as a backend for small programs

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.

Reference materials

[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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!