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.
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.
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.
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 })
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.
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) } })
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.
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!

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.