pyzmq icon indicating copy to clipboard operation
pyzmq copied to clipboard

AttributeError: module 'zmq' has no attribute 'SERVER'

Open bw7715 opened this issue 3 years ago • 7 comments

When I try create Client-Server pattern socket-api in my project I get:

E:\Work\Venvs\p39\Scripts\python.exe "E:/python_projects/communication/zeromq/server_socket.py"
Traceback (most recent call last):
  File "E:\python_projects\communication\zeromq\server_socket.py", line 6, in <module>
    socket = context.socket(zmq.SERVER)
AttributeError: module 'zmq' has no attribute 'SERVER'

Code to reporduce:

import zmq

port = 5556

context = zmq.Context()
socket = context.socket(zmq.SERVER)
socket.bind("tcp://*:%s" % port)

while True:
    message = socket.recv()
    print("Received request: ", message)
    socket.send("World from %s" % port)

Tested on: Windows 10 Python 3.9 and 3.10 pyzmq 22.3.0

bw7715 avatar Apr 27 '22 11:04 bw7715

SERVER is a draft socket type, so you need to install pyzmq and libzmq from source to enable drafts.

minrk avatar Apr 27 '22 11:04 minrk

I try to build pyzmq, but with command make -j && make install I get a lot of errors in msys2:

cc1plus: all warnings being treated as errors
make[1]: *** [Makefile:5850: src/libzmq_la-select.lo] Error 1
make[1]: *** [Makefile:5864: src/libzmq_la-session_base.lo] Error 1
make[1]: *** [Makefile:5654: src/libzmq_la-pair.lo] Error 1
make[1]: *** [Makefile:5633: src/libzmq_la-object.lo] Error 1
make[1]: *** [Makefile:5647: src/libzmq_la-own.lo] Error 1
make[1]: *** [Makefile:5871: src/libzmq_la-signaler.lo] Error 1
make[1]: *** [Makefile:5696: src/libzmq_la-plain_client.lo] Error 1
make[1]: *** [Makefile:5542: src/libzmq_la-ipc_connecter.lo] Error 1
make[1]: *** [Makefile:5703: src/libzmq_la-plain_server.lo] Error 1
make[1]: *** [Makefile:5710: src/libzmq_la-poll.lo] Error 1
make[1]: *** [Makefile:5752: src/libzmq_la-pub.lo] Error 1
make[1]: *** [Makefile:5766: src/libzmq_la-push.lo] Error 1
make[1]: *** [Makefile:5731: src/libzmq_la-pollset.lo] Error 1
make[1]: *** [Makefile:5745: src/libzmq_la-proxy.lo] Error 1
make[1]: *** [Makefile:5808: src/libzmq_la-raw_engine.lo] Error 1
make[1]: *** [Makefile:5773: src/libzmq_la-radio.lo] Error 1
make[1]: *** [Makefile:5822: src/libzmq_la-rep.lo] Error 1
make[1]: *** [Makefile:5759: src/libzmq_la-pull.lo] Error 1
make[1]: *** [Makefile:5829: src/libzmq_la-req.lo] Error 1
make[1]: *** [Makefile:5836: src/libzmq_la-router.lo] Error 1
make[1]: *** [Makefile:5857: src/libzmq_la-server.lo] Error 1
make[1]: *** [Makefile:5843: src/libzmq_la-scatter.lo] Error 1
make[1]: *** [Makefile:5815: src/libzmq_la-reaper.lo] Error 1
make[1]: Leaving directory '/home/BW7715/zeromq-4.3.4'
make: *** [Makefile:8217: all-recursive] Error 1

My system: Windows 10 ZMQ=4.3.4 gcc=11.2.0

bw7715 avatar Apr 28 '22 09:04 bw7715

$ gcc --version
gcc (GCC) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ make --version
GNU Make 4.3
Built for x86_64-pc-msys
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

bw7715 avatar Apr 28 '22 09:04 bw7715

Sorry, I don't know how to build libzmq on msys. You might ask over there.

minrk avatar Apr 28 '22 10:04 minrk

Thanks, I try also with mingw but also fails after:

  CXX      src/libzmq_la-raw_encoder.lo
In file included from src/poller.hpp:52,
                 from src/io_object.hpp:36,
                 from src/session_base.hpp:36,
                 from src/curve_server.cpp:36:

What do you recommend for building the libzmq library on Windows?

bw7715 avatar Apr 28 '22 11:04 bw7715

I believe the binaries here have drafts enabled.

minrk avatar Apr 28 '22 11:04 minrk

I try to build pyzmq with drafts. On linux works, but on Windows I get an error:

PS D:\Work\pyzmq_test> D:\Work\venv\Scripts\python.exe .\client-server.py                 
Assertion failed: socket_type_ >= 0 && socket_type_ < static_cast<int> (names_count) (bundled\zeromq\src\mechanism.cpp:119)

PyZMQ build with script: https://github.com/zeromq/pyzmq/blob/main/examples/draft/install.sh

Code to reproduce: https://github.com/zeromq/pyzmq/blob/main/examples/draft/client-server.py

import time
import zmq

ctx = zmq.Context.instance()

url = 'tcp://127.0.0.1:5555'
server = ctx.socket(zmq.SERVER)
server.bind(url)

for i in range(10):
    client = ctx.socket(zmq.CLIENT)
    client.connect(url)
    client.send(b'request %i' % i)
    msg = server.recv(copy=False)
    print(f'server recvd {msg.bytes!r} from {msg.routing_id!r}')
    server.send_string('reply %i' % i, routing_id=msg.routing_id)
    reply = client.recv_string()
    print('client recvd %r' % reply)
    time.sleep(0.1)
    client.close()

server.close()
ctx.term()

bw7715 avatar May 12 '22 10:05 bw7715