{"id":7,"date":"2005-11-19T09:57:03","date_gmt":"2005-11-19T13:57:03","guid":{"rendered":"http:\/\/thesmithfam.org\/blog\/?p=7"},"modified":"2019-08-12T07:16:43","modified_gmt":"2019-08-12T13:16:43","slug":"python-sockets-clean-concise","status":"publish","type":"post","link":"https:\/\/thesmithfam.org\/blog\/2005\/11\/19\/python-sockets-clean-concise\/","title":{"rendered":"Python Sockets: Clean, Concise"},"content":{"rendered":"<p>I was recently pleased to find an excuse to code some <a href=\"http:\/\/python.org\/\">Python <\/a>for the first time. I am impressed. Python doesn&#8217;t feel like other programming languages. When I write Python I feel like I&#8217;m just telling the computer what to do, rather than constantly worrying about essoteric language issues. What I mean is this. While coding  Java, I am constantly thinking about class design, exception handling, patterns, etc. While coding C++, I worry a lot about memory management and I spend a lot of time trying to remember funky STL APIs. However, while coding Python the other day, I almost forgot I was programming. It was like I was just telling the computer what to do, line after line. My day&#8217;s work culminated with a pleasant experience coding to the Python socket API. Read on for the details.<\/p>\n<p><!--more--><\/p>\n<p>Coding Python client sockets is simple and intuitive. You just create a socket, set it up, connect it, and let Python&#8217;s superb exception handling take care of any problems. Like this:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ntry:\r\n    host = &quot;example.com&quot;\r\n    port = 80\r\n    # Create a socket object:\r\n    s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )\r\n    # If any operation takes more than 2 seconds, drop to the &quot;except&quot; below\r\n    s.settimeout( 2 )\r\n    # Connect to the host\r\n    s.connect( ( host, port ) )\r\n    # Send &quot;Some string\\n&quot; to the host\r\n    s.send( &quot;Some string\\n&quot; )\r\n    # See if the host has anything to say in reply\r\n    response_string, server = s.recvfrom(4096)\r\n   # Print the response\r\n    print response_string\r\n\r\nexcept socket.error, msg:\r\n   # If anything bad happened above, this runs:\r\n   print &quot;An error occurred:&quot;, msg\r\n\r\nelse:\r\n   # If all went well (no exceptions), we get here:\r\n   print &quot;Succcess!&quot;\r\n<\/pre>\n<p>All told, I got a good ROI on learning Python. I had a working application, complete with error handling, in under a day with minimal code. There is a plethora of Python info on the web, and though I find its <a href=\"http:\/\/www.python.org\/doc\/\">documentation site <\/a>to be lacking compared to <a href=\"http:\/\/php.net\/manual\">PHP&#8217;s docs,<\/a> it&#8217;s pretty good.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently pleased to find an excuse to code some Python for the first time. I am impressed. Python doesn&#8217;t feel like other programming languages. When I write Python I feel like I&#8217;m just telling the computer what to do, rather than constantly worrying about essoteric language issues. What I mean is this. While [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-code-and-cruft"],"_links":{"self":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":1,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":1626,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions\/1626"}],"wp:attachment":[{"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thesmithfam.org\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}