Commit e66565f3 authored by Jeremy Cline's avatar Jeremy Cline Committed by Daniel Borkmann

bpf: Add Python 3 support to selftests scripts for bpf

Adjust tcp_client.py and tcp_server.py to work with Python 3 by using
the print function, marking string literals as bytes, and using the
newer exception syntax. This should be functionally equivalent and
supports Python 3+.
Signed-off-by: default avatarJeremy Cline <jcline@redhat.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 2cc512c1
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
# #
...@@ -9,11 +9,11 @@ import subprocess ...@@ -9,11 +9,11 @@ import subprocess
import select import select
def read(sock, n): def read(sock, n):
buf = '' buf = b''
while len(buf) < n: while len(buf) < n:
rem = n - len(buf) rem = n - len(buf)
try: s = sock.recv(rem) try: s = sock.recv(rem)
except (socket.error), e: return '' except (socket.error) as e: return b''
buf += s buf += s
return buf return buf
...@@ -22,7 +22,7 @@ def send(sock, s): ...@@ -22,7 +22,7 @@ def send(sock, s):
count = 0 count = 0
while count < total: while count < total:
try: n = sock.send(s) try: n = sock.send(s)
except (socket.error), e: n = 0 except (socket.error) as e: n = 0
if n == 0: if n == 0:
return count; return count;
count += n count += n
...@@ -39,10 +39,10 @@ try: ...@@ -39,10 +39,10 @@ try:
except socket.error as e: except socket.error as e:
sys.exit(1) sys.exit(1)
buf = '' buf = b''
n = 0 n = 0
while n < 1000: while n < 1000:
buf += '+' buf += b'+'
n += 1 n += 1
sock.settimeout(1); sock.settimeout(1);
......
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
# #
...@@ -9,11 +9,11 @@ import subprocess ...@@ -9,11 +9,11 @@ import subprocess
import select import select
def read(sock, n): def read(sock, n):
buf = '' buf = b''
while len(buf) < n: while len(buf) < n:
rem = n - len(buf) rem = n - len(buf)
try: s = sock.recv(rem) try: s = sock.recv(rem)
except (socket.error), e: return '' except (socket.error) as e: return b''
buf += s buf += s
return buf return buf
...@@ -22,7 +22,7 @@ def send(sock, s): ...@@ -22,7 +22,7 @@ def send(sock, s):
count = 0 count = 0
while count < total: while count < total:
try: n = sock.send(s) try: n = sock.send(s)
except (socket.error), e: n = 0 except (socket.error) as e: n = 0
if n == 0: if n == 0:
return count; return count;
count += n count += n
...@@ -43,7 +43,7 @@ host = socket.gethostname() ...@@ -43,7 +43,7 @@ host = socket.gethostname()
try: serverSocket.bind((host, 0)) try: serverSocket.bind((host, 0))
except socket.error as msg: except socket.error as msg:
print 'bind fails: ', msg print('bind fails: ' + str(msg))
sn = serverSocket.getsockname() sn = serverSocket.getsockname()
serverPort = sn[1] serverPort = sn[1]
...@@ -51,10 +51,10 @@ serverPort = sn[1] ...@@ -51,10 +51,10 @@ serverPort = sn[1]
cmdStr = ("./tcp_client.py %d &") % (serverPort) cmdStr = ("./tcp_client.py %d &") % (serverPort)
os.system(cmdStr) os.system(cmdStr)
buf = '' buf = b''
n = 0 n = 0
while n < 500: while n < 500:
buf += '.' buf += b'.'
n += 1 n += 1
serverSocket.listen(MAX_PORTS) serverSocket.listen(MAX_PORTS)
...@@ -79,5 +79,5 @@ while True: ...@@ -79,5 +79,5 @@ while True:
serverSocket.close() serverSocket.close()
sys.exit(0) sys.exit(0)
else: else:
print 'Select timeout!' print('Select timeout!')
sys.exit(1) sys.exit(1)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment