Skip to content

Commit e66565f

Browse files
jeremyclineborkmann
authored andcommitted
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: Jeremy Cline <jcline@redhat.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 2cc512c commit e66565f

2 files changed

Lines changed: 14 additions & 14 deletions

File tree

‎tools/testing/selftests/bpf/tcp_client.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
22
#
33
# SPDX-License-Identifier: GPL-2.0
44
#
@@ -9,11 +9,11 @@
99
import select
1010

1111
def read(sock, n):
12-
buf = ''
12+
buf = b''
1313
while len(buf) < n:
1414
rem = n - len(buf)
1515
try: s = sock.recv(rem)
16-
except (socket.error), e: return ''
16+
except (socket.error) as e: return b''
1717
buf += s
1818
return buf
1919

@@ -22,7 +22,7 @@ def send(sock, s):
2222
count = 0
2323
while count < total:
2424
try: n = sock.send(s)
25-
except (socket.error), e: n = 0
25+
except (socket.error) as e: n = 0
2626
if n == 0:
2727
return count;
2828
count += n
@@ -39,10 +39,10 @@ def send(sock, s):
3939
except socket.error as e:
4040
sys.exit(1)
4141

42-
buf = ''
42+
buf = b''
4343
n = 0
4444
while n < 1000:
45-
buf += '+'
45+
buf += b'+'
4646
n += 1
4747

4848
sock.settimeout(1);

‎tools/testing/selftests/bpf/tcp_server.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python2
1+
#!/usr/bin/env python3
22
#
33
# SPDX-License-Identifier: GPL-2.0
44
#
@@ -9,11 +9,11 @@
99
import select
1010

1111
def read(sock, n):
12-
buf = ''
12+
buf = b''
1313
while len(buf) < n:
1414
rem = n - len(buf)
1515
try: s = sock.recv(rem)
16-
except (socket.error), e: return ''
16+
except (socket.error) as e: return b''
1717
buf += s
1818
return buf
1919

@@ -22,7 +22,7 @@ def send(sock, s):
2222
count = 0
2323
while count < total:
2424
try: n = sock.send(s)
25-
except (socket.error), e: n = 0
25+
except (socket.error) as e: n = 0
2626
if n == 0:
2727
return count;
2828
count += n
@@ -43,18 +43,18 @@ def send(sock, s):
4343

4444
try: serverSocket.bind((host, 0))
4545
except socket.error as msg:
46-
print 'bind fails: ', msg
46+
print('bind fails: ' + str(msg))
4747

4848
sn = serverSocket.getsockname()
4949
serverPort = sn[1]
5050

5151
cmdStr = ("./tcp_client.py %d &") % (serverPort)
5252
os.system(cmdStr)
5353

54-
buf = ''
54+
buf = b''
5555
n = 0
5656
while n < 500:
57-
buf += '.'
57+
buf += b'.'
5858
n += 1
5959

6060
serverSocket.listen(MAX_PORTS)
@@ -79,5 +79,5 @@ def send(sock, s):
7979
serverSocket.close()
8080
sys.exit(0)
8181
else:
82-
print 'Select timeout!'
82+
print('Select timeout!')
8383
sys.exit(1)

0 commit comments

Comments
 (0)