本文整理匯總了Python中socket.ntohl方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.ntohl方法的具體用法?Python socket.ntohl怎麽用?Python socket.ntohl使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socket
的用法示例。
在下文中一共展示了socket.ntohl方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_netmask
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def get_netmask(self):
"""
Get ip network netmask
"""
if not CTYPES_SUPPORT:
raise exceptions.TestSkipError("Getting the netmask requires "
"python > 2.4")
ifreq = struct.pack('16sH14s', self.name.encode(),
socket.AF_INET, b'\x00' * 14)
try:
res = fcntl.ioctl(sockfd, arch.SIOCGIFNETMASK, ifreq)
except IOError:
return 0
netmask = socket.ntohl(struct.unpack('16sH2xI8x', res)[2])
return 32 - int(math.log(ctypes.c_uint32(~netmask).value + 1, 2))
示例2: get_orig_dst
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [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!")
示例3: receive
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def receive(channel):
""" Receive a message from a channel """
size = struct.calcsize("L")
size = channel.recv(size)
try:
size = socket.ntohl(struct.unpack("L", size)[0])
except struct.error as e:
return ''
buf = ""
while len(buf) < size:
buf = channel.recv(size - len(buf))
return pickle.loads(buf)[0]
示例4: _decrypt
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def _decrypt(self, text, _id, exception=None):
text = to_binary(text)
plain_text = self.cipher.decrypt(base64.b64decode(text))
padding = plain_text[-1]
content = plain_text[16:-padding]
xml_length = socket.ntohl(struct.unpack(b"I", content[:4])[0])
xml_content = to_text(content[4 : xml_length + 4])
from_id = to_text(content[xml_length + 4 :])
if from_id != _id:
exception = exception or Exception
raise exception()
return xml_content
示例5: decrypt
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def decrypt(self, text, appid):
"""對解密後的明文進行補位刪除
@param text: 密文
@return: 刪除填充補位後的明文
"""
try:
cryptor = AES.new(self.key, self.mode, self.key[:16])
# 使用BASE64對密文進行解碼,然後AES-CBC解密
plain_text = cryptor.decrypt(base64.b64decode(text))
except Exception as e:
print(e)
return ierror.WXBizMsgCrypt_DecryptAES_Error, None
try:
# pad = ord(plain_text[-1])
pad = plain_text[-1]
# 去掉補位字符串
# pkcs7 = PKCS7Encoder()
# plain_text = pkcs7.encode(plain_text)
# 去除16位隨機字符串
content = plain_text[16:-pad]
xml_len = socket.ntohl(struct.unpack("I", content[: 4])[0])
xml_content = content[4: xml_len + 4]
from_appid = content[xml_len + 4:]
except Exception as e:
return ierror.WXBizMsgCrypt_IllegalBuffer, None
if from_appid != appid:
return ierror.WXBizMsgCrypt_ValidateAppid_Error, None
return 0, xml_content.decode()
示例6: ntoh
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def ntoh(value):
size = value.obj_size
if size == 2:
return socket.ntohs(value.v())
elif size == 4:
return socket.ntohl(value.v())
from rekall import obj
return obj.NoneObject("Not a valid integer")
示例7: reverse
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def reverse(self, val):
if self._size == 16:
val = socket.ntohs(val)
elif self._size == 32:
val = socket.ntohl(val)
return val
示例8: testNtoH
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def testNtoH(self):
# This just checks that htons etc. are their own inverse,
# when looking at the lower 16 or 32 bits.
sizes = {socket.htonl: 32, socket.ntohl: 32,
socket.htons: 16, socket.ntohs: 16}
for func, size in sizes.items():
mask = (1L<<size) - 1
for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
self.assertEqual(i & mask, func(func(i&mask)) & mask)
swapped = func(mask)
self.assertEqual(swapped & mask, mask)
self.assertRaises(OverflowError, func, 1L<<34)
示例9: testNtoHErrors
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def testNtoHErrors(self):
good_values = [ 1, 2, 3, 1L, 2L, 3L ]
bad_values = [ -1, -2, -3, -1L, -2L, -3L ]
for k in good_values:
socket.ntohl(k)
socket.ntohs(k)
socket.htonl(k)
socket.htons(k)
for k in bad_values:
self.assertRaises(OverflowError, socket.ntohl, k)
self.assertRaises(OverflowError, socket.ntohs, k)
self.assertRaises(OverflowError, socket.htonl, k)
self.assertRaises(OverflowError, socket.htons, k)
示例10: Ip2Int
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def Ip2Int(ip):
return socket.ntohl(struct.unpack("I",socket.inet_aton(str(ip)))[0])
示例11: reverse
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def reverse(self, val):
if self.size == 16:
val = socket.ntohs(val)
elif self.size == 32:
val = socket.ntohl(val)
return val
示例12: _split_addrport
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def _split_addrport(self, addrtext):
addr_s, port_s = addrtext.split(":")
return ntohl(int(addr_s, 16)), int(port_s, 16)
示例13: update
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def update(self, filt=None):
"""update([filter])
Update the RouteTable with current values. If a filter function is supplied
then it will be called with a RouteEntry and must return a true or false value.
If true, the RouterEntry will be included in the table. If false, it will not.
"""
self._entries = []
lines = open(FILE).readlines()
for line in lines[1:]:
[iface, dest, gateway, flags, refcnt, use, metric, mask,
mtu, window, irtt] = line.split()
rt = RouteEntry(iface,
Destination=ntohl(int(dest, 16)),
Gateway=ntohl(int(gateway, 16)),
Flags=RouteFlags(flags),
RefCnt=int(refcnt, 16),
Use=int(use, 16),
Metric=int(metric, 16),
Mask=ntohl(int(mask, 16)),
MTU=int(mtu, 16),
Window=int(window, 16),
IRTT=int(irtt, 16))
if filt:
if filt(rt):
self._entries.append(rt)
else:
self._entries.append(rt)
示例14: _parse_l3_proto
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def _parse_l3_proto(self, event):
if event.l3_protocol == 0x0008: # IPv4
saddr = ipaddress.IPv4Address(socket.ntohl(event.addrs.v4.saddr))
daddr = ipaddress.IPv4Address(socket.ntohl(event.addrs.v4.daddr))
elif event.l3_protocol == 0xDD86: # IPv6
saddr = ipaddress.IPv6Address(bytes(event.addrs.v6.saddr))
daddr = ipaddress.IPv6Address(bytes(event.addrs.v6.daddr))
else:
print(f"Unsupported l3 protocol {event.l3_protocol}")
exit(1)
return (str(saddr), str(daddr))
示例15: reverse
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import ntohl [as 別名]
def reverse(self, val):
if self.size == 16:
# Replaces socket.ntohs (but work on both little/big endian)
val = struct.unpack('>H', struct.pack('<H', int(val)))[0]
elif self.size == 32:
# Same here but for socket.ntohl
val = struct.unpack('>I', struct.pack('<I', int(val)))[0]
return val