本文整理匯總了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 ###
############