一定要看这本书《UNIX网络编程卷1》 –I’m lineuman

python关于socket接口

字节序(注意,是字节序,而不是位序)

网络字节序的概念:网络字节序是大端字节序

主机字节序的概念:主机的字节序要看操作系统。你可以通过简单的使用ntohs(2)的结果是2还是512,来判断其字节序是否和网络字节序一致。

这两种字节序之间相互转换使用一下4个函数

from socket import *
htons() # host to net short
ntohs() # net to host short
htonl() # host to net long
ntohl() # net to host long

名称与地址转换

from socket import *
gethostbyname()
gethostbyaddr()
getservbyname()
getservbyport()

地址转换函数

from socket import *
inet_aton()  # ascii to numeric
>>> inet_aton('127.0.0.1')
'\x7f\x00\x00\x01'

inet_ntoa()
>>> inet_ntoa('\x34\x33\x31\x33')
'52.51.49.51'

inet_pton()  # presentation to numeric
inet_ntop