user916499 user916499 - 1 year ago 302
Python Question

How to bind socket to an interface in python (socket.SO_BINDTODEVICE missing)

This is probably a very simple thing.
I'm new to python so don't crucify me.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, IN.SO_BINDTODEVICE, "eth1"+'\0')

the above command gives me:

NameError: name 'IN' is not defined

the only import I have is

import socket

Answer Source

If you don't understand the error message, it means you're referring a name IN which is not available at that point. Your code snippet is likely missing an import statement.

The socket module may not offer SO_BINDTODEVICE for portability reasons. If you are absolutely sure that you're running on Linux that supports it, try replacing it with it's numerical value, which is 25:

s.setsockopt(socket.SOL_SOCKET, 25, "eth1"+'\0')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download