AntiGuide: UdpSwitchPythonTestSwing



PagePrincipale :: DerniersChangements :: ParametresUtilisateur :: Vous êtes 216.73.216.92 :: Signaler un abus :: le: 20250720 07:52:47
UdpSwitchPython

obsolete, lire: UdpSwitchPythonTestlight
cet exemple traite un flot continu, sans notion de paquet.

serveur d'interconexion

switch.py
import sys, socket, datetime
# Switch UDP sur port 7778 et reponse au meme port

port=7778
face=""
pile=""
noadd=("","")
pilead=noadd
facead=noadd
nompile=""
nomface=""
aller="."
retour="."

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('',port))

print " Switch UDP port ", port," (frapper ^C pour abandonner)\n"

while 1:
 (buf, address)=sock.recvfrom(2048)
 #print address,"===="+buf+"===="
 done=0
 if not len(buf):
  pass
  #break
 # isoler destinataire
 #print "recu:" , address,buf
 if (buf.find(":")<=0):
   print "pas exp/dest erreur venant de " , address
 fromto=buf[:buf.find(":")]
 if (buf.find(">")<=0):
   print "pas >dest erreur venant de " , address
 from1= fromto[:fromto.find(">")]
 to1= fromto[fromto.find(">")+1:]
 # pile estle premier vu, arbitrairement 
 if (nompile==""):
   nompile=from1
   print "pile: "+nompile
 if (nomface==""): 
   nomface=to1
   print "face: "+nomface
 if (aller=="."):
    aller=nomface+">"+nompile+":"
 if (retour=="."):
    retour=nompile+">"+nomface+":"
 contenu=buf[len(aller):]
 if (buf.find(aller)==0):

    facead=address
    #print "faceadd: ", address 
    #print "buf pile etait ===="+pile+"===="
    if pile=="" :
       pile=aller
    pile=pile + contenu
    #print "buffer now pour pile:" ,pile
    #envoyer a face ce qui est en retard ou vide 
    #print "send " , address , " buf face=", face 
    sock.sendto(face, address)
    #print "repondu:",face
    face=""
    done=1
    
 if (buf.find(retour)==0):
    pilead=address
    #print "pileadd: ", address 
    #print "buf face etait ===="+face+"===="
    if face=="" :
       face=retour
    face=face + contenu 
    #print "buffer now pour face:" , face
    #print "send " , address , " buf pile=", pile 
    sock.sendto(pile, address)
    #print "repondu: " ,pile
    pile=""
    done=1
   
 if (done == 0):
   sock.sendto("error: "+buf, address)


client:
appel: sudo pytjon3 swing.pi pile face
swing.py:
import sys, socket, datetime
# envoie unpaquet UDP au port 7777 d'une adresse WAN et attend reponse
port=7778
switch="flavigny.hd.free.fr"
text=""
print datetime.datetime.now(),'"'+text +'" send to: ' ,switch,":",port
print "entrer ligne ou control-c "
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('',port))
i=0
from1=""
to1=""
for arg in sys.argv:
  if  i==1 :
    from1=arg
  if i==2:
    to1=arg
  i=i+1
token=from1 + ">" + to1 +":"
print "token:===="+token+"===="
text=" "
while 1: 
 text=raw_input("")
 sock.sendto(token+text, (switch, port))

 (buf, address)=sock.recvfrom(2048)
 if len(buf)==0: 
     pasvide=0
 if (len(buf)>0):
    ##print datetime.datetime.now(),"answer from IP: " , address,buf
    print buf