使用python搭建简单的后台

07 Jan 2020

just give me the code

环境搭建

首先确认安装了 python3.x, 可以去官网下载, python org

然后安装Visual Studio Code, 因为它可以很方便的编辑python 和 h5 等语言,还可以debug,打断点

然后安装python插件

安装了python之后终端输入

pip3 install tornado

安装pythonrest框架 tornado

创建一个简单的get请求

创建第一个接口, 使用 8801 端口

使用debug 模型运行

启动成功之后可以看到当成程序的状态

因为是在本机运行的,所以这里使用 localhost:8801 作为根地址 可以看到请求结果,第一个api创建成功了

也可以使用 127.0.0.1 也是本机地址

或者使用本机的ip地址,这样在同一个局域网下都可以访问

macOs 查看ip的方式

至此我们已经搭建好环境,并创建了一个get请求

get的其他形式

返回h5

app = tornado.web.Application([
        # create the root request, return a html
        (r"/", rootRequestHandler),
        (r"/quoting", quotingRequestHandler)
    ])

# deal with the root request
class rootRequestHandler(tornado.web.RequestHandler):
    def get(self):
        # return and a html from a file
        self.render("index.html")

创建一个根请求,处理get请求,返回一个本地的h5文件,访问这个链接可以看到渲染好的网页

处理get参数

app = tornado.web.Application([
        # create the root request, return a html
        (r"/", rootRequestHandler),
        # the basic get, no parameter
        (r"/quoting", quotingRequestHandler),
        # get with query params
        (r"/isEven", queryParamRequestHandler)
    ])

class queryParamRequestHandler(tornado.web.RequestHandler):
    def get(self):
        # deal with arguments
        num = self.get_argument("num")
        if num.isdigit():
            r = "odd" if int(num) % 2 else "even"
            self.write(f"The number {num} is {r}")
        else:
            self.write(f"{num} is not a valid number.")

处理某种类型的get请求

app = tornado.web.Application([
        # create the root request, return a html
        (r"/", rootRequestHandler),
        # the basic get, no parameter
        (r"/quoting", quotingRequestHandler),
        # get with query params
        (r"/isEven", queryParamRequestHandler),
        # [A-z] A to z, "+" infinity
        (r"/students/([A-z]+)/([0-9]+)", resourceParamRequestHandler)
    ])

class resourceParamRequestHandler(tornado.web.RequestHandler):
    def get(self, studentName, courseId):
        # get regex path, this can just return html, or anything you like
        self.write(f"Welcome {studentName} the course you are enter is {courseId}")

[A-z]+ 所有字母,[0-9]+ 所有数字 /students/([A-z]+)/([0-9]+)接收students/后面跟着字母组合,数字组合的链接

链接没有处理,会出现 404 异常

使用json返回服务器数据

import json

app = tornado.web.Application([
        # create the root request, return a html
        (r"/", rootRequestHandler),
        # the basic get, no parameter
        (r"/quoting", quotingRequestHandler),
        # get with query params
        (r"/isEven", queryParamRequestHandler),
        # [A-z] A to z, "+" infinity
        (r"/students/([A-z]+)/([0-9]+)", resourceParamRequestHandler),
        # load and show server file
        (r"/list", listRequestHandler)
    ])

class listRequestHandler(tornado.web.RequestHandler):
    def get(self):
        with open("data.txt", "r") as f:
            # read file
            content = f.read().splitlines()
            # to json
            jsonContent = json.dumps(content)
            # return a json read from server
            self.write(jsonContent)

Post请求

Post请求通常用来更改数据, 而且只能通过代码,或者其他工具来请求

Post 添加数据

class listRequestHandler(tornado.web.RequestHandler):
    def get(self):
        with open("data.txt", "r") as f:
            # read file
            content = f.read().splitlines()
            # to json
            jsonContent = json.dumps(content)
            # return a json read from server
            self.write(jsonContent)
    def post(self):
        with open("data.txt", "a") as f:
            # just get the argument, body first then url
            print(f"{self.get_argument('animal')}")
            # just get the query argument in the url
            print(f"{self.get_query_argument('animal')}")
            # just get the body argument in "form-data"
            # print(f"{self.get_body_argument('animal')}")
            animal = self.get_argument("animal")
            f.write("\n"+animal)
            self.write({"msg": f"{animal} is successful added"})

在前面获取动物列表的请求处理中添加post的处理,使用postman来验证

Post 上传文件

因为需要和h5联动,可以查看示例demo

查看服务器的图片资源

app = tornado.web.Application([
        # create the root request, return a html
        (r"/", rootRequestHandler),
        # the basic get, no parameter
        (r"/quoting", quotingRequestHandler),
        # get with query params
        (r"/isEven", queryParamRequestHandler),
        # [A-z] A to z, "+" infinity
        (r"/students/([A-z]+)/([0-9]+)", resourceParamRequestHandler),
        # load and show server file
        (r"/list", listRequestHandler)
    ])
    #  read server's files
        #  The handler constructor requires a ``path`` argument, which specifies the
        #  local root directory of the content to be served.
        (r"/img/(.*)", tornado.web.StaticFileHandler, {"path": "img"})
    ])

r"/img/(.*)"可以通过这个路径访问资源文件

访问服务器上的文件

reference:
Python on the Backend | Udemy