summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/examples/zmq_server_client.py
blob: cbe136d032984dfd94579a6c7f6a498db854afe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import os

python_ver = 'python%s' % sys.version_info.major
ucs_ver = 'ucs2' if sys.maxunicode == 65535 else 'ucs4'

zmq_path = os.path.abspath(os.path.join(os.pardir,os.pardir,os.pardir,
        'external_libs','pyzmq-14.5.0', python_ver, ucs_ver, '64bit'))
if zmq_path not in sys.path:
   sys.path.append(zmq_path)

import zmq
import json
from argparse import *

parser = ArgumentParser(description=' Runs a Scapy Server Client example ')
parser.add_argument('-p','--dest-scapy-port',type=int, default = 4507, dest='dest_scapy_port',
                    help='Select port to which this Scapy Server client will send to.\n default is 4507\n',action='store')
parser.add_argument('-s','--server',type=str, default = 'localhost', dest='dest_scapy_ip',
                    help='Remote server IP address .\n default is localhost\n',action='store')

args = parser.parse_args()

dest_scapy_port = args.dest_scapy_port
dest_scapy_ip = args.dest_scapy_ip

context = zmq.Context()

#  Socket to talk to server
print 'Connecting:'
socket = context.socket(zmq.REQ)
socket.connect("tcp://"+str(dest_scapy_ip)+":"+str(dest_scapy_port)) 
try:
    while True:
        command = raw_input("enter RPC command [enter quit to exit]:\n")
        if (command == 'quit'):
            break
        user_parameter = raw_input("input for command [should be left blank if not needed]:\n")
        json_rpc_req = { "jsonrpc":"2.0","method": command ,"params":[user_parameter], "id":"1"}
        request = json.dumps(json_rpc_req)
        print("Sending request in json format %s" % request)
        socket.send(request)

        #  Get the reply.
        message = socket.recv()
        print("Received reply %s [ %s ]" % (request, message))
except KeyboardInterrupt:
                print('Terminated By Ctrl+C')
                socket.close()
                context.destroy()