本文整理汇总了Python中socket.SO_BINDTODEVICE属性的典型用法代码示例。如果您正苦于以下问题:Python socket.SO_BINDTODEVICE属性的具体用法?Python socket.SO_BINDTODEVICE怎么用?Python socket.SO_BINDTODEVICE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类socket
的用法示例。
在下文中一共展示了socket.SO_BINDTODEVICE属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect_to_dst
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BINDTODEVICE [as 别名]
def connect_to_dst(dst_addr, dst_port):
""" Connect to desired destination """
sock = create_socket()
if OUTGOING_INTERFACE:
try:
sock.setsockopt(
socket.SOL_SOCKET,
socket.SO_BINDTODEVICE,
OUTGOING_INTERFACE.encode(),
)
except PermissionError as err:
print("Only root can set OUTGOING_INTERFACE parameter")
EXIT.set_status(True)
try:
sock.connect((dst_addr, dst_port))
return sock
except socket.error as err:
error("Failed to connect to DST", err)
return 0
示例2: listener_sock
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BINDTODEVICE [as 别名]
def listener_sock(self):
l_sock = socket(AF_INET, SOCK_DGRAM)
l_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
l_sock.setsockopt(SOL_SOCKET, SO_BROADCAST,1)
l_sock.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, f'{self._intf}\0'.encode('utf-8'))
l_sock.bind((str(INADDR_ANY), PROTO.DHCP_SVR))
return l_sock
示例3: initialize_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BINDTODEVICE [as 别名]
def initialize_sockets(self):
try:
# This is needed to join a multicast group
self.mreq = struct.pack("4sl", socket.inet_aton(self.host), socket.INADDR_ANY)
# Set up client socket
self.csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.csock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
# Set up server socket
self.ssock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# BSD systems also need to set SO_REUSEPORT
try:
self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except Exception:
pass
# Only bind to this interface
if self.interface is not None:
print_info("Binding to interface: " + self.interface)
self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,
struct.pack("%ds" % (len(self.interface) + 1,), self.interface))
self.csock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,
struct.pack("%ds" % (len(self.interface) + 1,), self.interface))
try:
self.ssock.bind(('', self.port))
except Exception:
print_warning("failed to bind: " + self.host + ":" + str(self.port) + " ")
try:
self.ssock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self.mreq)
except Exception:
print_warning("failed to join multicast group")
except Exception:
print_error("failed to initialize UPNP sockets")
return False
return True
# Clean up file/socket descriptors
示例4: Main
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BINDTODEVICE [as 别名]
def Main(self):
self.sock = socket(AF_INET, SOCK_DGRAM)
self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
self.sock.setsockopt(SOL_SOCKET, SO_BROADCAST,1)
self.sock.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, self.bind_int)
self.sock.bind(('0.0.0.0', 68))
示例5: _knx_search_worker
# 需要导入模块: import socket [as 别名]
# 或者: from socket import SO_BINDTODEVICE [as 别名]
def _knx_search_worker(self):
"""Send a KnxSearch request to see if target is a KNX device."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setblocking(0)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,
struct.pack('256s', str.encode(self.iface)))
protocol = KnxGatewaySearch(multicast_addr=self.multicast_addr,
port=self.port)
waiter = asyncio.Future(loop=self.loop)
transport = self.loop._make_datagram_transport(
sock, protocol, (self.multicast_addr, self.port), waiter)
try:
# Wait until connection_made() has been called on the transport
yield from waiter
except asyncio.CancelledError:
LOGGER.error('Creating multicast transport failed!')
transport.close()
return
# Wait SEARCH_TIMEOUT seconds for responses to our multicast packets
yield from asyncio.sleep(self.search_timeout)
if protocol.responses:
if True:
# TODO: check if we want diagnostic requests as well
print('sending diagnostic request')
protocol.send_diagnostic_request()
yield from asyncio.sleep(self.search_timeout)
# If protocol received SEARCH_RESPONSE packets, print them
for response in protocol.responses:
peer = response[0]
response = response[1]
t = KnxTargetReport(
host=peer[0],
port=peer[1],
mac_address=response.dib_dev_info.get('knx_mac_address'),
knx_address=response.dib_dev_info.get('knx_address'),
device_serial=response.dib_dev_info.get('knx_device_serial'),
friendly_name=response.dib_dev_info.get('device_friendly_name'),
device_status=response.dib_dev_info.get('device_status'),
knx_medium=response.dib_dev_info.get('knx_medium'),
project_install_identifier=response.dib_dev_info.get('project_install_identifier'),
supported_services=[
KNX_SERVICES[k] for k, v in
response.dib_supp_sv_families.get('families').items()],
bus_devices=[])
self.knx_gateways.append(t)
except asyncio.CancelledError:
pass