简体   繁体   中英

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. Inside init .py I have a one line import *

Here's what server.py looks like:

   # 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.

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. How can I expose it to public? I tried hours and hours, gunicorn, sanic built in server, apache, nginx everything But I failed. I would appreciate any help.

I think what you need to do is bind Sanic to 0.0.0.0 .

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

Read this to learn about 0.0.0.0

Or, alternatively, you would put the IP address that you want it to be exposed on. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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