Skip to content

Commit

Permalink
bpf: Add Python 3 support to selftests scripts for bpf
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
jeremycline authored and borkmann committed Jul 25, 2018
1 parent 2cc512c commit e66565f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
12 changes: 6 additions & 6 deletions tools/testing/selftests/bpf/tcp_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
#
# SPDX-License-Identifier: GPL-2.0
#
Expand All @@ -9,11 +9,11 @@
import select

def read(sock, n):
buf = ''
buf = b''
while len(buf) < n:
rem = n - len(buf)
try: s = sock.recv(rem)
except (socket.error), e: return ''
except (socket.error) as e: return b''
buf += s
return buf

Expand All @@ -22,7 +22,7 @@ def send(sock, s):
count = 0
while count < total:
try: n = sock.send(s)
except (socket.error), e: n = 0
except (socket.error) as e: n = 0
if n == 0:
return count;
count += n
Expand All @@ -39,10 +39,10 @@ def send(sock, s):
except socket.error as e:
sys.exit(1)

buf = ''
buf = b''
n = 0
while n < 1000:
buf += '+'
buf += b'+'
n += 1

sock.settimeout(1);
Expand Down
16 changes: 8 additions & 8 deletions tools/testing/selftests/bpf/tcp_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
#
# SPDX-License-Identifier: GPL-2.0
#
Expand All @@ -9,11 +9,11 @@
import select

def read(sock, n):
buf = ''
buf = b''
while len(buf) < n:
rem = n - len(buf)
try: s = sock.recv(rem)
except (socket.error), e: return ''
except (socket.error) as e: return b''
buf += s
return buf

Expand All @@ -22,7 +22,7 @@ def send(sock, s):
count = 0
while count < total:
try: n = sock.send(s)
except (socket.error), e: n = 0
except (socket.error) as e: n = 0
if n == 0:
return count;
count += n
Expand All @@ -43,18 +43,18 @@ def send(sock, s):

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

sn = serverSocket.getsockname()
serverPort = sn[1]

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

buf = ''
buf = b''
n = 0
while n < 500:
buf += '.'
buf += b'.'
n += 1

serverSocket.listen(MAX_PORTS)
Expand All @@ -79,5 +79,5 @@ def send(sock, s):
serverSocket.close()
sys.exit(0)
else:
print 'Select timeout!'
print('Select timeout!')
sys.exit(1)

0 comments on commit e66565f

Please sign in to comment.