]> gitweb.pimeys.fr Git - NK2015_Client_Python_Alpha.git/commitdiff
On peut filer l'IP du serveur en plus des keywords
authorVincent Le Gallic <legallic@crans.org>
Fri, 22 Jun 2012 16:55:05 +0000 (18:55 +0200)
committerVincent Le Gallic <legallic@crans.org>
Fri, 22 Jun 2012 16:55:05 +0000 (18:55 +0200)
client.py

index 42668ec003257cf062d25ec94d5139bfbde97dac..50da2402fc4a8b22c2c6b0625c6d79e8f0aaaca9 100755 (executable)
--- a/client.py
+++ b/client.py
@@ -19,10 +19,23 @@ from pprint import pprint
 sys.path.append(client_config.rsa_path)
 import monRSA
 
-def connect(sock,where="cerebro",port=None):
+def full_read(sock):
+    """Lit sur la socket jusqu'à ce que l'output soit déJSON-izable"""
+    output=""
+    while True:
+        output+=sock.read()
+        try:
+            return json.loads(output)
+        except:
+            pass
+
+def connect(sock,where,port=None):
     if port==None:
         port=client_config.port
-    ip={"ici":"localhost","vent":"vent.crans.org","eva":"eva.crans.org","cerebro":"cerebro.crans.org"}[where]
+    try:
+        ip={"ici":"localhost","vent":"vent.crans.org","eva":"eva.crans.org","cerebro":"cerebro.crans.org"}[where]
+    except:
+        ip=where
     sock.connect((ip,port))
     return ssl.wrap_socket(sock,ca_certs=client_config.ca_certfile)
 #              cert_reqs=ssl.CERT_NONE,ciphers="AES256-SHA")
@@ -60,12 +73,11 @@ def login(socket,user,mdp="",typ="bdd"):
     
 def search(socket,term):
     a.write("search %s"%(term))
-    return json.loads(a.read())
+    return lit(a)
 
 def lit(socket):
     try:
-        t=a.read(4096)
-        obj=json.loads(t)
+        obj=full_read(socket)
         if obj["retcode"]==0:
           return obj['msg']
         else:
@@ -76,22 +88,6 @@ def lit(socket):
     except ValueError:
         return "Serveur mort ?"
 
-def challenge(a):
-    _,pub=monRSA.litcles(None,client_config.server_rsa_pub_key)
-    chall=str(time.time())
-    chall+="".join(["azertyuiopqsdfghjklmwxcvbn"[random.randrange(0,26)] for i in range(20)])
-    coded=monRSA.crypte(chall,pub)
-    a.send('challenge "%s"'%(coded))
-    a.setblocking(True)
-    result=a.read()
-    a.settimeout(0.5)
-    if json.loads(result)==chall:
-        print "Recognized"
-    elif json.loads(result)==u"Challenge tenté il y a moins de 10 minutes ! =p":
-        print "Top tôt. Réessaye plus tard."
-    else:
-        print "Imposteur !"
-    
 if __name__=="__main__":
     where=raw_input("quel serveur ?")
     #port=int(raw_input("port ? "))