Created
February 12, 2013 20:49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from struct import Struct | |
from binascii import hexlify | |
import socket | |
from sys import argv | |
subscripciones = {} | |
def server(host, puerto): | |
global subscripciones | |
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) | |
s.bind( ( host, puerto ) ) | |
while True: | |
desempaque = Struct('25s') | |
datos, addr = s.recvfrom( desempaque.size ) | |
desempaque = desempaque.unpack(datos) | |
if str(desempaque[0:1]) == '01': | |
print "Subscripcion" | |
subscripciones[addr] = desempaque[3:8] | |
print subscripciones | |
print "host:", addr[ 0 ] | |
print "puerto:", addr[ 1 ] | |
print "Desempaque: ",desempaque | |
#print "\t" + desempaque | |
s.sendto( datos, addr ) | |
print "Se enviaron paquetes." | |
s.close() | |
def client(host, puerto): | |
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) | |
while True: | |
#datos = "hola" | |
m = "01.Asalto.123,000.-10,000" | |
datos = Struct('25s') | |
datos = datos.pack(m) | |
print "Esto envie: ",datos | |
s.sendto( datos, ( host, puerto ) ) | |
datos, addr = s.recvfrom( 1024 ) | |
print "Paquete recibido:" | |
print "Desde el host:", addr[ 0 ] | |
print "puerto:", addr[ 1 ] | |
print "Contiene:" | |
print "\t" + datos + "\n" | |
s.close() | |
def main(): | |
try: | |
host = argv[2] | |
except: | |
host = "127.0.0.1" | |
try: | |
puerto = int(argv[3]) | |
except: | |
puerto = 5000 | |
print argv[1] | |
if argv[1] == "server": | |
server(host, puerto) | |
if argv[1] == "client": | |
client(host, puerto) | |
print "Especifica si eres server o cliente." | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment