{"id":1322,"date":"2011-06-23T11:49:58","date_gmt":"2011-06-23T11:49:58","guid":{"rendered":"https:\/\/supportex.net\/?p=1322"},"modified":"2019-05-10T21:04:13","modified_gmt":"2019-05-10T19:04:13","slug":"list-listen-sockets-linux-python","status":"publish","type":"post","link":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/","title":{"rendered":"Python: how to get list of listen sockets in Linux"},"content":{"rendered":"<p>Here is an example of Python code to get listen socket from Python:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">def listen_sockets():\n    listens = []\n    lines = open(\"\/proc\/net\/tcp\").readlines()\n    for l in lines:\n        ls = l.split()\n        if ls[3] == '0A':\n            lp =  ls[1].split(':')\n            ip = str(lp[0])\n            pair = \"%s.%s.%s.%s:%s\" %( int(ip[6:8], 16), int(ip[4:6], 16), int(ip[2:4], 16), int(ip[0:2], 16), int(lp[1], 16))\n            listens.append(str(pair))\n    return listenslistens = listen_sockets()\nprint listens\nlistens = listen_sockets()\nprint listens<\/pre>\n<p><strong>Didn&#8217;t find the answer to your question? <a href=\"\/contacts\/\">Ask it<\/a> our administrators to reply we will publish on website.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is an example of Python code to get listen socket from Python: def listen_sockets(): listens = [] lines = open(&#8220;\/proc\/net\/tcp&#8221;).readlines() for l in lines: ls = l.split() if ls[3] == &#8216;0A&#8217;: lp = ls[1].split(&#8216;:&#8217;) ip = str(lp[0]) pair = &#8220;%s.%s.%s.%s:%s&#8221; %( int(ip[6:8], 16), int(ip[4:6], 16), int(ip[2:4], 16), int(ip[0:2], 16), int(lp[1], 16)) listens.append(str(pair)) return listenslistens&hellip; <\/p>\n<div class=\"readmore-wrapper\"><a href=\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\" class=\"more-link\">Read <\/a><\/div>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[275,217],"class_list":["post-1322","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-python-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python: how to get list of listen sockets in Linux &#8211; Supportex.NET blog<\/title>\n<meta name=\"description\" content=\"Python script for get list listen sockets in Linux\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: how to get list of listen sockets in Linux &#8211; Supportex.NET blog\" \/>\n<meta property=\"og:description\" content=\"Python script for get list listen sockets in Linux\" \/>\n<meta property=\"og:url\" content=\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Supportex.NET blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-06-23T11:49:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-10T19:04:13+00:00\" \/>\n<meta name=\"author\" content=\"Oleksii Tykhonov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleksii Tykhonov\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\"},\"author\":{\"name\":\"Oleksii Tykhonov\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"headline\":\"Python: how to get list of listen sockets in Linux\",\"datePublished\":\"2011-06-23T11:49:58+00:00\",\"dateModified\":\"2019-05-10T19:04:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\"},\"wordCount\":42,\"commentCount\":0,\"keywords\":[\"linux\",\"python\"],\"articleSection\":[\"linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\",\"url\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\",\"name\":\"Python: how to get list of listen sockets in Linux &#8211; Supportex.NET blog\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\"},\"datePublished\":\"2011-06-23T11:49:58+00:00\",\"dateModified\":\"2019-05-10T19:04:13+00:00\",\"author\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"description\":\"Python script for get list listen sockets in Linux\",\"breadcrumb\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/supportex.net\/blog\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python: how to get list of listen sockets in Linux\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\",\"url\":\"https:\/\/supportex.net\/blog\/en\/\",\"name\":\"Supportex.NET blog\",\"description\":\"Server and network management company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\",\"name\":\"Oleksii Tykhonov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"caption\":\"Oleksii Tykhonov\"},\"url\":\"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: how to get list of listen sockets in Linux &#8211; Supportex.NET blog","description":"Python script for get list listen sockets in Linux","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/","og_locale":"en_US","og_type":"article","og_title":"Python: how to get list of listen sockets in Linux &#8211; Supportex.NET blog","og_description":"Python script for get list listen sockets in Linux","og_url":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/","og_site_name":"Supportex.NET blog","article_published_time":"2011-06-23T11:49:58+00:00","article_modified_time":"2019-05-10T19:04:13+00:00","author":"Oleksii Tykhonov","twitter_misc":{"Written by":"Oleksii Tykhonov"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#article","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/"},"author":{"name":"Oleksii Tykhonov","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"headline":"Python: how to get list of listen sockets in Linux","datePublished":"2011-06-23T11:49:58+00:00","dateModified":"2019-05-10T19:04:13+00:00","mainEntityOfPage":{"@id":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/"},"wordCount":42,"commentCount":0,"keywords":["linux","python"],"articleSection":["linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/","url":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/","name":"Python: how to get list of listen sockets in Linux &#8211; Supportex.NET blog","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/en\/#website"},"datePublished":"2011-06-23T11:49:58+00:00","dateModified":"2019-05-10T19:04:13+00:00","author":{"@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"description":"Python script for get list listen sockets in Linux","breadcrumb":{"@id":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/supportex.net\/blog\/2011\/06\/list-listen-sockets-linux-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/supportex.net\/blog\/en\/"},{"@type":"ListItem","position":2,"name":"Python: how to get list of listen sockets in Linux"}]},{"@type":"WebSite","@id":"https:\/\/supportex.net\/blog\/en\/#website","url":"https:\/\/supportex.net\/blog\/en\/","name":"Supportex.NET blog","description":"Server and network management company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251","name":"Oleksii Tykhonov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","caption":"Oleksii Tykhonov"},"url":"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/"}]}},"_links":{"self":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/1322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/comments?post=1322"}],"version-history":[{"count":3,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/1322\/revisions"}],"predecessor-version":[{"id":3238,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/1322\/revisions\/3238"}],"wp:attachment":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/media?parent=1322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/categories?post=1322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/tags?post=1322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}