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.


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 ;)


Thursday, October 31, 2013

PyCon Iran 2013: A Long Awaited Start

Just Had a Thought:
For the past 10 years, I, like most people of my generation have witnessed how technology climate in Iran has changed.
From ineffective government policies to international (US) sanctions to Iranian personality, we can make up enough reasons to why we are where we are. Where are we in the global IT industry anyway? - It just doesn't feel good.

As a community (of IT experts), we have failed on so many occasions to grow out of our problems. Don't get me wrong, we are excellent consumers, "smart buyer" if you prefer. But that does not help us to fill the gap with, let's say, India.
That being said, and with PyCon Iran 2013 just around the corner, I think an opportunity is coming to take our community, the Python community in Iran, to the right direction.
I'm sure, a healthy community can make a difference because naturally it involves activities that we tend to do less like sharing and mentoring.
Python is known to have a healthy international community which I hope to be extended into our community. Our First PyCon, certainly has many shortcomings and I'm not going to talk about it now, not before it actually started, but, it is a start to gather everybody who is involved with Python or likes to be, and bond to them and rely on that bond to gain knowledge, share it, guide others in using it and ... having fun. We are going to get somewhere as a community or be stuck as individuals.

Let us hope, that from this point on, we would follow on the path of a thriving community to get where we want to get, to cease the future opportunities and to build a better industry.

Think about it, we will talk about this again, very soon.


PyCon Iran 2013: