本文整理汇总了Python中socket.ntohs方法的典型用法代码示例。如果您正苦于以下问题:Python socket.ntohs方法的具体用法?Python socket.ntohs怎么用?Python socket.ntohs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.ntohs方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: parse
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def parse(self, ip, packet_string):
""" Parse required info from packet according Ethernet Header structure
Reference: http://en.wikipedia.org/wiki/Ethernet_frame#Structure
param: ip(str): local IP address of machine
param: packet_string(str): packet string from packet tuple object
"""
eth_h = packet_string[:ETH_LENGTH]
unpacked_eth_h = struct.unpack('!6s6sH', eth_h)
eth_protocol = socket.ntohs(unpacked_eth_h[2])
if (eth_protocol == 8):
iph_len, packet_protocol, addr = self.parse_ip_header(
packet_string)
if (packet_protocol == 6):
data, ports = self.parse_tcp_header(packet_string, iph_len)
if self.verify_packet_data(ip, data, addr, ports):
self.payload_parser.parse(data, ports)
示例2: _get_pid_port_tcp
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def _get_pid_port_tcp(self, port):
for item in self.get_extended_tcp_table():
lPort = socket.ntohs(item.dwLocalPort)
lAddr = socket.inet_ntoa(struct.pack('L', item.dwLocalAddr))
pid = item.dwOwningPid
if lPort == port:
return pid
else:
return None
##########################################################################
# The GetExtendedUdpTable function retrieves a table that contains a list of UDP endpoints available to the application.
#
# DWORD GetExtendedUdpTable(
# _Out_ PVOID pUdpTable,
# _Inout_ PDWORD pdwSize,
# _In_ BOOL bOrder,
# _In_ ULONG ulAf,
# _In_ UDP_TABLE_CLASS TableClass,
# _In_ ULONG Reserved
# );
示例3: _get_pid_port_udp
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def _get_pid_port_udp(self, port):
for item in self.get_extended_udp_table():
lPort = socket.ntohs(item.dwLocalPort)
lAddr = socket.inet_ntoa(struct.pack('L', item.dwLocalAddr))
pid = item.dwOwningPid
if lPort == port:
return pid
else:
return None
##########################################################################
# Retrieves the name of the executable file for the specified process.
#
# DWORD WINAPI GetProcessImageFileName(
# _In_ HANDLE hProcess,
# _Out_ LPTSTR lpImageFileName,
# _In_ DWORD nSize
# );
示例4: get_orig_dst
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [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!")
示例5: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def __init__(self, buffer: bytes = b""):
super().__init__()
self.data_offset = self.ihl * 4
self.raw_buffer = buffer
self.raw_header = buffer[:self.data_offset]
phbs = {v.value: k for k, v in DSCP.__members__.items()}
self.dscp = phbs[self.raw_dscp] if self.raw_dscp in phbs else "Unknown PHB"
self.ecn = "Non-ECT" if self.raw_ecn is 0 else ("ECT(0)" if self.raw_ecn is 2 else ("ECT(1)" if self.raw_ecn is 1 else "CE"))
self.differentiated_services = (self.dscp, self.ecn)
self.total_length = socket.ntohs(self.raw_total_length)
self.identification = socket.ntohs(self.raw_identification)
self.flags = {0: self.raw_flags&1, 1: self.raw_flags&2, 2: self.raw_flags&4}
self.protocol = self.protocols[self.raw_protocol] if self.raw_protocol in self.protocols else "Unknown"
self.checksum = socket.ntohs(self.raw_checksum)
self.source = socket.inet_ntoa(struct.pack("<L", self.raw_source))
self.destination = socket.inet_ntoa(struct.pack("<L", self.raw_destination))
self.raw_options = b""
self.options = None
if self.ihl > 5:
self.raw_options = buffer[0x14:self.data_offset]
self.options = self.options = self.Options(self.raw_options)#self.parse_options(self.raw_options)
self.data = buffer[self.data_offset:]
示例6: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def __init__(self, raw_intf = 'eth1', listen_port=10001):
self.logger = logging.getLogger()
# setup TCP side
self.s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
self.s.bind(('::0', listen_port))
self.s.listen(1)
self.tcp = None
# track current state of TCP side tunnel. 0 = reading size, 1 = reading packet
self.tcp_state = 0
self.tcp_buf = b''
self.tcp_remaining = 0
# setup raw side
self.raw = socket.socket( socket.AF_PACKET , socket.SOCK_RAW , socket.ntohs(0x0003))
self.raw.bind((raw_intf, 0))
# don't block
self.raw.setblocking(0)
示例7: listen
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def listen(local_addr, local_port=80):
"""
This function will initiate a web listener (NOT SERVER!) on default port 80.
It will then capture files and save them into a local file.
:param local_addr: The ip address to bind to.
:param local_port: The port. If not mentioned, 80 will be chosen.
:return:
"""
def eth_addr(a):
b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5]))
return b
try:
s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(ETH_P_ALL))
except socket.error, msg:
sys.stderr.write('Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] + "\n")
raise ()
示例8: decode_ip_packet
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def decode_ip_packet(s):
d={}
d['version']=(ord(s[0]) & 0xf0) >> 4
d['header_len']=ord(s[0]) & 0x0f
d['tos']=ord(s[1])
d['total_len']=socket.ntohs(struct.unpack('H',s[2:4])[0])
d['id']=socket.ntohs(struct.unpack('H',s[4:6])[0])
d['flags']=(ord(s[6]) & 0xe0) >> 5
d['fragment_offset']=socket.ntohs(struct.unpack('H',s[6:8])[0] & 0x1f)
d['ttl']=ord(s[8])
d['protocol']=ord(s[9])
d['checksum']=socket.ntohs(struct.unpack('H',s[10:12])[0])
d['source_address']=pcap.ntoa(struct.unpack('i',s[12:16])[0])
d['destination_address']=pcap.ntoa(struct.unpack('i',s[16:20])[0])
if d['header_len']>5:
d['options']=s[20:4*(d['header_len']-5)]
else:
d['options']=None
d['data']=s[4*d['header_len']:]
return d
示例9: bind
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def bind(self):
"""
Bind server, and call listen if using TCP, meaning that the client test code can successfully connect.
"""
if self.proto == "tcp":
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif self.proto == "udp":
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif self.proto == "raw":
self.server_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0003))
else:
raise Exception("Invalid protocol type: '{0}'".format(self.proto))
self.server_socket.bind((self.host, 0)) # let OS choose a free port
if self.proto == "tcp":
self.server_socket.listen(1)
self.active_port = self.server_socket.getsockname()[1]
示例10: bind
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def bind(self):
"""
Bind server, and call listen if using TCP, meaning that the client test code can successfully connect.
"""
if self.proto == "tcp":
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif self.proto == "udp":
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
elif self.proto == "raw":
self.server_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(ETH_P_ALL))
else:
raise Exception("Invalid protocol type: '{0}'".format(self.proto))
self.server_socket.bind((self.host, 0)) # let OS choose a free port
if self.proto == "tcp":
self.server_socket.listen(1)
self.active_port = self.server_socket.getsockname()[1]
示例11: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def __init__(self):
# info about non-sdn participants
# TODO: Create a mapping between actual interface IP addresses
# and the corresponding MAC addresses for all the non-SDN participants
# In case of MDS, it is actual mac adresses of these interfaces, in case
# of the superset scheme it is : 1XXXX-nexthop_id
# self.nonSDN_nhip_2_nhmac = {}
try:
self.sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(ETH_TYPE_ARP))
self.sock.bind((config.interface, 0))
except socket.error as msg:
logger.error("Can't open socket %s", str(config.interface))
logger.exception('Failed to create socket. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
raise
示例12: ntoh
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [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")
示例13: reverse
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [as 别名]
def reverse(self, val):
if self._size == 16:
val = socket.ntohs(val)
elif self._size == 32:
val = socket.ntohl(val)
return val
示例14: testNtoH
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [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)
示例15: testNtoHErrors
# 需要导入模块: import socket [as 别名]
# 或者: from socket import ntohs [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)