本文整理匯總了Python中socket.inet_ntop方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.inet_ntop方法的具體用法?Python socket.inet_ntop怎麽用?Python socket.inet_ntop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socket
的用法示例。
在下文中一共展示了socket.inet_ntop方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: mac_set_ip_address
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def mac_set_ip_address(self, dev, ip, serverip, netmask):
ifr = struct.pack('<16sBBHIIIBBHIIIBBHIII',
self.iface_name,
16, socket.AF_INET, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, ip))[0], 0, 0,
16, socket.AF_INET, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, serverip))[0], 0, 0,
16, 0, 0, struct.unpack('<L', socket.inet_pton(socket.AF_INET, "255.255.255.255"))[0], 0, 0)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
fcntl.ioctl(sock, self.IOCTL_MACOSX_SIOCAIFADDR, ifr)
except Exception as e:
common.internal_print("Something went wrong with setting up the interface.", -1)
print(e)
sys.exit(-1)
# adding new route for forwarding packets properly.
integer_ip = struct.unpack(">I", socket.inet_pton(socket.AF_INET, serverip))[0]
rangeip = socket.inet_ntop(socket.AF_INET, struct.pack(">I", integer_ip & ((2**int(netmask))-1)<<32-int(netmask)))
ps = subprocess.Popen(["route", "add", "-net", rangeip+"/"+netmask, serverip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if stderr:
if not "File exists" in stderr:
common.internal_print("Error: adding client route: {0}".format(stderr), -1)
sys.exit(-1)
return
示例2: bin2ip
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def bin2ip(ipval):
"""Converts a 16-bytes binary blob to an IPv4 or IPv6 standard
representation. See ip2bin().
"""
try:
socket.inet_aton(ipval)
return ipval
except (TypeError, socket.error):
pass
try:
socket.inet_pton(socket.AF_INET6, ipval)
return ipval
except (TypeError, socket.error):
pass
try:
return int2ip(ipval)
except TypeError:
pass
if ipval[:12] == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff':
return socket.inet_ntoa(ipval[12:])
return socket.inet_ntop(socket.AF_INET6, ipval)
示例3: testStringToIPv6
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def testStringToIPv6(self):
try:
from socket import inet_ntop, AF_INET6, has_ipv6
if not has_ipv6:
self.skipTest('IPv6 not available')
except ImportError:
self.skipTest('could not import needed symbols from socket')
f = lambda a: inet_ntop(AF_INET6, a)
self.assertEqual('::', f('\x00' * 16))
self.assertEqual('::1', f('\x00' * 15 + '\x01'))
self.assertEqual(
'aef:b01:506:1001:ffff:9997:55:170',
f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
)
# XXX The following don't test module-level functionality...
示例4: canonicalize
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def canonicalize(self, ip_str):
"""
Parse an IP string for listen to IPAddress content.
"""
try:
if ':' in ip_str:
self.version = 'ipv6'
if '%' in ip_str:
ip_str, scope = ip_str.split('%')
self.scope = int(scope)
self.packed_addr = socket.inet_pton(socket.AF_INET6, ip_str)
self.addr = socket.inet_ntop(socket.AF_INET6, self.packed_addr)
else:
self.version = 'ipv4'
self.packed_addr = socket.inet_pton(socket.AF_INET, ip_str)
self.addr = socket.inet_ntop(socket.AF_INET, self.packed_addr)
except socket.error as detail:
if 'illegal IP address' in str(detail):
self.addr = ip_str
self.version = 'hostname'
示例5: testStringToIPv6
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def testStringToIPv6(self):
if not hasattr(socket, 'inet_ntop'):
return # No inet_ntop() on this platform
try:
from socket import inet_ntop, AF_INET6, has_ipv6
if not has_ipv6:
return
except ImportError:
return
f = lambda a: inet_ntop(AF_INET6, a)
self.assertEqual('::', f('\x00' * 16))
self.assertEqual('::1', f('\x00' * 15 + '\x01'))
self.assertEqual(
'aef:b01:506:1001:ffff:9997:55:170',
f('\x0a\xef\x0b\x01\x05\x06\x10\x01\xff\xff\x99\x97\x00\x55\x01\x70')
)
# XXX The following don't test module-level functionality...
示例6: read_address
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def read_address(self):
atype = await self.read_response(1)
if atype[0] == c.SOCKS5_ATYP_IPv4:
addr = socket.inet_ntoa((await self.read_response(4)))
elif atype[0] == c.SOCKS5_ATYP_DOMAIN:
length = await self.read_response(1)
addr = await self.read_response(ord(length))
elif atype[0] == c.SOCKS5_ATYP_IPv6:
addr = await self.read_response(16)
addr = socket.inet_ntop(socket.AF_INET6, addr)
else:
raise InvalidServerReply('SOCKS5 proxy server sent invalid data')
port = await self.read_response(2)
port = struct.unpack('>H', port)[0]
return addr, port
示例7: socket5proxy
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def socket5proxy(self):
try:
sock = self.connection
addrtype = ord(sock.recv(1))
if addrtype > 4:
return addrtype
if addrtype == 1:
addr = socket.inet_ntoa(self.rfile.read(4))
elif addrtype == 3:
addr = self.rfile.read(ord(sock.recv(1)))
elif addrtype == 4:
addr = socket.inet_ntop(socket.AF_INET6,self.rfile.read(16))
else:
# not support
logging.warn('addr_type not support')
return
port = struct.unpack('>H', self.rfile.read(2))
try:
logging.info('connecting %s:%d' % (addr, port[0]))
remote = socket.create_connection((addr, port[0]))
except socket.error, e:
logging.warn(e)
return
self.handle_socket5(sock, remote)
示例8: parse_ipv6
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def parse_ipv6(
self,
packed_data, # type: struct
pointer, # type: int
field_size # type: int
):
"""
Unpack an IPv6 address
Args:
packed_data (struct): Packed data
pointer (int): Current unpack location
field_size (int): Length of data to unpack
Returns:
str: IPv4 address
"""
payload = self.socket.inet_ntop(self.socket.AF_INET6,packed_data[pointer:pointer+field_size])
return payload
### Generic MAC Address Parsers ###
示例9: convert_ip
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def convert_ip(value):
"""Convert an IP address from binary to text.
:param value: Raw binary data to convert
:type value: str
:returns: str
"""
try:
return socket.inet_ntop(socket.AF_INET, value)
except ValueError:
return socket.inet_ntop(socket.AF_INET6, value)
示例10: lin_set_ip_address
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def lin_set_ip_address(self, dev, ip, serverip, netmask):
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# set IP
ifr = struct.pack('<16sH2s4s8s', dev, socket.AF_INET, "\x00"*2, socket.inet_aton(ip), "\x00"*8)
fcntl.ioctl(sockfd, self.IOCTL_LINUX_SIOCSIFADDR, ifr)
# get flags
ifr = struct.pack('<16sh', dev, 0)
flags = struct.unpack('<16sh', fcntl.ioctl(sockfd, self.IOCTL_LINUX_SIOCSIFFLAGS, ifr))[1]
# set new flags
flags = flags | self.IOCTL_LINUX_IFF_UP
ifr = struct.pack('<16sh', dev, flags)
# iface up
fcntl.ioctl(sockfd, self.IOCTL_LINUX_SIOCSIFFLAGS, ifr)
except Exception as e:
common.internal_print("Something went wrong with setting up the interface.", -1)
print(e)
sys.exit(-1)
# adding new route for forwarding packets properly.
integer_ip = struct.unpack(">I", socket.inet_pton(socket.AF_INET, serverip))[0]
rangeip = socket.inet_ntop(socket.AF_INET, struct.pack(">I", integer_ip & ((2**int(netmask))-1)<<32-int(netmask)))
integer_netmask = struct.pack(">I", ((2**int(netmask))-1)<<32-int(netmask))
netmask = socket.inet_ntoa(integer_netmask)
ps = subprocess.Popen(["route", "add", "-net", rangeip, "netmask", netmask, "dev", dev], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if stderr:
if not "File exists" in stderr:
common.internal_print("Error: adding client route: {0}".format(stderr), -1)
sys.exit(-1)
return
示例11: freebsd_set_ip_address
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def freebsd_set_ip_address(self, dev, ip, serverip, netmask):
sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
#set ip, serverip and netmask
ifaliasreq = struct.pack('<16sBBHI8sBBHI8sBBHI8sI', self.iface_name,
16, socket.AF_INET, 0, struct.unpack('<I', socket.inet_aton(ip))[0], '\x00'*8,
16, socket.AF_INET, 0, struct.unpack('<I', socket.inet_aton(serverip))[0], '\x00'*8,
16, socket.AF_INET, 0, struct.unpack('<I', socket.inet_aton('255.255.255.255'))[0],
'\x00'*8,
0)
fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCAIFADDR, ifaliasreq)
# get flags
ifr = struct.pack('<16sh', self.iface_name, 0)
flags = struct.unpack('<16sh', fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCGIFFLAGS, ifr))[1]
# set new flags
flags = flags | self.IOCTL_FREEBSD_IFF_UP
ifr = struct.pack('<16sh', self.iface_name, flags)
# iface up
fcntl.ioctl(sockfd, self.IOCTL_FREEBSD_SIOCSIFFLAGS, ifr)
except Exception as e:
common.internal_print("Something went wrong with setting up the interface.", -1)
print(e)
sys.exit(-1)
# adding new route for forwarding packets properly.
integer_ip = struct.unpack(">I", socket.inet_pton(socket.AF_INET, serverip))[0]
rangeip = socket.inet_ntop(socket.AF_INET, struct.pack(">I", integer_ip & ((2**int(netmask))-1)<<32-int(netmask)))
ps = subprocess.Popen(["route", "add", "-net", rangeip+"/"+netmask, serverip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = ps.communicate()
if stderr:
if not "File exists" in stderr:
common.internal_print("Error: adding client route: {0}".format(stderr), -1)
sys.exit(-1)
return
示例12: canonicalize_ip_address
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def canonicalize_ip_address(address):
if ":" in address:
family = socket.AF_INET6
else:
family = socket.AF_INET
return socket.inet_ntop(family, socket.inet_pton(family, address))
示例13: normalize_ipaddress
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def normalize_ipaddress(ip_string):
try:
packed = socket.inet_pton(socket.AF_INET, ip_string)
ip_string = socket.inet_ntop(socket.AF_INET, packed)
ipv6_words = None
ipv4_int = struct.unpack("!L", packed)[0]
except socket.error:
packed = socket.inet_pton(socket.AF_INET6, ip_string)
ip_string = socket.inet_ntop(socket.AF_INET6, packed)
ipv6_words = struct.unpack("!8H", packed)
ipv4_int = None
return ip_string, ipv4_int, ipv6_words
示例14: stream_deserialize
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def stream_deserialize(cls, f, without_time=False):
c = cls()
if c.protover >= CADDR_TIME_VERSION and not without_time:
c.nTime = struct.unpack(b"<I", ser_read(f, 4))[0]
c.nServices = struct.unpack(b"<Q", ser_read(f, 8))[0]
packedIP = ser_read(f, 16)
if bytes(packedIP[0:12]) == IPV4_COMPAT: # IPv4
c.ip = socket.inet_ntop(socket.AF_INET, packedIP[12:16])
else: #IPv6
c.ip = socket.inet_ntop(socket.AF_INET6, packedIP)
c.port = struct.unpack(b">H", ser_read(f, 2))[0]
return c
示例15: parse_ip
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import inet_ntop [as 別名]
def parse_ip(addrtype, data, length, offset):
if addrtype == QTYPE_A:
return socket.inet_ntop(socket.AF_INET, data[offset:offset + length])
elif addrtype == QTYPE_AAAA:
return socket.inet_ntop(socket.AF_INET6, data[offset:offset + length])
elif addrtype in [QTYPE_CNAME, QTYPE_NS]:
return parse_name(data, offset)[1]
else:
return data[offset:offset + length]