本文整理汇总了Python中socket.h方法的典型用法代码示例。如果您正苦于以下问题:Python socket.h方法的具体用法?Python socket.h怎么用?Python socket.h使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.h方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_new_address
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def handle_new_address(self, raw_addr, addr_family, interface):
addr = socket.inet_ntop(addr_family, raw_addr)
logger.debug('new address {} on {}'.format(addr, interface.name))
if not self.is_address_handled(raw_addr, addr_family, interface):
logger.debug('ignoring that address on {}'.format(interface.name))
return
# filter out what is not wanted
# Ignore addresses or interfaces we already handle. There can only be
# one multicast handler per address family and network interface
# However, multiple link-local addresses can be
for mch in self.mchs:
if mch.handles(addr_family, addr, interface):
return
logger.debug('handling traffic for {} on {}'.format(
addr, interface.name))
mch = MulticastHandler(addr_family, addr, interface, self.selector)
self.mchs.append(mch)
if not args.no_host:
h = WSDHost(mch)
self.hosts.append(h)
if not args.no_http:
self.http_servers.append(WSDHttpServer(
mch, WSDHttpRequestHandler, mch.family, self.selector))
if args.discovery:
client = WSDClient(mch, self.known_devices)
self.clients.append(client)
示例2: handle_deleted_address
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def handle_deleted_address(self, raw_addr, addr_family, interface):
addr = socket.inet_ntop(addr_family, raw_addr)
logger.warn('deleted address {} on {}'.format(addr, interface.name))
if not self.is_address_handled(addr_family, raw_addr, interface):
return
mch = self.get_mch_by_address(addr_family, addr, interface)
if mch is None:
return
# Do not tear the client/hosts down. Saying goodbye does not work
# because the address is already gone (at least on Linux).
for c in self.clients:
if c.mch == mch:
c.cleanup()
self.clients.remove(c)
break
for h in self.hosts:
if h.mch == mch:
h.cleanup()
self.hosts.remove(h)
break
for s in self.http_servers:
if s.mch == mch:
s.server_close()
self.http_servers.remove(s)
mch.cleanup()
self.mchs.remove(mch)
示例3: cleanup
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def cleanup(self):
for h in self.hosts:
h.teardown()
h.cleanup()
for c in self.clients:
c.teardown()
c.cleanup()
示例4: read_routes
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def read_routes():
f=open("/proc/net/route","r")
routes = []
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",LOOPBACK_NAME))
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifreq2 = ioctl(s, SIOCGIFNETMASK,struct.pack("16s16x",LOOPBACK_NAME))
msk = socket.ntohl(struct.unpack("I",ifreq2[20:24])[0])
dst = socket.ntohl(struct.unpack("I",ifreq[20:24])[0]) & msk
ifaddr = scapy.utils.inet_ntoa(ifreq[20:24])
routes.append((dst, msk, "0.0.0.0", LOOPBACK_NAME, ifaddr))
else:
warning("Interface lo: unkown address family (%i)"% addrfamily)
for l in f.readlines()[1:]:
iff,dst,gw,flags,x,x,x,msk,x,x,x = l.split()
flags = int(flags,16)
if flags & RTF_UP == 0:
continue
if flags & RTF_REJECT:
continue
try:
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",iff))
except IOError: # interface is present in routing tables but does not have any assigned IP
ifaddr="0.0.0.0"
else:
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifaddr = scapy.utils.inet_ntoa(ifreq[20:24])
else:
warning("Interface %s: unkown address family (%i)"%(iff, addrfamily))
continue
routes.append((socket.htonl(long(dst,16))&0xffffffffL,
socket.htonl(long(msk,16))&0xffffffffL,
scapy.utils.inet_ntoa(struct.pack("I",long(gw,16))),
iff, ifaddr))
f.close()
return routes
############
### IPv6 ###
############
示例5: read_routes
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def read_routes():
try:
f=open("/proc/net/route","r")
except IOError:
warning("Can't open /proc/net/route !")
return []
routes = []
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",LOOPBACK_NAME))
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifreq2 = ioctl(s, SIOCGIFNETMASK,struct.pack("16s16x",LOOPBACK_NAME))
msk = socket.ntohl(struct.unpack("I",ifreq2[20:24])[0])
dst = socket.ntohl(struct.unpack("I",ifreq[20:24])[0]) & msk
ifaddr = scapy.utils.inet_ntoa(ifreq[20:24])
routes.append((dst, msk, "0.0.0.0", LOOPBACK_NAME, ifaddr))
else:
warning("Interface lo: unkown address family (%i)"% addrfamily)
for l in f.readlines()[1:]:
iff,dst,gw,flags,x,x,x,msk,x,x,x = l.split()
flags = int(flags,16)
if flags & RTF_UP == 0:
continue
if flags & RTF_REJECT:
continue
try:
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",iff))
except IOError: # interface is present in routing tables but does not have any assigned IP
ifaddr="0.0.0.0"
else:
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifaddr = scapy.utils.inet_ntoa(ifreq[20:24])
else:
warning("Interface %s: unkown address family (%i)"%(iff, addrfamily))
continue
routes.append((socket.htonl(long(dst,16))&0xffffffffL,
socket.htonl(long(msk,16))&0xffffffffL,
scapy.utils.inet_ntoa(struct.pack("I",long(gw,16))),
iff, ifaddr))
f.close()
return routes
############
### IPv6 ###
############
示例6: read_routes
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def read_routes():
try:
f=open("/proc/net/route","rb")
except IOError:
warning("Can't open /proc/net/route !")
return []
routes = []
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",LOOPBACK_NAME.encode('utf-8')))
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifreq2 = ioctl(s, SIOCGIFNETMASK,struct.pack("16s16x",LOOPBACK_NAME.encode('utf-8')))
msk = socket.ntohl(struct.unpack("I",ifreq2[20:24])[0])
dst = socket.ntohl(struct.unpack("I",ifreq[20:24])[0]) & msk
ifaddr = kamene.utils.inet_ntoa(ifreq[20:24])
routes.append((dst, msk, "0.0.0.0", LOOPBACK_NAME, ifaddr))
else:
warning("Interface lo: unkown address family (%i)"% addrfamily)
for l in f.readlines()[1:]:
iff,dst,gw,flags,x,x,x,msk,x,x,x = l.split()
flags = int(flags,16)
if flags & RTF_UP == 0:
continue
if flags & RTF_REJECT:
continue
try:
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",iff))
except IOError: # interface is present in routing tables but does not have any assigned IP
ifaddr="0.0.0.0"
else:
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifaddr = kamene.utils.inet_ntoa(ifreq[20:24])
else:
warning("Interface %s: unkown address family (%i)"%(iff, addrfamily))
continue
routes.append((socket.htonl(int(dst,16))&0xffffffff,
socket.htonl(int(msk,16))&0xffffffff,
kamene.utils.inet_ntoa(struct.pack("I",int(gw,16))),
iff.decode('utf-8'), ifaddr))
f.close()
return routes
############
### IPv6 ###
############
示例7: read_routes
# 需要导入模块: import socket [as 别名]
# 或者: from socket import h [as 别名]
def read_routes():
try:
f=open("/proc/net/route","rb")
except IOError:
warning("Can't open /proc/net/route !")
return []
routes = []
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",LOOPBACK_NAME.encode('utf-8')))
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifreq2 = ioctl(s, SIOCGIFNETMASK,struct.pack("16s16x",LOOPBACK_NAME.encode('utf-8')))
msk = socket.ntohl(struct.unpack("I",ifreq2[20:24])[0])
dst = socket.ntohl(struct.unpack("I",ifreq[20:24])[0]) & msk
ifaddr = scapy.utils.inet_ntoa(ifreq[20:24])
routes.append((dst, msk, "0.0.0.0", LOOPBACK_NAME, ifaddr))
else:
warning("Interface lo: unkown address family (%i)"% addrfamily)
for l in f.readlines()[1:]:
iff,dst,gw,flags,x,x,x,msk,x,x,x = l.split()
flags = int(flags,16)
if flags & RTF_UP == 0:
continue
if flags & RTF_REJECT:
continue
try:
ifreq = ioctl(s, SIOCGIFADDR,struct.pack("16s16x",iff))
except IOError: # interface is present in routing tables but does not have any assigned IP
ifaddr="0.0.0.0"
else:
addrfamily = struct.unpack("h",ifreq[16:18])[0]
if addrfamily == socket.AF_INET:
ifaddr = scapy.utils.inet_ntoa(ifreq[20:24])
else:
warning("Interface %s: unkown address family (%i)"%(iff, addrfamily))
continue
routes.append((socket.htonl(int(dst,16))&0xffffffff,
socket.htonl(int(msk,16))&0xffffffff,
scapy.utils.inet_ntoa(struct.pack("I",int(gw,16))),
iff.decode('utf-8'), ifaddr))
f.close()
return routes
############
### IPv6 ###
############