简体   繁体   English

部署一个基本的 python sanic webserver

[英]Deploy a basic python sanic webserver

I have a sanic app which as a server folder, inside that I have server.py along with init .py.我有一个作为服务器文件夹的 sanic 应用程序,里面有 server.py 和init .py。 Inside init .py I have a one line import *init .py 我有一个单行import *

Here's what server.py looks like:下面是 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)

and there is a main file outside of the server folder where I import server.py and run it.并且在我导入 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")))

I accept some POST request via this simple server and print them in the console.It works fine in localhost but in web server If I run it I get webservers localhost address which is not exposed in public.我通过这个简单的服务器接受一些 POST 请求并在控制台中打印它们。它在 localhost 中工作正常,但在 web 服务器中如果我运行它,我会得到网络服务器 localhost 地址,该地址不会公开。 How can I expose it to public?我怎样才能把它暴露给公众? I tried hours and hours, gunicorn, sanic built in server, apache, nginx everything But I failed.我尝试了几个小时,gunicorn,sanic 内置服务器,apache,nginx 一切但我失败了。 I would appreciate any help.我将不胜感激任何帮助。

I think what you need to do is bind Sanic to 0.0.0.0 .我认为您需要做的是将 Sanic 绑定到0.0.0.0

server.run(host="0.0.0.0", ...)

Read this to learn about 0.0.0.0 阅读本文以了解0.0.0.0

Or, alternatively, you would put the IP address that you want it to be exposed on.或者,或者,您可以放置您希望在其上公开的 IP 地址。 It is difficult to give you exact details not knowing how you deploy it in production.不知道如何在生产中部署它很难给你确切的细节。

Feel free to post in the Sanic community forums or Discord server if you would like to engage in more of a conversation about this.如果您想参与更多关于此的对话,请随时在 Sanic 社区论坛或 Discord 服务器上发帖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM