forked from midonet/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
echo-send.py
executable file
·109 lines (94 loc) · 2.98 KB
/
echo-send.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python
# Copyright 2015 Midokura SARL
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import getopt
import socket
import sys
import traceback
from zephyr.common import exceptions
DEFAULT_ECHO_PORT = 5080
TERMINATION_STRING = chr(0x03) + chr(0x04)
def usage():
print('Usage: echo-send.py [-i <ip>] [-p <port>] [-d] [-c <protocol>]')
print(' [-o <output_string>] [-t <timeout>]')
arg_map, _ = getopt.getopt(
sys.argv[1:],
'i:'
'p:'
'c:'
'o:'
't:'
'h',
['help', 'ip', 'port', 'protocol', 'timeout', 'out-str'])
ip_addr = 'localhost'
port = DEFAULT_ECHO_PORT
echo_request_string = "ping"
protocol = "tcp"
timeout = 5
for arg, value in arg_map:
if arg in ('-i', 'ip'):
ip_addr = value
elif arg in ('-p', 'port'):
port = int(value)
elif arg in ('-c', 'protocol'):
protocol = value
elif arg in ('-t', 'timeout'):
timeout = float(value)
elif arg in ('-o', 'out-str'):
echo_request_string = value
elif arg in ('-h', 'help'):
usage()
exit(0)
else:
usage()
raise exceptions.ArgMismatchException(
"Option not recognized: " + arg)
try:
req = echo_request_string + TERMINATION_STRING
if protocol == 'tcp':
new_socket = socket.create_connection((ip_addr, port), timeout)
new_socket.sendall(req)
elif protocol == 'udp':
new_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
new_socket.settimeout(timeout)
new_socket.sendto(req, (ip_addr, port))
else:
raise exceptions.ArgMismatchException(
'Unsupported self.protocol: ' + protocol)
new_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
new_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
data = ''
if protocol == 'tcp':
while True:
new_data = new_socket.recv(2048)
""" :type: str"""
pos = new_data.find(TERMINATION_STRING)
if pos != -1:
data += new_data[0:pos]
break
else:
data += new_data
elif protocol == 'udp':
data, addr = new_socket.recvfrom(2048)
new_socket.close()
pos = data.find(TERMINATION_STRING)
out_str = data
if pos != -1:
out_str = data[0:pos]
print(out_str.strip())
except Exception as e:
sys.stderr.write("ERROR: " + str(e))
sys.stderr.flush()
traceback.print_tb(sys.exc_traceback, file=sys.stderr)
exit(2)