PyZMQ:权限被拒绝

PyZMQ: Permission denied

代码:

import zmq
from multiprocessing import Process
import time

def server():
    port = '5556'
    context = zmq.Context()
    socket = context.socket(zmq.PAIR)
    socket.bind('tcp://*:%s' % port)

    for i in range(10):
        socket.send('server to client')
        msg = socket.recv()
        print msg
        time.sleep(1)

def client():
    port = '5556'
    context = zmq.Context()
    socket = context.socket(zmq.PAIR)
    socket.bind('tcp://127.0.0.1:%s' % port)

    for i in range(10):
        msg = socket.recv()
        print msg
        socket.send('msg to server')
        time.sleep(1)

if __name__ == '__main__':
    Process(target=server, args=()).start()
    Process(target=client, args=()).start() 

错误:

Process Process-2:
Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 258, in _bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "C:\...\zmqtest.py", line 21, in client
socket.bind('tcp://127.0.0.1:%s' % port)
File "socket.pyx", line 444, in zmq.backend.cython.socket.Socket.bind (zmq\backend\cython\socket.c:4076)
File "checkrc.pxd", line 21, in zmq.backend.cython.checkrc._check_rc (zmq\backend\cython\socket.c:6032)
ZMQError: Permission denied

它可以为服务器启动进程 1 吗?为什么权限不同?

试图学习 ZMQ,但无法获得任何简单示例 运行 原因。有什么地方可以打开ZMQ的权限吗?找不到关于此事的文件

您只能在一侧(服务器).bind。在客户端你必须使用 .connect,虽然我得到 "Address already in use" 错误而不是 Permission denied.

尝试更改它以便客户端连接并查看它是否适合您:

import zmq
from multiprocessing import Process
import time

def server():
    port = '5556'
    context = zmq.Context()
    socket = context.socket(zmq.PAIR)
    socket.bind('tcp://*:%s' % port)

    for i in range(10):
        socket.send('server to client')
        msg = socket.recv()
        print msg
        time.sleep(1)

def client():
    port = '5556'
    context = zmq.Context()
    socket = context.socket(zmq.PAIR)
    socket.connect('tcp://127.0.0.1:%s' % port)

    for i in range(10):
        msg = socket.recv()
        print msg
        socket.send('msg to server')
        time.sleep(1)

if __name__ == '__main__':
    Process(target=server, args=()).start()
    Process(target=client, args=()).start()