socket.getsockopt(level, option)
- This function returns the value of a socket option
-
The
leveldefines the level of the option- It is
SOL_SOCKETfor socket-level options - It is
IPPROTO_IPfor protocol-related options
- It is
- The
optnameselects a specific option -
The following are the socket options:
SOL_SOCKET:The option is defined at the socket levelIPPROTO_IP:The option is defined at the IP protocol level
Some Socket Options for SOL_SOCKET
| Option Name | Value | Description |
|---|---|---|
| SO_ACCEPTCONN | 0,1 | Is the socket accepting connections? |
| SO_ERROR | int | Gets error status. |
| SO_KEEPALIVE | 0,1 | Is there a keepalive probe? |
| SO_TYPE | int | Gets socket type. |
- For more options, see the list in Python Essential Reference
Some Socket Options for IPPROTO_IP
| Option Name | Value | Description |
|---|---|---|
| IP_HDRINCL | int | Gets IP header included with data. |
| IP_RECVDSTADDR | 0,1 | Receive all IP options with datagram. |
| IP_TOS | int | Gets type of service. |
| IP_TTL | int | Gets time-to-live. |
- For more options, see the list in Python Essential Reference
Example of getsockopt
>>> import socket
>>> sock = socket(AF_INET, SOCK_STREAM)
>>> sock.getsockopt(sock.SOL_SOCKET, sock.SO_REUSEADDR)
1References
Previous
Next