Sending raw binary data over sockets in Python 3
<p>I'm working on Huffman Encoding and Decoding. I have encoded a string into binary using Huffman Algorithm and now i want to send it over to another computer over sockets using Python 3 where the encoded data will be decoded back. What would be the most efficient way of doing so ? </p>

<p>Encoder part of code : </p>

<pre><code>import heapq
import socket

class HuffmanEncoder:
    output = {};
    class Node:
        def __init__(self,data,freq,left=None,right=None):
            self.data = data
            self.freq = freq
            self.left = left
            self.right = right

    def __init__(self,root):
        self.root = root

    def isLeaf(root):
        return not root.left and not root.right

    def buildHuffman(p):
        while len(p) != 1:
            left = heapq.heappop(p)[1]
            right = heapq.heappop(p)[1]
            top = HuffmanEncoder.Node('$',left.freq + right.freq)
            top.left = left
            top.right = right
            heapq.heappush(p,(top.freq,top))
        return heapq.heappop(p)[1]

    def printCodes(root,arr,top):
        if root.left:
            arr.insert(top,'0')
            HuffmanEncoder.printCodes(root.left,arr,top + 1)

        if root.right:
            arr.insert(top,'1')
            HuffmanEncoder.printCodes(root.right,arr,top + 1)

        if HuffmanEncoder.isLeaf(root):
            s = ""
            for i in range(0,top):
                s += arr[i]
            HuffmanEncoder.output[root.data] = s
        return HuffmanEncoder.output

def main():
    p = []
    arr = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ']
    freq = [8.167,1.492,2.782,4.253,12.702,2.228,2.015,6.094,6.966,0.153,0.772,4.025,2.406,6.749,7.507,1.929,0.095,5.987,6.327,9.056,2.758,0.978,2.360,0.150,1.974,0.074,25.422]
    for i in range(0,len(arr)):
        x = HuffmanEncoder.Node(arr[i],freq[i])
        heapq.heappush(p,(x.freq,x))

    root = HuffmanEncoder.buildHuffman(p)
    arr = []
    top = 0
    codes = HuffmanEncoder.printCodes(root,arr,top)
    for key in sorted(codes):
        print(key,codes[key])
    s = input()
    for i in range(0,len(s)):
        print(codes[s[i]])

if __name__ == '__main__':
                 main()
</code></pre>