Python 中的 socket options 对应于 setsockopt()/getsockopt() 两个函数。函数声明如下： setsockopt(level, optname, value) getsockopt(level, optname[, buflen])
buflen must be specified and gives the maximum length of a string you're willing to accept.
对于 level，用于定义 which set of options is to be used。一般只有 SOL_SOCKET，其它的 level 都是操作系统相关，不具备可移植性。下面是 SOL_SOCKET 下可设置的一些 optname。
||Causes the socket to be valid on a particular network interface (network card) only. May not be portable.
||A string giving the device name, or an empty string to return to default behavior
||Permits the transmission and reception of packets to the broadcast address. Only valid for UDP.
||Prohibits outgoing packets from crossing any router or gateway. This is most frequently useful for UDP communication on an Ethernet LAN as a security measure. It prevents data from leaving the local network, no matter what IP address is used for the destination.
||Enables the transmission of keep-alive packets for TCP connections. These packets help both endpoints of the connection verify that the connection remains open even when no data is passing through it.
||Causes incoming out-of-band data to be treated as if it were normal data; that is, it will be receivable via a standard call to recv().
||The port number used by the local endpoint of this socket becomes immediately reusable after the socket is closed. Normal behavior prevents it from being reused for a system-defined amount of time.
||Retrieves the socket type (such as SOCKSTREAM or SOCK_DGRAM). getsockopt() only.
下面的代码可以看到我们当前的 Python 支持哪些 optname。
solist = [x for x in dir(socket) if x.startswith('SO_')]
for x in solist: