UdpSwitchPython
voir une variante passive:
UdpSwitchPythonDemoPmv
pour l'exemple de client présenté ici:
à chaque ligne frappée sur un des clients
- si non vide le contenu est ajouté à la file d'attente de l'adversaire
- ne permet pas d'ajouter un paque vide!
- le switch répond:
- signe = + le premier paquet sur la file
- vide
GAFFE: le dessin du client est bloquant! en cas de non réponse du switch, abandonner par ^C
Appel:
sudo python testline.py [-t] serveur:port moi adversaire
(pour le serveur:
UdpSwitchPythonProto )
import sys, socket, datetime
# envoie un paquet UDP au port 7778 d'une adresse WAN et attend reponse
port=7778
trace=0
switch=""
text=""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('',port))
i=0
from1=""
to1=""
if len(sys.argv)<4 :
print "appel: sudo python "+sys.argv[0]+" [-t] serveur:port un autre "
print " exemple: sudo python "+sys.argv[0]+" flavigny.hd.free.fr:7778 guche droite "
sys.exit(0)
#
if sys.argv[1]=="-t" : trace=1
switch=sys.argv[1+trace]
port=int(switch[switch.find(":")+1:])
switch=switch[:switch.find(":")]
from1=sys.argv[2+trace]
to1=sys.argv[3+trace]
token=from1 + ">" + to1 +":"
antitoken=to1 + ">" + from1 +":"
print datetime.datetime.now(),'"'+text +'" send to: ' ,switch,":",port
if trace: print "token:<<<<"+token+">>>>" , switch, port
sock.sendto(token,(switch, port))
if trace: print "envoye<<<<"+token+">>>>", switch, port
text=""
while 1:
#priorite a la reception
if trace: print "attente retour"
reussi=1
while reussi:
(buf, address)=sock.recvfrom(2048)
if trace: print "recu====="+buf+"===== antitoken:==="+antitoken+"======",buf.find(antitoken)
if buf.find(antitoken+"=")==0: print ">" + buf[len(antitoken)+1:]
if buf.find(antitoken+"=")==-1:reussi=0
sock.sendto(token, (switch, port))
if trace: print "envoye<<<<"+token+">>>>",(switch, port)
fini=0
if trace : print "entrer"
text=raw_input("ecrire: ")
if text!="" : text="="+text
tt=token+text
sock.sendto(tt, (switch, port))
if trace: print "envoye<<<<"+tt+">>>>",(switch, port)