Whether you are trying to learn the concepts and design patterns of net programming or building a production service which requires any level of network programming, Python is a great choice.
However, dealing with sockets at lower levels could give you a little bit of headache.A specific problem which I've come across time to time is how to access socket options.
Socket options are, ..., options, to control (or monitor) different aspects of underlying mechanisms for sending and receiving data.
Options are organized into levels like IP level or TCP level. You can find a list of TCP level options here.
For example if you need to know about the status of your connection, something like what netstat command prints, you have to get the tcp_info option from TCP level. let's do that:
- 1) you need to know the type (and size) of the value you are going to receive.In this case it can be looked up here. It's "__u8"*7 + "__u32"*24 = 104
- you are going to use python's struct module to parse the returned value.
- use socket.getsockopt method to access the options with the correct level and option name.
- in the parsed value, you need the right item (First in this case).
The overall operation would be something like this:
def getTCPInfo(s):
fmt = "B"*7+"I"*21
x = struct.unpack(fmt, s.getsockopt(socket.IPPROTO_TCP, socket.TCP_INFO, 104))
print x
I've posted a complete version of this here, previously.
Remember that each option of each level might require different approach but the general steps is the same.
Enjoy.