Zeromy Quick Start-python

Source: Internet
Author: User

Software:

Pip Install PYZMQ

Code:

==server.py

#
# Hello World Server in Python
# binds REP socket to tcp://*:5555
# expects "Hello" from the client, replies with "World"
#
Import ZMQ
Import time

Context = Zmq. Context ()
Socket = Context.socket (ZMQ. REP)
Socket.bind ("tcp://*:5555")

While True:
# Wait for next request from client
message = SOCKET.RECV ()
Print ("Received Request:", message)

# do some ' work '
Time.sleep (1) # do some ' work '

# Send reply back to client
Socket.send_string ("World")

==client.py

#
# Hello World Client in Python
# connects REQ socket to tcp://localhost:5555
# sends "Hello" to server, expects ' world ' back
#
Import ZMQ

Context = Zmq. Context ()

# Socket to talk to server
Print ("Connecting to Hello World server ...")
Socket = Context.socket (ZMQ. REQ)
Socket.connect ("tcp://localhost:5555")

# do ten requests, waiting each time for a response
For request in range (1,10):
Print ("Sending request", request, "...")
Socket.send_string ("Hello")

# Get the reply.
message = SOCKET.RECV ()
Print ("Received reply", Request, "[", Message, "]")

Steps

Open a command line and execute the Python server.py

Open a command line and execute the Python client.py

Reference:

10397917

Zeromy Quick Start-python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.