程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using the python backend API hosted by vercel -- package and environment indexing, detailed tutorial for hands-on

編輯:Python

vercel brief introduction

vercel Is a code hosting platform , It can host your static html Interface , Can even host your node.js And Python Server script , It's a blessing for lazy people who don't want to have servers !

Start using vercel Drag tube static website

To use vercel First you need a vercel account number , Here I am Recommend to use GitHub Account Association , So you can be in vercel Directly hosting your GitHub Items in the library , Realize the development and deployment in one step .
P.S: If the link is not available, please go online scientifically

stay Vercel The home page can be used directly GitHub Sign in




Click on new project





If you can't find the library you need , Click on the new jump interface Adjust Github App Permissions



Then you can import the project you need to host and successfully host it


It is worth noting that , You're here vercel Default entry for managed static projects ( route ) yes index.html, If they can't find it, they call 404

Use vercel trusteeship python project

trusteeship python A project is no different from hosting a static website , However, there are several points to be noted :
1. Use vercel trusteeship python project , First you need to create a project directory api Folder , It holds your python Script .
2.api The folder is also marked with index.py For the entrance , Of course you can be in Vercel Program entry specified in .
Yours python The script structure should look like the following example :

from http.server import BaseHTTPRequestHandler
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write("DearXuan's API by python!".encode())
return

When your hosting is successful , You can enter... In the address field ${your_project_name}.vercel.app/api To view your backend interface ~

So if I introduce the outside py What about the bag ?

It doesn't seem very good to package all the packages into your program , But don't worry ,vercel I have considered the problem for you , You just need to generate a package that contains the package you referenced requirements.txt That's all right. .
As for how to generate requirements.txt Well ? There are many ways , But many don't work . After my test , Find only one effective way :

  • install pipreqs
  • Run the command in the project path pipreqs ./ --encoding=utf8

In this way, even if your project references an external package, it can run normally ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved