Table of Contents
Method 1. WeChat cloud hosting[1].
Method 2. WeChat’s cloud function transfer [2]
Method 3: Build your own server and register the domain name
Reference materials
Home Backend Development Python Tutorial Three ways to use Python as a backend for small programs

Three ways to use Python as a backend for small programs

Apr 12, 2023 pm 09:10 PM
python docker container

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to change the docker image source in China How to change the docker image source in China Apr 15, 2025 am 11:30 AM

You can switch to the domestic mirror source. The steps are as follows: 1. Edit the configuration file /etc/docker/daemon.json and add the mirror source address; 2. After saving and exiting, restart the Docker service sudo systemctl restart docker to improve the image download speed and stability.

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to build a private repository by docker How to build a private repository by docker Apr 15, 2025 am 11:06 AM

You can build Docker private repositories to securely store and manage container images, providing strict control and security. The steps include: creating a repository, granting access, deploying a repository, pushing an image, and pulling an image. Advantages include security, version control, reduced network traffic and customization.

How to call docker lnmp How to call docker lnmp Apr 15, 2025 am 11:15 AM

Docker LNMP container call steps: Run the container: docker run -d --name lnmp-container -p 80:80 -p 443:443 lnmp-stack to get the container IP: docker inspect lnmp-container | grep IPAddress access website: http://<Container IP>/index.phpSSH access: docker exec -it lnmp-container bash access MySQL: mysql -u roo

How to run the docker command How to run the docker command Apr 15, 2025 am 11:24 AM

How to run Docker commands? Install Docker and start the daemon. Common Docker commands: docker images: display image docker ps: display container docker run: run container docker stop: stop container docker rm: delete container interact with container using Docker command: docker exec: execute command docker attach: attach console docker logs: display log docker commit: commit change to mirror stop Docker daemon: sudo systemctl stop doc

How to save docker image How to save docker image Apr 15, 2025 am 11:54 AM

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

See all articles