本文整理匯總了Python中socket.SOL_IP屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.SOL_IP屬性的具體用法?Python socket.SOL_IP怎麽用?Python socket.SOL_IP使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.SOL_IP屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ready_to_read
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def ready_to_read(self):
while True:
ancillary_size = socket.CMSG_LEN(self.MAX_SIZE)
try:
message, ancillary_messages, _msg_flags, from_info = \
self.sock.recvmsg(self.MAX_SIZE, ancillary_size)
except (IOError, OSError) as err:
if err.args[0] != errno.EWOULDBLOCK:
self.warning("Socket receive failed: %s", err)
return
if not MACOS:
rx_interface_index = None
for anc in ancillary_messages:
# pylint:disable=no-member
if anc[0] == socket.SOL_IP and anc[1] == socket.IP_PKTINFO:
packet_info = in_pktinfo.from_buffer_copy(anc[2])
rx_interface_index = packet_info.ipi_ifindex
elif anc[0] == socket.SOL_IPV6 and anc[1] == socket.IPV6_PKTINFO:
packet_info = in6_pktinfo.from_buffer_copy(anc[2])
rx_interface_index = packet_info.ipi6_ifindex
if rx_interface_index and (rx_interface_index != self._interface_index):
# Message received on "wrong" interface; ignore
return
self._receive_function(message, from_info, self.sock)
示例2: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def __init__(self, type=ETH_P_IP, filter=None, iface=None, promisc=None, nofilter=0): # noqa: E501
self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) # noqa: E501
self.outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) # noqa: E501
self.iface = iface
if iface is not None:
self.ins.bind((self.iface, type))
if not six.PY2:
try:
# Receive Auxiliary Data (VLAN tags)
self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1)
self.ins.setsockopt(
socket.SOL_SOCKET,
SO_TIMESTAMPNS,
1
)
self.auxdata_available = True
except OSError:
# Note: Auxiliary Data is only supported since
# Linux 2.6.21
msg = "Your Linux Kernel does not support Auxiliary Data!"
log_runtime.info(msg)
示例3: get_orig_dst
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def get_orig_dst(sock):
own_addr = sock.getsockname()[0]
own_af = detect_af(own_addr)
if own_af == socket.AF_INET:
buf = sock.getsockopt(socket.SOL_IP, constants.SO_ORIGINAL_DST, sockaddr_size)
sa = sockaddr_in.from_buffer_copy(buf)
addr = socket.ntohl(sa.sin_addr)
addr = str(addr >> 24) + '.' + str((addr >> 16) & 0xFF) + '.' + str((addr >> 8) & 0xFF) + '.' + str(addr & 0xFF)
port = socket.ntohs(sa.sin_port)
return addr, port
elif own_af == socket.AF_INET6:
buf = sock.getsockopt(constants.SOL_IPV6, constants.SO_ORIGINAL_DST, sockaddr6_size)
sa = sockaddr_in6.from_buffer_copy(buf)
addr = socket.inet_ntop(socket.AF_INET6, sa.sin6_addr)
port = socket.ntohs(sa.sin_port)
return addr, port
else:
raise RuntimeError("Unknown address family!")
示例4: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def __init__(self, sock=None, map=None, conn=True, verbose=False):
self.out_buffer=b""
self.verbose=verbose
self.allsent=False
if conn is True:
#get the original dst address and port
odestdata = sock.getsockopt(socket.SOL_IP, SO_ORIGINAL_DST, 16)
_, port, a1, a2, a3, a4 = struct.unpack("!HHBBBBxxxxxxxx", odestdata)
address = "%d.%d.%d.%d" % (a1, a2, a3, a4)
if self.verbose:
display('[+] Forwarding incoming connection from %s to %s through the proxy' % (repr(sock.getpeername()), (address, port)))
#connect to the original dst :
self.conn_sock = socks.socksocket()
#self.conn_sock.settimeout(15)
self.conn_sock.connect((address, port))
self.sock_class=Socks5Conn(sock=self.conn_sock, conn=self) #add a dispatcher to handle the other side
else:
self.sock_class=conn
self.conn_sock=None
asyncore.dispatcher.__init__(self, sock, map)
示例5: enablePktInfo
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def enablePktInfo(self, flag=1):
if not hasattr(self.socket, 'sendmsg') or \
not hasattr(self.socket, 'recvmsg'):
raise error.CarrierError('sendmsg()/recvmsg() interface is not supported by this OS and/or Python version')
try:
if self.socket.family in (socket.AF_INET,socket.AF_INET6):
self.socket.setsockopt(socket.SOL_IP, socket.IP_PKTINFO, flag)
if self.socket.family == socket.AF_INET6:
self.socket.setsockopt(socket.SOL_IPV6, socket.IPV6_RECVPKTINFO, flag)
except socket.error:
raise error.CarrierError('setsockopt() for %s failed: %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", sys.exc_info()[1]))
self._sendto = sockmsg.getSendTo(self.addressType)
self._recvfrom = sockmsg.getRecvFrom(self.addressType)
debug.logger & debug.flagIO and debug.logger('enablePktInfo: %s option %s on socket %s' % (self.socket.family == socket.AF_INET6 and "IPV6_RECVPKTINFO" or "IP_PKTINFO", flag and "enabled" or "disabled", self.socket.fileno()))
return self
示例6: enableTransparent
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def enableTransparent(self, flag=1):
try:
if self.socket.family == socket.AF_INET:
self.socket.setsockopt(
socket.SOL_IP, socket.IP_TRANSPARENT, flag
)
if self.socket.family == socket.AF_INET6:
self.socket.setsockopt(
socket.SOL_IPV6, socket.IP_TRANSPARENT, flag
)
except socket.error:
raise error.CarrierError('setsockopt() for IP_TRANSPARENT failed: %s' % sys.exc_info()[1])
except PermissionError:
raise error.CarrierError('IP_TRANSPARENT socket option requires superusre previleges')
debug.logger & debug.flagIO and debug.logger('enableTransparent: %s option IP_TRANSPARENT on socket %s' % (flag and "enabled" or "disabled", self.socket.fileno()))
return self
示例7: getSendTo
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def getSendTo(addressType):
def sendto(s, _data, _to):
ancdata = []
if type(_to) == addressType:
addr = ipaddress.ip_address(_to.getLocalAddress()[0])
else:
addr = ipaddress.ip_address(s.getsockname()[0])
if type(addr) == ipaddress.IPv4Address:
_f = in_pktinfo()
_f.ipi_spec_dst = in_addr.from_buffer_copy(addr.packed)
ancdata = [(socket.SOL_IP, socket.IP_PKTINFO, memoryview(_f).tobytes())]
elif s.family == socket.AF_INET6 and type(addr) == ipaddress.IPv6Address:
_f = in6_pktinfo()
_f.ipi6_addr = in6_addr.from_buffer_copy(addr.packed)
ancdata = [(socket.SOL_IPV6, socket.IPV6_PKTINFO, memoryview(_f).tobytes())]
return s.sendmsg([_data], ancdata, 0, _to)
return sendto
示例8: bind
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def bind(self, addr, port, tos, ttl, df):
log.debug(
"bind(addr=%s, port=%d, tos=%d, ttl=%d)", addr, port, tos, ttl)
self.socket = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_TOS, tos)
self.socket.setsockopt(socket.SOL_IP, socket.IP_TTL, ttl)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((addr, port))
if df:
if (sys.platform == "linux2"):
self.socket.setsockopt(socket.SOL_IP, 10, 2)
elif (sys.platform == "win32"):
self.socket.setsockopt(socket.SOL_IP, 14, 1)
elif (sys.platform == "darwin"):
log.error("do-not-fragment can not be set on darwin")
else:
log.error("unsupported OS, ignore do-not-fragment option")
else:
if (sys.platform == "linux2"):
self.socket.setsockopt(socket.SOL_IP, 10, 0)
示例9: try_redirect
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def try_redirect(self):
if not self.support_redirect:
return False
try:
dst = self.conn.getsockopt(socket.SOL_IP, SO_ORIGINAL_DST, 16)
except:
self.support_redirect = False
return False
try:
dst_port, srv_ip = struct.unpack("!2xH4s8x", dst)
ip_str = socket.inet_ntoa(srv_ip)
if dst_port != g.config.proxy_port and not utils.is_private_ip(ip_str):
xlog.debug("Redirect to:%s:%d from:%s", ip_str, dst_port, self.client_address)
handle_ip_proxy(self.conn, ip_str, dst_port, self.client_address)
return True
else:
return False
except Exception as e:
xlog.exception("redirect except:%r", e)
return True
示例10: addListener
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def addListener(self, addr, port, service):
if self.isBroadcast(addr):
self.etherAddrs[addr] = self.broadcastIpToMac(addr)
elif self.isMulticast(addr):
self.etherAddrs[addr] = self.multicastIpToMac(addr)
else:
# unicast -- we don't know yet which IP we'll want to send to
self.etherAddrs[addr] = None
# Set up the receiving socket and corresponding IP and interface information.
# One receiving socket is required per multicast address.
rx = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
rx.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
for interface in self.interfaces:
(ifname, mac, ip, netmask) = self.getInterface(interface)
# Add this interface to the receiving socket's list.
if self.isBroadcast(addr):
rx.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
elif self.isMulticast(addr):
packedAddress = struct.pack('4s4s', socket.inet_aton(addr), socket.inet_aton(ip))
rx.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, packedAddress)
# Generate a transmitter socket. Each interface
# requires its own transmitting socket.
if interface not in self.noTransmitInterfaces:
tx = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
tx.bind((ifname, 0))
self.transmitters.append({'relay': {'addr': addr, 'port': port}, 'interface': ifname, 'addr': ip, 'mac': mac, 'netmask': netmask, 'socket': tx, 'service': service})
rx.bind((addr, port))
self.receivers.append(rx)
示例11: _server_discovery
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def _server_discovery(self):
MULTI_GROUP = ("<broadcast>", 7359)
MESSAGE = "who is EmbyServer?"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(1.0) # This controls the socket.timeout exception
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
LOG.debug("MultiGroup : %s", str(MULTI_GROUP))
LOG.debug("Sending UDP Data: %s", MESSAGE)
servers = []
try:
sock.sendto(MESSAGE, MULTI_GROUP)
except Exception as error:
LOG.error(error)
return servers
while True:
try:
data, addr = sock.recvfrom(1024) # buffer size
servers.append(json.loads(data))
except socket.timeout:
LOG.info("Found Servers: %s", servers)
return servers
except Exception as e:
LOG.error("Error trying to find servers: %s", e)
return servers
示例12: _send_discover
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def _send_discover(self, sock, ssdp_mx):
msg = self.MSEARCH_MSG.format(
host=self.SSDP_ADDRESS, port=self.SSDP_PORT, mx=ssdp_mx)
if self.USE_SINGLE_SOCKET:
for addr in self.addresses:
sock.setsockopt(
socket.SOL_IP, socket.IP_MULTICAST_IF,
socket.inet_aton(addr))
sock.sendto(msg, (self.SSDP_ADDRESS, self.SSDP_PORT))
else:
sock.sendto(msg, (self.SSDP_ADDRESS, self.SSDP_PORT))
示例13: run
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def run(self):
# Listen for UDP port 1900 packets sent to SSDP multicast address
print("UPNP Responder Thread started...")
ssdpmc_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Required for receiving multicast
ssdpmc_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
ssdpmc_socket.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(LISTEN_IP))
ssdpmc_socket.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton("239.255.255.250") + socket.inet_aton(LISTEN_IP))
ssdpmc_socket.bind(("239.255.255.250", 1900))
while True:
try:
data, addr = ssdpmc_socket.recvfrom(1024)
except socket.error as e:
if stop_thread == True:
print("UPNP Reponder Thread closing socket and shutting down...")
ssdpmc_socket.close()
return
print ("UPNP Responder socket.error exception occured: {0}".format(e.__str__))
# SSDP M-SEARCH method received - respond to it unicast with our info
if "M-SEARCH" in data.decode('utf-8'):
print("UPNP Responder sending response to {0}:{1}".format(addr[0], addr[1]))
ssdpout_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ssdpout_socket.sendto(self.UPNP_RESPONSE, addr)
ssdpout_socket.close()
示例14: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def __init__(self, type = ETH_P_IP, filter=None, iface=None, promisc=None, nofilter=0):
self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW)
self.outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))
if iface is not None:
self.ins.bind((iface, type))
示例15: close
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SOL_IP [as 別名]
def close(self):
"""Ends the background threads, and prevent this instance from
servicing further queries."""
if not self._GLOBAL_DONE:
self._GLOBAL_DONE = True
# remove service listeners
self.remove_all_service_listeners()
self.unregister_all_services()
# shutdown recv socket and thread
self.engine.del_reader(self._listen_socket)
if self.address_family == socket.AF_INET:
self._listen_socket.setsockopt(socket.SOL_IP, socket.IP_DROP_MEMBERSHIP,
socket.inet_aton(_MDNS_ADDR) + socket.inet_aton('0.0.0.0'))
else:
group = socket.inet_pton(socket.AF_INET6,_MDNS_ADDR_IPV6) + self.ifn
self._listen_socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_LEAVE_GROUP,group)
self._listen_socket.close()
self.engine.join()
# shutdown the rest
self.notify_all()
self.reaper.join()
for s in self._respond_sockets:
s.close()