本文整理匯總了Python中socket.htons方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.htons方法的具體用法?Python socket.htons怎麽用?Python socket.htons使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socket
的用法示例。
在下文中一共展示了socket.htons方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def __init__(self, srcp, dstp, seqn=1):
self.srcp = srcp
self.dstp = dstp
self.seqn = seqn
self.ackn = 0
self.offset = 5 # Data offset: 5x4 = 20 bytes
self.reserved = 0
self.urg = 0
self.ack = 0
self.psh = 0
self.rst = 0
self.syn = 1
self.fin = 0
self.window = socket.htons(5840)
self.checksum = 0
self.urgp = 0
self.payload = ""
示例2: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def __init__(self, srcp, dstp):
self.srcp = srcp
self.dstp = dstp
self.seqn = 10
self.ackn = 0
self.offset = 5 # Data offset: 5x4 = 20 bytes
self.reserved = 0
self.urg = 0
self.ack = 0
self.psh = 0
self.rst = 0
self.syn = 1
self.fin = 0
self.window = htons(5840)
self.checksum = 0
self.urgp = 0
self.payload = ""
示例3: pack
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def pack(self):
ip_header = struct.pack("!BBHHHBBH4s4s",
self.ver_ihl,
self.tos,
self.length,
self.id,
self.flags_offset,
self.ttl,
self.protocol,
0, # checksum should be 0
self.src,
self.dst)
self.checksum = checksum(ip_header)
ip_header = struct.pack("!BBHHHBBH4s4s",
self.ver_ihl,
self.tos,
self.length,
self.id,
self.flags_offset,
self.ttl,
self.protocol,
socket.htons(self.checksum),
self.src,
self.dst)
return ip_header
示例4: send_one_ping
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def send_one_ping(my_socket, dest_addr, ID):
"""
Send one ping to the given >dest_addr<.
"""
dest_addr = socket.gethostbyname(dest_addr)
# Header is type (8), code (8), checksum (16), id (16), sequence (16)
my_checksum = 0
# Make a dummy heder with a 0 checksum.
header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
bytesInDouble = struct.calcsize("d")
data = (192 - bytesInDouble) * "Q"
data = struct.pack("d", default_timer()) + data
# Calculate the checksum on the data and the dummy header.
my_checksum = checksum(header + data)
# Now that we have the right checksum, we put that in. It's just easier
# to make up a new header than to stuff it into the dummy.
header = struct.pack(
"bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1
)
packet = header + data
my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
示例5: test_mocked_eth_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def test_mocked_eth_socket(self, socket_mock):
socket_family = getattr(gevent.socket,
'AF_PACKET',
gevent.socket.AF_INET)
proto = bsc.ETH_PROTOCOL
handler = {'name':'name', 'log_dir':'/tmp'}
bsc.RAW_SOCKET_FD = 'foobar'
sl = bsc.SocketStreamCapturer([handler], ['eho0', 0], 'ethernet')
# We need to test a different load if the rawsocket package is used
if not bsc.RAW_SOCKET_FD:
socket_mock.socket.assert_called_with(socket_family,
gevent.socket.SOCK_RAW,
socket.htons(proto))
else:
socket_mock.fromfd.assert_called_with(bsc.RAW_SOCKET_FD,
socket_family,
gevent.socket.SOCK_RAW,
socket.htons(proto))
assert sl.conn_type == 'ethernet'
bsc.RAW_SOCKET_FD = None
示例6: test_mocked_eth_socket_with_rawsocket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def test_mocked_eth_socket_with_rawsocket(self, socket_mock):
socket_family = getattr(gevent.socket,
'AF_PACKET',
gevent.socket.AF_INET)
rawsocket_is_installed = True if bsc.RAW_SOCKET_FD else False
if not rawsocket_is_installed:
rawsocket_fd = 'fake_rawsocket_fd'
bsc.RAW_SOCKET_FD = rawsocket_fd
else:
rawsocket_fd = bsc.RAW_SOCKET_FD
handler = {'name':'name', 'log_dir':'/tmp'}
sl = bsc.SocketStreamCapturer([handler], ['eho0', 0], 'ethernet')
# We need to test a different load if the rawsocket package is used
socket_mock.fromfd.assert_called_with(rawsocket_fd,
socket_family,
gevent.socket.SOCK_RAW,
socket.htons(bsc.ETH_PROTOCOL))
assert sl.conn_type == 'ethernet'
if not rawsocket_is_installed:
bsc.RAW_SOCKET_FD = None
示例7: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def __init__(self, type=ETH_P_IP, filter=None, iface=None, promisc=None, nofilter=0): # noqa: E501
self.outs = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) # noqa: E501
self.outs.setsockopt(socket.SOL_IP, socket.IP_HDRINCL, 1)
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type)) # noqa: E501
self.iface = iface
if iface is not None:
self.ins.bind((self.iface, type))
if not six.PY2:
try:
# Receive Auxiliary Data (VLAN tags)
self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1)
self.ins.setsockopt(
socket.SOL_SOCKET,
SO_TIMESTAMPNS,
1
)
self.auxdata_available = True
except OSError:
# Note: Auxiliary Data is only supported since
# Linux 2.6.21
msg = "Your Linux Kernel does not support Auxiliary Data!"
log_runtime.info(msg)
示例8: answers
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def answers(self, other):
if not isinstance(other, IP):
return 0
# Check if IP addresses match
test_IPsrc = not conf.checkIPsrc or self.src == other.src
test_IPdst = self.dst == other.dst
# Check if IP ids match
test_IPid = not conf.checkIPID or self.id == other.id
test_IPid |= conf.checkIPID and self.id == socket.htons(other.id)
# Check if IP protocols match
test_IPproto = self.proto == other.proto
if not (test_IPsrc and test_IPdst and test_IPid and test_IPproto):
return 0
return self.payload.answers(other.payload)
示例9: send_one_ping
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def send_one_ping(my_socket, dest_addr, ID):
"""
Send one ping to the given >dest_addr<.
"""
dest_addr = socket.gethostbyname(dest_addr)
# Header is type (8), code (8), checksum (16), id (16), sequence (16)
my_checksum = 0
# Make a dummy heder with a 0 checksum.
header = struct.pack(b"bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
bytesInDouble = struct.calcsize("d")
data = (192 - bytesInDouble) * b"Q"
data = struct.pack("d", default_timer()) + data
# Calculate the checksum on the data and the dummy header.
my_checksum = checksum(header + data)
# Now that we have the right checksum, we put that in. It's just easier
# to make up a new header than to stuff it into the dummy.
header = struct.pack(b"bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1)
packet = header + data
my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
示例10: __init__
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def __init__(self, *args, **kwargs):
super(VRRPInterfaceMonitorNetworkDevice, self).__init__(*args,
**kwargs)
self.__is_active = True
config = self.config
if config.is_ipv6:
family = socket.AF_INET6
ether_type = ether.ETH_TYPE_IPV6
mac_address = vrrp.vrrp_ipv6_src_mac_address(config.vrid)
else:
family = socket.AF_INET
ether_type = ether.ETH_TYPE_IP
mac_address = vrrp.vrrp_ipv4_src_mac_address(config.vrid)
# socket module doesn't define IPPROTO_VRRP
self.ip_socket = socket.socket(family, socket.SOCK_RAW,
inet.IPPROTO_VRRP)
self.packet_socket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW,
socket.htons(ether_type))
self.packet_socket.bind((self.interface.device_name, ether_type,
socket.PACKET_MULTICAST,
arp.ARP_HW_TYPE_ETHERNET,
addrconv.mac.text_to_bin(mac_address)))
self.ifindex = if_nametoindex(self.interface.device_name)
示例11: send_one_ping
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def send_one_ping(self, my_socket, dest_addr, ID):
"""
Send one ping to the given >dest_addr<.
"""
dest_addr = socket.gethostbyname(dest_addr)
# Header is type (8), code (8), checksum (16), id (16), sequence (16)
my_checksum = 0
# Make a dummy heder with a 0 checksum.
header = struct.pack("bbHHh", self.ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
bytesInDouble = struct.calcsize("d")
data = (192 - bytesInDouble) * "Q"
data = struct.pack("d", time.time()) + data
# Calculate the checksum on the data and the dummy header.
my_checksum = self.checksum(header + data)
# Now that we have the right checksum, we put that in. It's just easier
# to make up a new header than to stuff it into the dummy.
header = struct.pack(
"bbHHh", self.ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1
)
packet = header + data
my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
示例12: recv
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def recv(event, src, dst, srcp, dstp, buff, timeout):
"""
event: should be set when a packet is sent
src: source ip
dst: destination ip
srcp: source port
dstp: destination port
buff: buffer for received data
"""
sock = socket.socket(socket.AF_PACKET,
socket.SOCK_DGRAM,
socket.htons(ETH_P_IP))
sock.settimeout(timeout)
event.wait()
while True:
try:
data, sa_ll = sock.recvfrom(65535)
if sa_ll[1] == ETH_P_IP and sa_ll[2] == socket.PACKET_HOST:
break # break if we captured incoming ip packet,
except socket.timeout as e:
print 'socket.timeout:', e
sock.close()
return
except socket.error as e:
print 'socket.error:', e
sock.close()
return
sip = data[12:16] # src ip
dip = data[16:20] # dst ip
sp = data[20:22] # src port
dp = data[22:24] # dst port
if (sip == dst and dip == src and sp == dstp and dp == srcp):
buff.append(data) # return received packet
event.clear()
sock.close()
示例13: pack
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [as 別名]
def pack(self):
ver_ihl = (self.version << 4) + self.ihl
flags_offset = (self.flags << 13) + self.offset
ip_header = struct.pack("!BBHHHBBH4s4s",
ver_ihl,
self.tos,
self.tl,
self.id,
flags_offset,
self.ttl,
self.protocol,
self.checksum,
self.source,
self.destination)
self.checksum = checksum(ip_header)
ip_header = struct.pack("!BBHHHBBH4s4s",
ver_ihl,
self.tos,
self.tl,
self.id,
flags_offset,
self.ttl,
self.protocol,
socket.htons(self.checksum),
self.source,
self.destination)
return ip_header
示例14: testNtoH
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import htons [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 htons [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)