setsockopt() and getsockopt()

Using socket options gives you some advanced options for sockets to do interesting things.

Options have a LEVEL, OPTION NAME, and a VALUE.

  • LEVEL can be the entire socket (socket.SOL_SOCKET) or a specific protocol (socket.IPPROTO_IPV6).
  • The LEVEL specified determines what OPTION NAMEs are available to be set.
  • VALUES are assigned to the specified OPTION NAME.

Unfortunately, options are scattered all over the place in documentation. Man pages are currently the best source or for a grouping of common ones, see the refs.

socket.setsockopt(level, optname, value)

Set the value of the given socket option (see the Unix manual pagesetsockopt(2)). The needed symbolic constants are defined in thesocketmodule (SO_*etc.). The value can be an integer or a string representing a buffer. In the latter case it is up to the caller to ensure that the string contains the proper bits (see the optional built-in modulestructfor a way to encode C structures as strings).

socket.getsockopt(level, optname[, buflen])

Return the value of the given socket option (see the Unix man pagegetsockopt(2)). The needed symbolic constants (SO_*etc.) are defined in this module. If_buflen_is absent, an integer option is assumed and its integer value is returned by the function. If_buflen_is present, it specifies the maximum length of the buffer used to receive the option in, and this buffer is returned as a string. It is up to the caller to decode the contents of the buffer (see the optional built-in modulestructfor a way to decode C structures encoded as strings).

The default socket buffer size may not be suitable in many circumstances. In such circumstances, you can change the default socket buffer size to a more suitable value.

How to do it...

Let us manipulate the default socket buffer size using a socket object'ssetsockopt()method.

First, define two constants:SEND_BUF_SIZE/RECV_BUF_SIZEand then wrap a socket instance's call to thesetsockopt()method in a function. It is also a good idea to check the value of the buffer size before modifying it. Note that we need to set up the send and receive buffer size separately.

Listing 1.8 shows how to modify socket send/receive buffer sizes as follows:

import socket

SEND_BUF_SIZE = 4096
RECV_BUF_SIZE = 4096

def modify_buff_size():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM )

    # Get the size of the socket's send buffer
    bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    print "Buffer size [Before]:%d" %bufsize

    sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1)
    sock.setsockopt(
            socket.SOL_SOCKET,
            socket.SO_SNDBUF,
            SEND_BUF_SIZE)
    sock.setsockopt(
            socket.SOL_SOCKET,
            socket.SO_RCVBUF,
            RECV_BUF_SIZE)
    bufsize = sock.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
    print "Buffer size [After]:%d" %bufsize

if __name__ == '__main__':
    modify_buff_size()

If you run the preceding script, it will show the changes in the socket's buffer size. The following output may be different on your machine depending on your operating system's local settings:

$ python 1_8_modify_buff_size.py 
Buffer size [Before]:16384
Buffer size [After]:8192

How it works...

You can call thegetsockopt()andsetsockopt()methods on a socket object to retrieve and modify the socket object's properties respectively. Thesetsockopt()method takes three arguments:level,optname, andvalue. Here,optnametakes the option name andvalueis thecorrespondingvalue of that option. For the first argument, the needed symbolic constants can be found in the socket module (SO_*etc.).

See Also:

http://man7.org/linux/man-pages/man2/setsockopt.2.html

https://linux.die.net/man/2/getsockopt

results matching ""

    No results matching ""