variante de
UdpSwitchPythonDemoDialogue
ucpsend.py
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 "send file over cloud"
print "appel: sudo python "+sys.argv[0]+" [-t] serveur:port un autre file"
print " exemple: sudo python "+sys.argv[0]+" flavigny.hd.free.fr:7778 gauche droite demo.txt"
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 +":"
file=sys.argv[4+trace]
print datetime.datetime.now()
print "send file "+file+" over blog to "+token+" via " ,switch,":",port
nblines=0
nbbytes=0
f=open(file,'r')
if trace: print "token:<<<<"+token+">>>>" , switch, port
sock.sendto(token,(switch, port))
if trace: print "envoye<<<<"+token+">>>>", switch, port
if trace: print "attente reponse"
(buf, address)=sock.recvfrom(2048)
for text in f:
nblines=nblines+1
nbbytes=nbbytes+len(text)
tt=token+"="+text
sock.sendto(tt, (switch, port))
if trace: print "envoye<<<<"+tt+">>>>",(switch, port)
(buf, address)=sock.recvfrom(2048)
# empty packet
sock.sendto(token+"=", (switch, port))
if trace: print "envoye<<<<"+token+"=>>>>",(switch, port)
print "file ",file," ("+str(nbbytes)+" bytes on "+str(nblines)+" lines) sent to ",token, " via " ,switch,":",port