部署基本的python sanic web服务器

41zrol4v  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(237)

我有一个sanic应用程序作为服务器文件夹,里面有server.py和init.py。在init.py里面有一行 import * 下面是server.py的外观:


# server.py #

    from sanic import Sanic
    from sanic.response import json

    class Bot:
        def __init__(self):
            self.app = Sanic("message-queue")
            self.app.add_route(self._post, "/", methods=["POST"])

        async def _post(self, request):
            data = request.json

            data1 = data["data1"]
            data2 = data["data2"]
            data3 = data["data3"]

        def run(self, host="localhost", port=6778):
             self.app.run(host, port)

服务器文件夹外有一个主文件,我在其中导入server.py并运行它。

import os
from server import Server
from sanic import Sanic

if __name__ == "__main__":
    server = Server()
    server.run(host=os.getenv("HOST"), port=int(os.getenv("PORT")))

我通过这个简单的服务器接受一些post请求,并在控制台中打印它们。它在localhost中工作正常,但是在web服务器中,如果我运行它,我会得到webservers localhost地址,它不会公开。我怎样才能把它公之于众?我试了好几个小时,gunicorn,sanic内置服务器,apache,nginx,但我失败了。如果有任何帮助,我将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题