Professional Documents
Culture Documents
TRANSMISIN DE DATOS
CODIFICACIN Y DECODIFICACIN DE DATOS A TRAVES DE DOS
COMPUTADORES EN PYTHON
AUTORES:
Danny Curipoma
Luis Maita
LOJA ECUADOR
2017
Objetivo Especfico:
Desarrollar un algoritmo en Python que permita la codificacin y
decodificacin a travs de dos computadores, junto con la
visualizacin de bits transmitidos.
Objetivos Generales:
Codificacin Hoffman
Es un algoritmo que permite la compresin de datos, con la capacidad
de representarlos en una cadena de smbolos que se obtienen gracias
a un alfabeto. Hoffman usa el menor nmero de bits posible
considerando que debe mantener un orden para la posterior
decodificacin.
Justificacin:
Hoy en da nos encontramos con la necesidad de transmitir grandes
cantidades de informacin con la condicin de la confidencialidad de
la misma, por lo cual yace la propuesta de la implementacin de un
algoritmo fiable en Python que provea la transmisin de datos; en un
computador transmisor como enlace A y un computador B como
receptor el cual tiene la capacidad de decodificar la cadena de bits
enviados por el primer computador. Por lo tanto, se realizar el envo
de una cadena de caracteres hacia un segundo computador por el
puerto ethernet y el receptor debe tener la capacidad de
interpretarlos.
Implementacin:
Para la realizacin del proyecto planteado se escogi el algoritmo
Hoffman el cual sirve para la compresin de datos, codificacin y
decodificacin de una manera ptima respecto a otros algoritmos. En
primer aspecto se realiz la codificacin de una cadena de caracteres
como lo es TRANSMISION DE DATOS dentro del lenguaje de
programacin de Python. Para la utilizacin de dos computadores
conectadas mediante el cable ethernet se utiliz las libreras
siguientes:
1er PC
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*import os
from socket import *
host = "192.168.4.1" # set to IP address of target computer
port = 13002
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
class Node:
# properties
probability = 0.0
symbol = ""
encoding = ""
visited = False
parent = -1
class Huffman:
Tree = None
Root = None
Nodes = []
probs = {}
dictEncoder = {}
# methods
def __init__(self, symbols):
self.initNodes(symbols)
self.buildTree()
self.buildDictionary()
def initNodes(self, probs):
for symbol in probs:
node = Node()
node.symbol = symbol
node.probability = probs[symbol]
node.visited = False
self.Nodes.append(node)
self.probs[symbol]=probs[symbol]
def showTree(self):
print "--------------------------"
for i in range(0, len(self.Nodes)):
symbol = self.Nodes[i].symbol
prob = self.Nodes[i].probability
visited = self.Nodes[i].visited
parent = self.Nodes[i].parent
encoding = self.Nodes[i].encoding
print "[%d] Symbol= %s, Prob(%s) = %0.4f, visited=%d;
parent=%d; enc=%s" % (i, symbol, symbol, prob, visited, parent,
encoding)
def buildTree(self):
indexMin1 = self.getNodeWithMinimumProb()
indexMin2 = self.getNodeWithMinimumProb()
while indexMin1 != -1 and indexMin2 != -1:
node = Node()
node.symbol = "."
node.encoding = ""
prob1 = self.Nodes[indexMin1].probability
prob2 = self.Nodes[indexMin2].probability
and
if indexMin != -1:
self.Nodes[indexMin].visited = True
return indexMin
def showSymbolEncoding(self, symbol):
found = False
index = 0
encoding = ""
for i in range(0, len(self.Nodes)):
if self.Nodes[i].symbol == symbol:
found = True
index = i
break
if found:
while index != -1:
encoding = "%s%s" % (self.Nodes[index].encoding,
encoding)
index = self.Nodes[index].parent
else:
encoding = "Unknown symbol"
return encoding
def buildDictionary(self):
for symbol in self.probs:
encoding = self.showSymbolEncoding(symbol)
self.dictEncoder[symbol] = encoding
if __name__=="__main__":
# symbols,probabilities table
symbols = {"T":0.1, "R": 0.05, "A": 0.1, "N": 0.1, " ": 0.1, "S":
0.15, "M": 0.05, "I":0.1, "O": 0.1, "D": 0.1, "E": 0.05}
# instantiate encoder
huffman = Huffman(symbols)
print "Simbolos"
for symbol in symbols:
print "Simbolo: %s; Codificacion: %s" % (symbol,
huffman.showSymbolEncoding(symbol))
test = "CAAASAAAS"
encoded = huffman.encode(test)
print "Mensaje: %s; Codificacion: %s" % (test, encoded)
data = encoded
UDPSock.sendto(data, addr)
UDPSock.close()
os._exit(0)
2do PC
Como en la configuracin del primer computador se hace la
asignacin de una direccin IP especfica para que este computador2
sea considerado como el receptor as:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*import os
from socket import *
host = ""
port = 13001
buf = 1024
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
class Node:
# propiedades
probability = 0.0
symbol = ""
encoding = ""
visited = False
parent = -1
class Huffman:
Tree = None
Root = None
Nodes = []
probs = {}
dictEncoder = {}
# methods
def __init__(self, symbols):
self.initNodes(symbols)
self.buildTree()
self.buildDictionary()
def initNodes(self, probs):
for symbol in probs:
node = Node()
node.symbol = symbol
node.probability = probs[symbol]
node.visited = False
self.Nodes.append(node)
self.probs[symbol]=probs[symbol]
def showTree(self):
print "--------------------------"
for i in range(0, len(self.Nodes)):
symbol = self.Nodes[i].symbol
prob = self.Nodes[i].probability
visited = self.Nodes[i].visited
parent = self.Nodes[i].parent
encoding = self.Nodes[i].encoding
print "[%d] Symbol= %s, Prob(%s) = %0.4f, visited=%d;
parent=%d; enc=%s" % (i, symbol, symbol, prob, visited, parent,
encoding)
def buildTree(self):
indexMin1 = self.getNodeWithMinimumProb()
indexMin2 = self.getNodeWithMinimumProb()
while indexMin1 != -1 and indexMin2 != -1:
node = Node()
node.symbol = "."
node.encoding = ""
prob1 = self.Nodes[indexMin1].probability
prob2 = self.Nodes[indexMin2].probability
node.probability = prob1 + prob2
node.visited = False
node.parent = -1
self.Nodes.append(node)
self.Nodes[indexMin1].parent = len(self.Nodes) - 1
self.Nodes[indexMin2].parent = len(self.Nodes) - 1
# rule: 0 to highest probability, 1 to lowest.
if prob1 >= prob2:
self.Nodes[indexMin1].encoding = "0"
self.Nodes[indexMin2].encoding = "1"
else:
self.Nodes[indexMin1].encoding = "1"
self.Nodes[indexMin2].encoding = "0"
# self.showTree()
indexMin1 = self.getNodeWithMinimumProb()
indexMin2 = self.getNodeWithMinimumProb()
def getNodeWithMinimumProb(self):
minProb = 1.0
indexMin = -1
for index in range(0, len(self.Nodes)):
if (self.Nodes[index].probability < minProb
(not self.Nodes[index].visited)):
minProb = self.Nodes[index].probability
indexMin = index
and
if indexMin != -1:
self.Nodes[indexMin].visited = True
return indexMin
def showSymbolEncoding(self, symbol):
found = False
index = 0
encoding = ""
for i in range(0, len(self.Nodes)):
if self.Nodes[i].symbol == symbol:
found = True
index = i
break
if found:
while index != -1:
encoding = "%s%s" % (self.Nodes[index].encoding,
encoding)
index = self.Nodes[index].parent
else:
encoding = "Unknown symbol"
return encoding
def buildDictionary(self):
for symbol in self.probs:
encoding = self.showSymbolEncoding(symbol)
self.dictEncoder[symbol] = encoding
def encode(self, plain):
encoded = ""
for symbol in plain:
encoded = "%s%s" % (encoded, self.dictEncoder[symbol])
return encoded
def decode(self, encoded):
index = 0
decoded = ""
while index < len(encoded):
founf = False
aux = encoded[index:]
for symbol in self.probs:
if aux.startswith(self.dictEncoder[symbol]):
decoded = "%s%s" % (decoded, symbol)
index = index + len(self.dictEncoder[symbol])
break
return decoded
if __name__=="__main__":
# Tabla de probabilidades de smbolo
symbols = {"T":0.1, "R": 0.05, "A": 0.1, "N": 0.1, " ": 0.1, "S":
0.15, "M": 0.05, "I":0.1, "O": 0.1, "D": 0.1, "E": 0.05}
# instantiate encoder
huffman = Huffman(symbols)
Fig.8 Comprobacin de los datos enviados a travs del puerto ethernet entre dos
computadores
CONCLUSIONES:
ANEXOS
IMPLEMENTACION PARA LA CODIFICACION Y DECODIFICACION