{"id":231,"date":"2007-05-30T20:17:47","date_gmt":"2007-05-30T20:17:47","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2007\/05\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/"},"modified":"2007-05-30T20:17:47","modified_gmt":"2007-05-30T20:17:47","slug":"using_perl_to_send_heartbeat_to_a_python_server-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/","title":{"rendered":"Using Perl To Send Heartbeat To A Python Server"},"content":{"rendered":"<p>\nI&#8217;ve been brushing up on my networking and <a href=\"http:\/\/www.python.org\/\">Python<\/a> skills lately.\n<\/p>\n<p>\nOne example in the <a href=\"http:\/\/www.amazon.co.uk\/gp\/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.co.uk%2FPython-Cookbook-Alex-Martelli%2Fdp%2F0596007973%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1180551808%26sr%3D8-1&#038;tag=robertprcouk-21&#038;linkCode=ur2&#038;camp=1634&#038;creative=6738\">Python Cookbook<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/> is recipe 13.11 Detecting Inactive Computers. Here there are two scripts, one that sends a heartbeat, and another that listens for heartbeats. The idea is a simple UDP packet containing the short string &#8220;PyHB&#8221; is sent every 5 seconds or so by a client. A server then listens for these datagrams, stores the addresses of the clients which it receives messages from, and if it fails to receive a message in a certain time period it flags this up on the console.\n<\/p>\n<p>\nI thought I&#8217;d just check how easy it would be to create a <a href=\"http:\/\/www.perl.com\/\">Perl<\/a> client to talk to the Python backend. As it&#8217;s all through sockets it should be fairly easy.\n<\/p>\n<p>\nTo start with we define a few constants that cover the address of the server we want to connect to, the port to send the datagram to, how often we want to send it and if we want debugging information displayed or not.\n<\/p>\n<div class=\"code\"><code>use constant SERVER_IP   =&gt; '127.0.0.1';<br \/>\nuse constant SERVER_PORT =&gt; 43278;<br \/>\nuse constant BEAT_PERIOD =&gt; 5;<br \/>\nuse constant DEBUG       =&gt; 1;<br \/>\n<\/code><\/div>\n<p>\nNow we need to create the socket, in this case we&#8217;ll create a new IO::Socket object.\n<\/p>\n<div class=\"code\"><code>my $hbsocket = IO::Socket::INET-&gt;new(Proto    =&gt; 'udp',<br \/>\nPeerPort =&gt; SERVER_PORT,<br \/>\nPeerAddr =&gt; SERVER_IP)<br \/>\nor die \"Creating socket: $!n\";<br \/>\n<\/code><\/div>\n<p>\nNext we need to send our message, the string &#8220;PyHB&#8221; over the socket.\n<\/p>\n<div class=\"code\"><code>$hbsocket-&gt;send('PyHB') or die \"send: $!\";<br \/>\n<\/code><\/div>\n<p>\nFinally we need to wrap this up in a loop and sleep <var>BEAT_PERIOD<\/var> seconds before repeating our message.\n<\/p>\n<p>\nHere&#8217;s the final script.\n<\/p>\n<div class=\"code\"><code>#!\/usr\/bin\/perl -w<br \/>\nuse strict;<br \/>\nuse IO::Socket;<br \/>\nuse constant SERVER_IP   =&gt; '127.0.0.1';<br \/>\nuse constant SERVER_PORT =&gt; 43278;<br \/>\nuse constant BEAT_PERIOD =&gt; 5;<br \/>\nuse constant DEBUG       =&gt; 1;<br \/>\nprint \"Sending heatbeat to IP \" . SERVER_IP .\" , port \" . SERVER_PORT . \"n\";<br \/>\nprint \"press Ctrl-C to stopn\";<br \/>\nwhile (1) {<br \/>\nmy $hbsocket = IO::Socket::INET-&gt;new(Proto    =&gt; 'udp',<br \/>\nPeerPort =&gt; SERVER_PORT,<br \/>\nPeerAddr =&gt; SERVER_IP)<br \/>\nor die \"Creating socket: $!n\";<br \/>\n$hbsocket-&gt;send('PyHB') or die \"send: $!\";<br \/>\nprint \"Time: \" . localtime(time) . \"n\"  if (DEBUG);<br \/>\nsleep(BEAT_PERIOD);<br \/>\n}<br \/>\n<\/code><\/div>\n<p>\nJust set the value of DEBUG to 0 if you don&#8217;t want to see the time each time we send a message to the server.\n<\/p>\n<p>\nTo test this you&#8217;ll have to be running the server from recipe 13.11 in the <a href=\"http:\/\/www.amazon.co.uk\/gp\/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.co.uk%2FPython-Cookbook-Alex-Martelli%2Fdp%2F0596007973%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1180551808%26sr%3D8-1&#038;tag=robertprcouk-21&#038;linkCode=ur2&#038;camp=1634&#038;creative=6738\">Python Cookbook<\/a><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" \/>. You can download a <a href=\"http:\/\/examples.oreilly.com\/pythoncook2\/\">zip file of the examples in the Python Cookbook<\/a> from the O&#8217;Reilly website to test this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been brushing up on my networking and Python skills lately. One example in the Python Cookbook is recipe 13.11 Detecting Inactive Computers. Here there are two scripts, one that sends a heartbeat, and another that listens for heartbeats. The idea is a simple UDP packet containing the short string &#8220;PyHB&#8221; is sent every 5 &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using Perl To Send Heartbeat To A Python Server&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[47,53],"class_list":["post-231","post","type-post","status-publish","format-standard","hentry","category-dev","tag-perl","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using Perl To Send Heartbeat To A Python Server - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Perl To Send Heartbeat To A Python Server - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been brushing up on my networking and Python skills lately. One example in the Python Cookbook is recipe 13.11 Detecting Inactive Computers. Here there are two scripts, one that sends a heartbeat, and another that listens for heartbeats. The idea is a simple UDP packet containing the short string &#8220;PyHB&#8221; is sent every 5 &hellip; Continue reading &quot;Using Perl To Send Heartbeat To A Python Server&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2007-05-30T20:17:47+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using Perl To Send Heartbeat To A Python Server\",\"datePublished\":\"2007-05-30T20:17:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/\"},\"wordCount\":301,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.assoc-amazon.co.uk\\\/e\\\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\",\"keywords\":[\"Perl\",\"Python\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/\",\"name\":\"Using Perl To Send Heartbeat To A Python Server - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.assoc-amazon.co.uk\\\/e\\\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\",\"datePublished\":\"2007-05-30T20:17:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#primaryimage\",\"url\":\"http:\\\/\\\/www.assoc-amazon.co.uk\\\/e\\\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\",\"contentUrl\":\"http:\\\/\\\/www.assoc-amazon.co.uk\\\/e\\\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_perl_to_send_heartbeat_to_a_python_server-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Perl To Send Heartbeat To A Python Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Perl To Send Heartbeat To A Python Server - Robert Price","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:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Using Perl To Send Heartbeat To A Python Server - Robert Price","og_description":"I&#8217;ve been brushing up on my networking and Python skills lately. One example in the Python Cookbook is recipe 13.11 Detecting Inactive Computers. Here there are two scripts, one that sends a heartbeat, and another that listens for heartbeats. The idea is a simple UDP packet containing the short string &#8220;PyHB&#8221; is sent every 5 &hellip; Continue reading \"Using Perl To Send Heartbeat To A Python Server\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/","og_site_name":"Robert Price","article_published_time":"2007-05-30T20:17:47+00:00","og_image":[{"url":"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2","type":"","width":"","height":""}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using Perl To Send Heartbeat To A Python Server","datePublished":"2007-05-30T20:17:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/"},"wordCount":301,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#primaryimage"},"thumbnailUrl":"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2","keywords":["Perl","Python"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/","name":"Using Perl To Send Heartbeat To A Python Server - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#primaryimage"},"thumbnailUrl":"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2","datePublished":"2007-05-30T20:17:47+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#primaryimage","url":"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2","contentUrl":"http:\/\/www.assoc-amazon.co.uk\/e\/ir?t=robertprcouk-21&amp;l=ur2&amp;o=2"},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_perl_to_send_heartbeat_to_a_python_server-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using Perl To Send Heartbeat To A Python Server"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}