本文整理汇总了Python中socket.inet_pton方法的典型用法代码示例。如果您正苦于以下问题:Python socket.inet_pton方法的具体用法?Python socket.inet_pton怎么用?Python socket.inet_pton使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.inet_pton方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mac_set_ip_address
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [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: inet_pton
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def inet_pton(_, host):
return socket.inet_aton(host)
# A secure default.
# Sources for more information on TLS ciphers:
#
# - https://wiki.mozilla.org/Security/Server_Side_TLS
# - https://www.ssllabs.com/projects/best-practices/index.html
# - https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
#
# The general intent is:
# - Prefer TLS 1.3 cipher suites
# - prefer cipher suites that offer perfect forward secrecy (DHE/ECDHE),
# - prefer ECDHE over DHE for better performance,
# - prefer any AES-GCM and ChaCha20 over any AES-CBC for better performance and
# security,
# - prefer AES-GCM over ChaCha20 because hardware-accelerated AES is common,
# - disable NULL authentication, MD5 MACs and DSS for security reasons.
示例3: is_ipaddress
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def is_ipaddress(hostname):
"""Detects whether the hostname given is an IP address.
:param str hostname: Hostname to examine.
:return: True if the hostname is an IP address, False otherwise.
"""
if six.PY3 and isinstance(hostname, bytes):
# IDN A-label bytes are ASCII compatible.
hostname = hostname.decode('ascii')
families = [socket.AF_INET]
if hasattr(socket, 'AF_INET6'):
families.append(socket.AF_INET6)
for af in families:
try:
inet_pton(af, hostname)
except (socket.error, ValueError, OSError):
pass
else:
return True
return False
示例4: _create_ipv6_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def _create_ipv6_sockets(loopback_enabled):
# Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
intf_name = find_ethernet_interface()
intf_index = socket.if_nametoindex(intf_name)
mcast_address = "ff02::abcd:99"
port = 30000
group = (mcast_address, port)
txsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, intf_index)
if loopback_enabled:
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
else:
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
txsock.connect(group)
# Open a multicast receive socket and join the group
rxsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
req = struct.pack("=16si", socket.inet_pton(socket.AF_INET6, mcast_address), intf_index)
if platform.system() == "Darwin":
rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, req)
else:
rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_ADD_MEMBERSHIP, req)
rxsock.bind(("::", port))
return (txsock, rxsock)
示例5: recvfrom
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def recvfrom(self, bufsize, flags=0):
if self.type != socket.SOCK_DGRAM:
return _BaseSocket.recvfrom(self, bufsize, flags)
if not self._proxyconn:
self.bind(("", 0))
buf = BytesIO(_BaseSocket.recv(self, bufsize, flags))
buf.seek(+2, SEEK_CUR)
frag = buf.read(1)
if ord(frag):
raise NotImplementedError("Received UDP packet fragment")
fromhost, fromport = self._read_SOCKS5_address(buf)
peerhost, peerport = self.proxy_peername
filterhost = socket.inet_pton(self.family, peerhost).strip(b"\x00")
filterhost = filterhost and fromhost != peerhost
if filterhost or peerport not in (0, fromport):
raise socket.error(EAGAIN, "Packet filtered")
return (buf.read(), (fromhost, fromport))
示例6: is_fdqn
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def is_fdqn(self):
# I will assume the following
# If I can't socket.inet_aton() then it's not an IPv4 address
# Same for ipv6, but since socket.inet_pton is not available in Windows, I'll look for ':'. There can't be
# an FQDN with ':'
# Is it isn't both, then it is a FDQN
try:
socket.inet_aton(self.__target)
except:
# Not an IPv4
try:
self.__target.index(':')
except:
# Not an IPv6, it's a FDQN
return True
return False
示例7: ip2int
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def ip2int(ipstr):
"""Converts the classical decimal, dot-separated, string
representation of an IPv4 address, or the hexadecimal,
colon-separated, string representation of an IPv6 address, to an
integer.
"""
try:
ipstr = ipstr.decode()
except AttributeError:
pass
try:
return struct.unpack('!I', socket.inet_aton(ipstr))[0]
except socket.error:
val1, val2 = struct.unpack(
'!QQ', socket.inet_pton(socket.AF_INET6, ipstr),
)
return (val1 << 64) + val2
示例8: bin2ip
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [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)
示例9: testIPv6toString
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def testIPv6toString(self):
try:
from socket import inet_pton, 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_pton(AF_INET6, a)
self.assertEqual('\x00' * 16, f('::'))
self.assertEqual('\x00' * 16, f('0::0'))
self.assertEqual('\x00\x01' + '\x00' * 14, f('1::'))
self.assertEqual(
'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
)
示例10: canonicalize
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [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'
示例11: is_ip
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def is_ip(ip_addr):
"""
Check if `ip_addr` looks like an IP Address
"""
if sys.version_info[0] < 3:
ip_addr = ip_addr.encode("utf-8")
try:
socket.inet_pton(socket.AF_INET, ip_addr)
return True
except socket.error:
try:
socket.inet_pton(socket.AF_INET6, ip_addr)
return True
except socket.error:
return False
示例12: is_ipaddress
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def is_ipaddress(hostname):
"""Detects whether the hostname given is an IP address.
:param str hostname: Hostname to examine.
:return: True if the hostname is an IP address, False otherwise.
"""
if six.PY3 and isinstance(hostname, six.binary_type):
# IDN A-label bytes are ASCII compatible.
hostname = hostname.decode('ascii')
families = [socket.AF_INET]
if hasattr(socket, 'AF_INET6'):
families.append(socket.AF_INET6)
for af in families:
try:
inet_pton(af, hostname)
except (socket.error, ValueError, OSError):
pass
else:
return True
return False
示例13: is_ip_address
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def is_ip_address(address):
try:
socket.inet_aton(address)
return True
except socket.error:
if ':' in address:
# ':' is not a valid character for a hostname. If we get
# here a few things have to be true:
# - We're on a recent version of python 2.7 (2.7.9+).
# 2.6 and older 2.7 versions don't support SNI.
# - We're on Windows XP or some unusual Unix that doesn't
# have inet_pton.
# - The application is using IPv6 literals with TLS, which
# is pretty unusual.
return True
return False
示例14: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def __init__(self, upstream, cl_ip, cl_port, my_ip, my_port, proto_tag):
self.upstream = upstream
if ":" not in cl_ip:
self.remote_ip_port = b"\x00" * 10 + b"\xff\xff"
self.remote_ip_port += socket.inet_pton(socket.AF_INET, cl_ip)
else:
self.remote_ip_port = socket.inet_pton(socket.AF_INET6, cl_ip)
self.remote_ip_port += int.to_bytes(cl_port, 4, "little")
if ":" not in my_ip:
self.our_ip_port = b"\x00" * 10 + b"\xff\xff"
self.our_ip_port += socket.inet_pton(socket.AF_INET, my_ip)
else:
self.our_ip_port = socket.inet_pton(socket.AF_INET6, my_ip)
self.our_ip_port += int.to_bytes(my_port, 4, "little")
self.out_conn_id = myrandom.getrandbytes(8)
self.proto_tag = proto_tag
示例15: is_ipv4
# 需要导入模块: import socket [as 别名]
# 或者: from socket import inet_pton [as 别名]
def is_ipv4(value):
"""Utility function to detect if a value is a valid IPv4
:param value: The value to match against.
:return: True if the value is a valid IPv4.
"""
try:
socket.inet_pton(socket.AF_INET, value)
except AttributeError: # no inet_pton here, sorry
try:
socket.inet_aton(value)
except socket.error:
return False
return value.count('.') == 3
except socket.error: # not a valid address
return False
return True