Created
May 21, 2018 08:30
-
-
Save devlights/1405536ac3ab7739fbabc74f54f31043 to your computer and use it in GitHub Desktop.
[python][zeromq] REP-REQのサンプル (REPがサーバ、REQがクライアント)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import zmq | |
| def main(): | |
| ctx = zmq.Context() | |
| socket = ctx.socket(zmq.REP) | |
| socket.bind('tcp://*:5560') #: https://stackoverflow.com/questions/6024003/why-doesnt-zeromq-work-on-localhost | |
| while True: | |
| m = socket.recv() | |
| if m == b'quit': | |
| print('shutdown server....') | |
| socket.send(b'shutdown') | |
| break | |
| print(f'{m}') | |
| socket.send(m.upper()) | |
| if __name__ == '__main__': | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| import zmq | |
| def main(): | |
| ctx = zmq.Context() | |
| socket = ctx.socket(zmq.REQ) | |
| socket.connect('tcp://localhost:5560') | |
| socket.send(b'quit' if args.quit else b'hello') | |
| m = socket.recv() | |
| print(f'{m}') | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--quit', action='store_true', default=False) | |
| args = parser.parse_args() | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment