{"id":50,"date":"2008-10-16T14:35:00","date_gmt":"2008-10-16T14:35:00","guid":{"rendered":"https:\/\/freezion.com\/2008\/10\/16\/handy-python-snippets\/"},"modified":"2008-10-16T14:35:00","modified_gmt":"2008-10-16T14:35:00","slug":"handy-python-snippets","status":"publish","type":"post","link":"https:\/\/freezion.com\/?p=50","title":{"rendered":"Handy Python Snippets"},"content":{"rendered":"<p>Obtaining the local IP address (getip.py)<\/p>\n<pre>#!\/usr\/bin\/env python\n\ndef getip():\n from socket import gethostbyaddr, gethostname\n theip = gethostbyaddr(gethostname())[2][0]\n return theip<\/pre>\n<p>Obtaining the local MAC address (getmac.py)<\/p>\n<pre>#!\/usr\/bin\/env python\n\ndef getmac():\n import sys, os\n if sys.platform == 'win32':\n  for line in os.popen(\"ipconfig \/all\"):\n   if line.lstrip().startswith('Physical Address'):\n    mac = line.split(':')[1].strip().replace('-',':')\n    break\n else:\n  for line in os.popen(\"\/sbin\/ifconfig\"):\n   if line.find('Ether') &gt; -1:\n    mac = line.split()[4]\n    break\n return mac<\/pre>\n<p>Putting these together (test.py)<\/p>\n<pre>#!\/usr\/bin\/env python\nimport getmac, getip\n\nmyip = getip.getip()\nmymac = getmac.getmac()\n\nprint mymac + \" has address: \" + myip<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Obtaining the local IP address (getip.py) #!\/usr\/bin\/env python def getip(): from socket import gethostbyaddr, gethostname theip = gethostbyaddr(gethostname())[2][0] return&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[22,42,45,52,56],"class_list":["post-50","post","type-post","status-publish","format-standard","hentry","category-tech","tag-howto","tag-net","tag-python","tag-skiddy","tag-tools"],"_links":{"self":[{"href":"https:\/\/freezion.com\/index.php?rest_route=\/wp\/v2\/posts\/50","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freezion.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freezion.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freezion.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freezion.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=50"}],"version-history":[{"count":0,"href":"https:\/\/freezion.com\/index.php?rest_route=\/wp\/v2\/posts\/50\/revisions"}],"wp:attachment":[{"href":"https:\/\/freezion.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=50"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freezion.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=50"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freezion.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=50"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}