python

Mininet: Set Interface MTU

1
2
3
4
5
6
import subprocess
MTU = 9000
net = Mininet( ... )
for intf in [intf for sw in net.switches for intf in sw.intfNames()]:
subprocess.call(['ifconfig', intf, 'mtu', str(MTU)])

Wait for a Local TCP Port with Python

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
#!/usr/bin/env python
import time
def waitForTcpPort(port, timeout=20, poll=0.1):
start = time.time()
with open('/proc/net/tcp','r') as f4, open(
'/proc/net/tcp6', 'r') as f6:
while True:
f4.seek(0)
f6.seek(0)
lines = f4.readlines()[1:] + f6.readlines()[1:]
sockets = map(str.split, lines)
listeners = [l for l in sockets if l[3] == '0A']
ports = [int(l[1].split(':')[1], 16) for l in listeners]
if port in ports:
break
time.sleep(poll)
if time.time() - start > timeout:
raise Exception("Timed out waiting (>%gs) for TCP port %d"
% (timeout, port))
if __name__ == '__main__':
import argparse
p = argparse.ArgumentParser()
p.add_argument('port', help='port to wait for', type=int)
args = p.parse_args()
waitForTcpPort(args.port)

Compile Python 2.7, OpenSSL and zlib with a Custom Prefix

First, export an environment variable with your prefix:

1
export MY_PREFIX=$HOME/python27

Configure, make and install zlib:

1
2
3
4
5
tar xf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure --prefix=$MY_PREFIX
make -j4
make install

Configure, make and install OpenSSL:

1
2
3
4
5
tar xf openssl-1.0.2f.tar.gz
cd openssl-1.0.2f
./config shared --prefix=$MY_PREFIX
make -j4
make install

Configure, make and install Python:

1
2
3
4
5
6
7
8
tar xf Python-2.7.11.tar.xz
cd Python-2.7.11
LDFLAGS="-L$MY_PREFIX/lib -L$MY_PREFIX/lib64 -Wl,-rpath=$MY_PREFIX/lib" \
LD_LIBRARY_PATH="$MY_PREFIX/lib:$MY_PREFIX/lib64" \
CPPFLAGS="-I$MY_PREFIX/include -I$MY_PREFIX/ssl" \
./configure --prefix=$MY_PREFIX --enable-shared
make -j4
make install

Check that it was installed correctly:

1
2
3
4
export PATH=$MY_PREFIX/bin:$PATH
which python
python --version
python