tag:blogger.com,1999:blog-33445579476208737352024-02-07T17:18:53.656-08:00The k33perAnonymoushttp://www.blogger.com/profile/02666314795581465696noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-3344557947620873735.post-26082385235812023282013-11-02T18:58:00.000-07:002013-11-02T19:08:01.672-07:00Python Networking: Socket OptionsI 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.<br />
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.<br />
<br />
However, dealing with <a href="http://en.wikipedia.org/wiki/Berkeley_sockets" target="_blank">sockets</a> at lower levels could give you a little bit of headache.A specific problem which I've come across time to time is <u>how to access socket options.</u><br />
<br />
Socket options are, ..., options, to control (or monitor) different aspects of underlying mechanisms for sending and receiving data.<br />
<br />
Options are organized into levels like IP level or TCP level. You can find a list of TCP level options <a href="http://man7.org/linux/man-pages/man7/tcp.7.html" target="_blank">here</a>.<br />
For example if you need to know about the status of your connection, something like what <b>netstat</b> command prints, you have to get the tcp_info option from TCP level. let's do that:<br />
<br />
<ol>
<li>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 <a href="http://lxr.free-electrons.com/source/include/uapi/linux/tcp.h#L148" target="_blank">here</a>. It's "__u8"*7 + "__u32"*24 = 104</li>
<li>you are going to use python's <a href="http://docs.python.org/2/library/struct.html" target="_blank">struct module</a> to parse the returned value.</li>
<li>use socket.getsockopt method to access the options with the correct level and option name.</li>
<li>in the parsed value, you need the right item (First in this case).</li>
</ol>
<br />
The overall operation would be something like this:<br />
<br />
<pre class="lang-py prettyprint prettyprinted"><span style="background-color: #cccccc;"><code><span class="pln"> </span><span class="kwd">def</span><span class="pln"> getTCPInfo</span><span class="pun">(</span><span class="pln">s</span><span class="pun">):</span><span class="pln">
fmt </span><span class="pun">=</span><span class="pln"> </span><span class="str">"B"</span><span class="pun">*</span><span class="lit">7</span><span class="pun">+</span><span class="str">"I"</span><span class="pun">*</span><span class="lit">21</span><span class="pln">
x </span><span class="pun">=</span><span class="pln"> struct</span><span class="pun">.</span><span class="pln">unpack</span><span class="pun">(</span><span class="pln">fmt</span><span class="pun">,</span><span class="pln"> s</span><span class="pun">.</span><span class="pln">getsockopt</span><span class="pun">(</span><span class="pln">socket</span><span class="pun">.</span><span class="pln">IPPROTO_TCP</span><span class="pun">,</span><span class="pln"> socket</span><span class="pun">.</span><span class="pln">TCP_INFO</span><span class="pun">,</span><span class="pln"> 104</span><span class="pun">))</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> x</span></code></span></pre>
<span style="background-color: #cccccc;"><br /></span>
I've posted a complete version of this <b><a href="http://stackoverflow.com/questions/18185165/query-socket-connection-state-in-python/18189190#18189190">here</a></b>, previously.<br />
Remember that each option of each level might require different approach but the general steps is the same.<br />
<br />
Enjoy.<br />
<br />
<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/02666314795581465696noreply@blogger.com0tag:blogger.com,1999:blog-3344557947620873735.post-60313990028067039202013-11-02T16:11:00.000-07:002013-11-03T04:16:52.309-08:00PyCon Iran 2013: As It HappendOk, It was highly anticipated and total fun.<br />
(I'll keep it as short as possible)<br />
<br />
<b>My Verdict:</b><br />
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 :) <br />
<br />
<br />
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.<br />
<br />
(Note: I don't have access to all photos yet)<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgMqPd9nXxzO3YM2SoAFUKbdPmkwKw_a_Cwmv8pBuF3WMMMvxOXdDN83lsuFWErU7if2pC1CkeLNKkjSekGL7NsKatpJK6u1wq9OohwNSKeGJLOMJnw28wUHOtfMfbhYPo_T0ry9_7hux5/s1600/IMG_0328.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="425" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgMqPd9nXxzO3YM2SoAFUKbdPmkwKw_a_Cwmv8pBuF3WMMMvxOXdDN83lsuFWErU7if2pC1CkeLNKkjSekGL7NsKatpJK6u1wq9OohwNSKeGJLOMJnw28wUHOtfMfbhYPo_T0ry9_7hux5/s640/IMG_0328.JPG" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><br /></td></tr>
</tbody></table>
<br />
Let me point out some highlights:<br />
<br />
<b>Turn Out:</b><br />
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.<br />
<br />
<b>BDFL's Message:</b><br />
I was not sure how he would respond to our request. <a class="g-profile" href="http://plus.google.com/115212051037621986145" target="_blank">+Guido</a> actually wrote us a message which got the audience quite excited. Thanks Gudio, that made our day.<br />
And, <a class="g-profile" href="http://plus.google.com/116865269069705863179" target="_blank">+Armin Ronacher</a> was cool enough to send in a recorded video message. You had to see the look on <a href="http://en.wikipedia.org/wiki/Flask_%28programming%29" target="_blank">Flask</a> lovers' face :D .<br />
<br />
<b>Presentations & Workshops:</b><br />
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. <span class="short_text" id="result_box" lang="en"><span class="hps">Spontaneous i</span></span>n-depth discussions erupted around python's concurrency which was fun.<br />
Order of presentations and workshops was not that much thoughtful, but we can give it a pass for now.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIVLzjYOk0Fxcj0sKoomzejOnemt1v1P4bxCyTv21-MswvsSne9DpStsPzZ9RWgxR9CZShmFyuiD-sH0u8vY-hB9ZAqF5RLPt40O0q0MBtJGebZPF5rpX8ejpeZDuK-nTmjW3xdeMiQ0aM/s1600/IMG_0199.JPG" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIVLzjYOk0Fxcj0sKoomzejOnemt1v1P4bxCyTv21-MswvsSne9DpStsPzZ9RWgxR9CZShmFyuiD-sH0u8vY-hB9ZAqF5RLPt40O0q0MBtJGebZPF5rpX8ejpeZDuK-nTmjW3xdeMiQ0aM/s640/IMG_0199.JPG" width="640" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><br /></td></tr>
</tbody></table>
<br />
All to all, I think we have managed to start something on the right direction. People got to know each other and connect.<br />
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.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6_aqtaBjJFYtbXNtIqEgKdsjQfgQadPmGM9r0Egf2uOAAOuMaLIKbQb4p6poEwPAHZLDlpkn6G5AiGGOiT2_2wuIfdw171JQq5U67FR3_EeAPrQpqN5up3z83-66pPr1eWYB_h4h5Wh7h/s1600/IMG_0247.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="425" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6_aqtaBjJFYtbXNtIqEgKdsjQfgQadPmGM9r0Egf2uOAAOuMaLIKbQb4p6poEwPAHZLDlpkn6G5AiGGOiT2_2wuIfdw171JQq5U67FR3_EeAPrQpqN5up3z83-66pPr1eWYB_h4h5Wh7h/s640/IMG_0247.JPG" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAtoYAoA3mXZH_6cw68-daN_X8EFSFZBpTF5lsiYokXvhyphenhyphenlz4gl-965b15vQpYpqBTzg4yeVuAlLdruep6a27G3PEPiRgTRJKOkoVd4qYewxEaDCBlc2LgJYI85sSlGPkkzSWJnS_y1H63/s1600/IMG_0348.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAtoYAoA3mXZH_6cw68-daN_X8EFSFZBpTF5lsiYokXvhyphenhyphenlz4gl-965b15vQpYpqBTzg4yeVuAlLdruep6a27G3PEPiRgTRJKOkoVd4qYewxEaDCBlc2LgJYI85sSlGPkkzSWJnS_y1H63/s640/IMG_0348.JPG" width="640" /></a></div>
<br />
<br />
<br />
<b>Final verdict:</b><br />
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.<br />
<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhabF10dycovyWcdiE3DlVMmkiE4rAWjGjNIVNuo2JmPca2bZwlBLxsoZL2Tbv1kve7GC_f2K4n6hOaEcfcP1iWLOlpqR40PmwncF55oIS9oF5ohF9i864zDK_s0a4nBZ0UrsfUSf0wI5S_/s1600/IMG_0330.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="425" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhabF10dycovyWcdiE3DlVMmkiE4rAWjGjNIVNuo2JmPca2bZwlBLxsoZL2Tbv1kve7GC_f2K4n6hOaEcfcP1iWLOlpqR40PmwncF55oIS9oF5ohF9i864zDK_s0a4nBZ0UrsfUSf0wI5S_/s640/IMG_0330.JPG" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNvhTWde73LPsa-TIaNsxhuVio9PWqKOWVNZ1e9mtMdmLo7n1CzZkXm96qDnfXTATDdpd7I3AGNTTeIAbznofcmT9kKxE6xEuflgUDGtmR768FkVwUqEBVYZ2VRH6MZKJ41VHusa52Xx49/s1600/IMG_0184.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="426" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNvhTWde73LPsa-TIaNsxhuVio9PWqKOWVNZ1e9mtMdmLo7n1CzZkXm96qDnfXTATDdpd7I3AGNTTeIAbznofcmT9kKxE6xEuflgUDGtmR768FkVwUqEBVYZ2VRH6MZKJ41VHusa52Xx49/s640/IMG_0184.JPG" width="640" /></a></div>
<br />
<br />
Let's meet again for the next PyCon Iran, until then get in touch and take care, It's a long road ahead ;)<br />
<br />
-Vahid<br />
<br />
<br />
<br />
<br />
<br />Anonymoushttp://www.blogger.com/profile/02666314795581465696noreply@blogger.com4tag:blogger.com,1999:blog-3344557947620873735.post-34715784725278117542013-10-31T22:59:00.003-07:002013-11-02T19:27:46.977-07:00PyCon Iran 2013: A Long Awaited StartJust Had a Thought: <br />
For the past 10 years, I, like most people of my generation have witnessed how technology climate in Iran has changed.<br />
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.<br />
<br />
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, <b><a href="http://en.wikipedia.org/wiki/Information_technology_in_India" target="_blank">India</a></b>.<br />
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.<br />
I'm sure, a healthy community can make a difference because naturally it involves activities that we tend to do less like <b>sharing</b> and <b>mentoring</b>.<br />
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.<br />
<br />
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.<br />
<br />
Think about it, we will talk about this again, very soon.<br />
<br />
-Vahid.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://pycon.ir/logo.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://pycon.ir/logo.png" /></a></div>
<br />
PyCon Iran 2013: <a href="http://pycon.ir/">http://pycon.ir</a><br />
<br />Anonymoushttp://www.blogger.com/profile/02666314795581465696noreply@blogger.com0