簡體   English   中英

如何從節點服務器通知 python 客戶端?

[英]How to notify a python client from node server?

我有一個節點進程,它目前正在https://company/api/bats_hook/處接收 POST 請求。 每當有作業進入時,我想通知 python 進程。我使用的是 node_redis 和 redis-py,實現如下所示。

當節點進程在本地運行時,以下代碼在本地運行良好,當我將節點端點推送到服務器時,我似乎沒有收到事件?

如何從 python 客戶端訂閱服務器上的端點? 什么不見了? 我確實在服務器https://company/api/bats_hook/上部署了 redis 服務器

javascript

    var redis = require("redis"),
    //redisClient = redis.createClient();
    redisClient = redis.createClient({url: process.env.REDIS_URL});

    app.post("/api/bats_holder", (req, res, next) => {
      console.log(req.query.params)
      console.log(req.body)
      console.log(req.body.name)
      // This publishes a message to the "bats hook channel"
      redisClient.publish("bats hook channel", JSON.stringify({
        params: req.query.params,
        body: req.body,
      }));
      res.status(200).json({
        message: "BATS object",
        posts: req.body
      }); 
    });

python

    import redis

    r = redis.Redis()
    p = r.pubsub()
    p.subscribe('bats hook channel')

    # This blocks and reads any messages from the "bats hook channel" as they come in
    for message in p.listen():
        print(message)

那是因為您沒有在 python 腳本中配置 Redis URL 。 這就是為什么它默認使用 localhost 上的端點。 使用以下內容更新您的腳本。

import os
import redis

REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')

r = redis.from_url(REDIS_URL)
p = r.pubsub()
p.subscribe('bats hook channel')

# This blocks and reads any messages from the "bats hook channel" as they come in
for message in p.listen():
    print(message)

文檔中的更多詳細信息和示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM