{"id":629,"date":"2009-04-27T00:40:40","date_gmt":"2009-04-27T04:40:40","guid":{"rendered":"http:\/\/cd34.com\/blog\/?p=629"},"modified":"2009-04-27T00:40:40","modified_gmt":"2009-04-27T04:40:40","slug":"python-perl-and-php-interoperability-with-pack-and-unpack","status":"publish","type":"post","link":"https:\/\/cd34.com\/blog\/programming\/python\/python-perl-and-php-interoperability-with-pack-and-unpack\/","title":{"rendered":"Python, Perl and PHP interoperability with pack and unpack"},"content":{"rendered":"<p>Perl has very powerful capabilities for dealing with structures.\u00a0 PHP&#8217;s support of those structures was based on Perl&#8217;s wisdom.\u00a0 Python went a different direction.<\/p>\n<p>Perl pack\/unpack definitions<\/p>\n<blockquote><p>PING_FORMAT =&gt; &#8216;(a4n2N2N\/a*)@245&#8217;;<br \/>\nTASK_FORMAT =&gt; &#8216;a4NIN\/a*a*&#8217;;<br \/>\nRETR_FORMAT =&gt; &#8216;a4N\/a*N&#8217;;<br \/>\nENCPAYLOAD_FORMAT =&gt; &#8216;Na*&#8217;;<\/p><\/blockquote>\n<p>PHP pack\/unpack definitions<\/p>\n<blockquote><p>define(&#8216;TASK_FORMAT&#8217;, &#8216;a4NINa*a*&#8217;);<br \/>\ndefine(&#8220;ENCPAYLOAD_FORMAT&#8221;,&#8217;Na*&#8217;);<\/p><\/blockquote>\n<p>For a communications package written in perl that communicates with 32 bit and 64 bit machines that may not share the same endian structure.\u00a0 The problem I&#8217;ve run into now is that Python does not support the Perl method, and, I don&#8217;t know why they didn&#8217;t at least offer some compatibility.\u00a0 pack and unpack give enormous power to communication systems between machines and their support of the perl methods allowed for reasonable interoperability between the two platforms.<\/p>\n<p>Python on the other hand opted to not support some of the features, which was one issue, but, their requirement is that you cannot send variable length packets.<\/p>\n<p>In Python, we&#8217;re able to replicate N, network endian Long by using !L:<\/p>\n<p>&gt;&gt;&gt; import struct<br \/>\n&gt;&gt;&gt; print struct.unpack(&#8216;!L&#8217;,&#8217;\\0\\0\\1\\0&#8242;);<br \/>\n(256,)<\/p>\n<p>However, there is no method to support a variable length payload behind that value.\u00a0 We&#8217;re able to set a fixed length like 5s, but, this means that we&#8217;ve got to know the length of the payload being sent.<\/p>\n<p>&gt;&gt;&gt; print struct.unpack(&#8216;!L5s&#8217;,&#8217;\\0\\0\\1\\0abcde&#8217;);<br \/>\n(256, &#8216;abcde&#8217;)<\/p>\n<p>If we overstate the size of the field, Python is more than happy to tell us that the payload length doesn&#8217;t match the length of the data.<\/p>\n<p>&gt;&gt;&gt; print struct.unpack(&#8216;!L8s&#8217;,&#8217;\\0\\0\\1\\0abcde&#8217;);<br \/>\nTraceback (most recent call last):<br \/>\nFile &#8220;&lt;stdin&gt;&#8221;, line 1, in &lt;module&gt;<br \/>\nFile &#8220;\/usr\/lib\/python2.5\/struct.py&#8221;, line 87, in unpack<br \/>\nreturn o.unpack(s)<br \/>\nstruct.error: unpack requires a string argument of length 12<\/p>\n<p>The cheeseshop\/pypi seems to show no suitable alternative which brings up a quandry.\u00a0 For this particular solution, I&#8217;ll write a wrapper function to do the heavy lifting on the two unpack strings I need to deal with and then I&#8217;ll debate pulling the perl unpack\/pack routines out of the perl source and wrapping it into an .egg, possibly for distribution.<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/programming\/python\/python-perl-and-php-interoperability-with-pack-and-unpack\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Perl has very powerful capabilities for dealing with structures.\u00a0 PHP&#8217;s support of those structures was based on Perl&#8217;s wisdom.\u00a0 Python went a different direction. Perl pack\/unpack definitions PING_FORMAT =&gt; &#8216;(a4n2N2N\/a*)@245&#8217;; TASK_FORMAT =&gt; &#8216;a4NIN\/a*a*&#8217;; RETR_FORMAT =&gt; &#8216;a4N\/a*N&#8217;; ENCPAYLOAD_FORMAT =&gt; &#8216;Na*&#8217;; PHP pack\/unpack definitions define(&#8216;TASK_FORMAT&#8217;, &#8216;a4NINa*a*&#8217;); define(&#8220;ENCPAYLOAD_FORMAT&#8221;,&#8217;Na*&#8217;); For a communications package written in perl that communicates with [&hellip;]<\/p>\n<div style=\"float:left;\">\n<div id=\"fb-root\"><\/div>\n<fb:like href=\"https:\/\/cd34.com\/blog\/programming\/python\/python-perl-and-php-interoperability-with-pack-and-unpack\/\" width=\"250\" send=\"false\" show_faces=\"false\" layout=\"button_count\" action=\"recommend\"><\/fb:like>\n<\/div><div style=\"clear:both;\"><\/div>","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,7],"tags":[31,17,30,299,32],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-programming","category-python","tag-pack","tag-perl","tag-php","tag-python","tag-unpack"],"_links":{"self":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/comments?post=629"}],"version-history":[{"count":1,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/posts\/629\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/media?parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/categories?post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cd34.com\/blog\/wp-json\/wp\/v2\/tags?post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}