Saturday, November 2, 2013

Python Networking: Socket Options

I love writing code in python. Well, who doesn't. Network programming has always been an interesting topic to code about and Python has great support for it.
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. 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
  2. you are going to use python's struct module to parse the returned value.
  3. use socket.getsockopt method to access the options with the correct level and option name.
  4. 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.






PyCon Iran 2013: As It Happend

Ok, It was highly anticipated and total fun.
(I'll keep it as short as possible)

My Verdict:
IMHO, It was a delicious soup with all ingredients for thirsty folks, so they didn't mind if it came out a little (just a little) watery :)


After few years of disoriented efforts, first PyCon in Iran kicked off. It's a good news to python community in Iran. For years this community existed only in name, it was motionless. But now, we have an official community, a brand, some might say. We need to keep it moving.

(Note: I don't have access to all photos yet)


Let me point out some highlights:

Turn Out:
It was very promising, around 300 people from all breeds (I don't have the exact number yet) had managed to show up, way more than anybody's expectations.

BDFL's Message:
I was not sure how he would respond to our request. +Guido actually wrote us a message which got the audience quite excited. Thanks Gudio, that made our day.
And, +Armin Ronacher was cool enough to send in a recorded video message. You had to see the look on Flask lovers' face :D .

Presentations & Workshops:
For the first PyCon, I don't think anybody had expected much of the high quality content. Anyway, topics were divers but not of much depth which I believe was good enough to appeal to the taste of most attendees. Spontaneous in-depth discussions erupted around python's concurrency which was fun.
Order of presentations and workshops was not that much thoughtful, but we can give it a pass for now.



All to all, I think we have managed to start something on the right direction. People got to know each other and connect.
It's all shaping up as of yesterday which is a good sign. Let's think about what can be done to keep it up.





Final verdict:
IMHO, It was a delicious soup with all ingredients for thirsty folks, so they didn't mind if it came out a little (just a little) watery.






Let's meet again for the next PyCon Iran, until then get in touch and take care, It's a long road ahead ;)

-Vahid