Creating Sockets Using Python Scripts

Creating a socket in a Python script defaults to the management interface. You can change the VR when the socket is create by using the following example:

Example: changing the VR to VR-Default (VRID = 2) for an UDP socket:
import socket
EXTREME_SO_VRID = 37 

udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
    f = open('/proc/self/ns_id', 'w')   
    f.write('2\n')    
    f.close()
except:
    udp_sock.setsockopt(socket.SOL_SOCKET, EXTREME_SO_VRID, 2)

 # your program here
Note

Note

Do not use os.system('echo 2 > /proc/self/ns_id‘) to do this because os.system() spawns a subprocess. The subprocess will switch to vr-default, and then exit. The calling process will still be using the same VR as before.

Finding the VR ID of any VR can be accomplished with the following example:

def vrNameToNumber(vrName):
    vrId = None
    with open('/proc/net/vr','r') as f:
      for l in f.readlines():
            rslt = l.split()
            if rslt[3] == vrName:
               vrId =  int(rslt[0]
               break
        return vrId