linzhiming0826 / aiowerobot

一个基于sanic的异步微信公众号开发框架
3 stars 2 forks source link

Sanic server could not start: [Errno 98] Address already in use. #1

Closed jcevo closed 1 year ago

jcevo commented 1 year ago
image
linzhiming0826 commented 1 year ago

Due to the version update of sanic, the old method is invalid. The following methods can be used temporarily

from sanic import response, Sanic
from aiowerobot.robot import BaseRoBot

robot = BaseRoBot(
    app_id=*, app_secret=*, token=*, encoding_aes_key=*)

app = Sanic(__name__)

@app.route('/', methods=['GET', 'POST'])
async def handler(request):
    timestamp = request.args.get('timestamp', '')
    nonce = request.args.get('nonce', '')
    signature = request.args.get('signature', '')
    if not robot.check_signature(
        timestamp,
        nonce,
        signature,
    ):
        return response.html(robot.make_error_page(html.escape(request.url)))
    if request.method == 'GET':
        return response.text(request.args['echostr'][0])
    message = robot.parse_message(
        request.body,
        timestamp=timestamp,
        nonce=nonce,
        msg_signature=request.args.get('msg_signature', '')
    )
    res = response.html(await robot.get_encrypted_reply(message))
    res.headers['content_type'] = 'application/xml'
    return res

config = {'host': '0.0.0.0', 'port': 8061,
          'access_log': False, 'workers': 1}
app.run(**config)
jcevo commented 1 year ago

Due to the version update of sanic, the old method is invalid. The following methods can be used temporarily

from sanic import response, Sanic
from aiowerobot.robot import BaseRoBot

robot = BaseRoBot(
    app_id=*, app_secret=*, token=*, encoding_aes_key=*)

app = Sanic(__name__)

@app.route('/', methods=['GET', 'POST'])
async def handler(request):
    timestamp = request.args.get('timestamp', '')
    nonce = request.args.get('nonce', '')
    signature = request.args.get('signature', '')
    if not robot.check_signature(
        timestamp,
        nonce,
        signature,
    ):
        return response.html(robot.make_error_page(html.escape(request.url)))
    if request.method == 'GET':
        return response.text(request.args['echostr'][0])
    message = robot.parse_message(
        request.body,
        timestamp=timestamp,
        nonce=nonce,
        msg_signature=request.args.get('msg_signature', '')
    )
    res = response.html(await robot.get_encrypted_reply(message))
    res.headers['content_type'] = 'application/xml'
    return res

config = {'host': '0.0.0.0', 'port': 8061,
          'access_log': False, 'workers': 1}
app.run(**config)

thanks,how could i use asynchronous calls in handler?

from aiowerobot import AioWeRoBot

robot = AioWeRoBot(app_id='', app_secret='', token='')

@robot.handler async def hello(message): return 'Hello World!'

config = {'host': '0.0.0.0', 'port': 8099} robot.run(**config)