staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / bpf / tcp_server.py
blob0ca60d193bed3bce196898c108030c48703910ad
1 #!/usr/bin/env python3
3 # SPDX-License-Identifier: GPL-2.0
6 import sys, os, os.path, getopt
7 import socket, time
8 import subprocess
9 import select
11 def read(sock, n):
12 buf = b''
13 while len(buf) < n:
14 rem = n - len(buf)
15 try: s = sock.recv(rem)
16 except (socket.error) as e: return b''
17 buf += s
18 return buf
20 def send(sock, s):
21 total = len(s)
22 count = 0
23 while count < total:
24 try: n = sock.send(s)
25 except (socket.error) as e: n = 0
26 if n == 0:
27 return count;
28 count += n
29 return count
32 SERVER_PORT = 12877
33 MAX_PORTS = 2
35 serverPort = SERVER_PORT
36 serverSocket = None
38 # create passive socket
39 serverSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
41 try: serverSocket.bind(('localhost', 0))
42 except socket.error as msg:
43 print('bind fails: ' + str(msg))
45 sn = serverSocket.getsockname()
46 serverPort = sn[1]
48 cmdStr = ("./tcp_client.py %d &") % (serverPort)
49 os.system(cmdStr)
51 buf = b''
52 n = 0
53 while n < 500:
54 buf += b'.'
55 n += 1
57 serverSocket.listen(MAX_PORTS)
58 readList = [serverSocket]
60 while True:
61 readyRead, readyWrite, inError = \
62 select.select(readList, [], [], 2)
64 if len(readyRead) > 0:
65 waitCount = 0
66 for sock in readyRead:
67 if sock == serverSocket:
68 (clientSocket, address) = serverSocket.accept()
69 address = str(address[0])
70 readList.append(clientSocket)
71 else:
72 sock.settimeout(1);
73 s = read(sock, 1000)
74 n = send(sock, buf)
75 sock.close()
76 serverSocket.close()
77 sys.exit(0)
78 else:
79 print('Select timeout!')
80 sys.exit(1)