V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
BingoXuan
V2EX  ›  Python

无法使用 zeromq 连接到服务器

  •  
  •   BingoXuan · 2018-07-04 16:18:32 +08:00 · 1631 次点击
    这是一个创建于 2115 天前的主题,其中的信息可能已经有所发展或是发生改变。
    问题描述:我在腾讯云的服务器上用 python 写了一个 zeromq 的 publisher,监听端口 8099。可我在自己电脑写的 subscriber 却不能连接到服务器,一直收不到服务器 publish 出来的信息。
    8099 的端口是开放可用的,我直接开了 python 自带的 CGIHTTPServer 是能连接上去。代码方面也是没有问题的,我在 localhost 环境下测试过才部署的。

    希望各位提出想法和建议吧~只要不是水的回复都送铜币
    7 条回复    2018-07-04 17:07:57 +08:00
    BingoXuan
        1
    BingoXuan  
    OP
       2018-07-04 16:51:54 +08:00
    贴一下服务器的部分代码

    class ZPub(Pub):

    def __init__(self, identity):
    super().__init__(identity)
    ctx = zmq.Context()
    self.pub_socket = ctx.socket(zmq.PUB)
    self.pub_socket.setsockopt_string(zmq.IDENTITY, self.identity)
    self.pub_socket.bind('tcp://127.0.0.1:8099')
    self.lock = threading.Lock()

    def _send(self, ts, msg, id_str):
    self.lock.acquire()
    self.pub_socket.send_multipart([bytes(m, encoding='ascii') for m in [ts, msg, id_str]])
    self.lock.release()
    BingoXuan
        2
    BingoXuan  
    OP
       2018-07-04 16:56:10 +08:00
    问题已解决:绑定 tcp://*8099

    有谁知道具体原因呢?
    ThirdFlame
        3
    ThirdFlame  
       2018-07-04 16:58:13 +08:00   ❤️ 1
    127.0.0.1 不是仅允许本机连接么。
    xcai
        4
    xcai  
       2018-07-04 16:58:50 +08:00 via Android   ❤️ 1
    腾讯云安全组放行端口
    tempdban
        5
    tempdban  
       2018-07-04 17:02:09 +08:00 via Android   ❤️ 1
    self.pub_socket.bind('tcp://0.0.0.0:8099')
    mosliu
        6
    mosliu  
       2018-07-04 17:05:20 +08:00   ❤️ 1
    127.0.0.1 是本机 loopback 网卡吧。。。
    BingoXuan
        7
    BingoXuan  
    OP
       2018-07-04 17:07:57 +08:00
    @ThirdFlame
    @xcai
    @tempdban
    @mosliu

    是的,是我智商掉线。设置防火墙时候知道 0.0.0.0,绑定时候是 127.0.0.1。多谢各位大佬
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5421 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 06:04 · PVG 14:04 · LAX 23:04 · JFK 02:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.