我正在从 python 码头学习套接字编程,并且我接触到了这些东西:
socket.ntohl(x) 将 32 位正整数从网络转换为主机
字节顺序。在主机字节顺序与以下相同的机器上
网络字节顺序,这是一个无操作;否则,它执行 4 字节
交换操作。
socket.ntohs(x) 将 16 位正整数从网络转换为主机
字节顺序。在主机字节顺序与以下相同的机器上
网络字节顺序,这是一个无操作;否则,它执行 2 字节
交换操作。
版本 3.10 中的更改:如果 x 不适合 a,则引发 OverflowError
16 位无符号整数。
socket.htonl(x) 将 32 位正整数从主机转换为网络
字节顺序。在主机字节顺序与以下相同的机器上
网络字节顺序,这是一个无操作;否则,它执行 4 字节
交换操作。
socket.htons(x) 将 16 位正整数从主机转换为网络
字节顺序。在主机字节顺序与以下相同的机器上
网络字节顺序,这是一个无操作;否则,它执行 2 字节
交换操作。
因此,我对这些函数所能做的就是将一些整数放在括号内,然后我得到结果:
import socket
a=socket.ntohs(32) #example number
print(a)
result : 8192
你能给出一些对这些函数有意义的例子吗?
I was learing socket programming from python docks and i reach to these things :
socket.ntohl(x) Convert 32-bit positive integers from network to host
byte order. On machines where the host byte order is the same as
network byte order, this is a no-op; otherwise, it performs a 4-byte
swap operation.
socket.ntohs(x) Convert 16-bit positive integers from network to host
byte order. On machines where the host byte order is the same as
network byte order, this is a no-op; otherwise, it performs a 2-byte
swap operation.
Changed in version 3.10: Raises OverflowError if x does not fit in a
16-bit unsigned integer.
socket.htonl(x) Convert 32-bit positive integers from host to network
byte order. On machines where the host byte order is the same as
network byte order, this is a no-op; otherwise, it performs a 4-byte
swap operation.
socket.htons(x) Convert 16-bit positive integers from host to network
byte order. On machines where the host byte order is the same as
network byte order, this is a no-op; otherwise, it performs a 2-byte
swap operation.
So all i can do with these functions is just putting some integer inside parentheses and then i have the result :
import socket
a=socket.ntohs(32) #example number
print(a)
result : 8192
Can you give some examples that makes sense with those functions?
发布评论
评论(2)
这是一个关于哪些机器使用大端和小端的问题。
我不是 IBM 大型机的用户,但似乎: https://www.ibm.com/docs/en/zos/2.4.0?topic=hosts-network-byte-order 他们使用大端字节序。然而,IBM 电脑使用小尾数法。
TCP/IP 标准网络字节顺序是大端字节序,因此它取决于运行应用程序代码的计算机以及是小端字节序还是大端字节序。如果您的网络和硬件字节顺序之间存在不匹配,您需要使用您指定的函数。
这是一个例子。
This is a question about which machines are using big endian vs little endian.
I'm not a user of IBM mainframes but it seems: https://www.ibm.com/docs/en/zos/2.4.0?topic=hosts-network-byte-order that they use big endian. IBM pcs however, use little endian.
The TCP/IP standard network byte order is big-endian so it depends on the machine where the application code will run and whether it is little or big endian. If there is a mismatch between your network and your hardware byte orders, you need to use a function like the one you indicated.
This is one example.
Python 的
int
与其运行的处理器具有相同的字节序。在计算机网络中,在传输和接收数据包时,使用大端排序。
因此,您必须在将数据发送到网络之前以及从网络接收数据之后进行转换,具体取决于 python 解释器运行的体系结构。
请参阅https://en.wikipedia.org/wiki/Endianness
Python's
int
has the same endianness as the processor it runs on.In computer networks, while transmitting and receiving data packets, Big Endian Ordering is used.
Therefore, you have to convert before sending data to the network and after receiving data from the network depending on the architecture the python interpreter is running on.
See https://en.wikipedia.org/wiki/Endianness