IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Voir le flux RSS

nothus

[Python] Mini-moteur d'enregistrement type NoSQL

Noter ce billet
par , 18/12/2015 à 15h03 (1055 Affichages)
A des fins de "daemonisation"... L'enregistrement du contenu reçu par socket est au format JSON et limité ici au local (127.0.0.1).

La signature (en début de chaque ligne) est générée pour répondre au besoin de référencement : date, module d'appel (en somme quel script fait appel au moteur), l'URL qui sert d'identifiant.

Code python : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import time, socket, threading, re, json
from time import gmtime, strftime 
from threading import Thread, RLock
 
verrou = threading.Lock()
 
class Enregistrer(Thread): 
    def __init__(self,client):
        Thread.__init__(self)  
        client, adresse = client
        self.contenu = client.recv(1024) 
        self.adresse = adresse[0] 
        self.client = client 
        if self.adresse!="127.0.0.1":
            self.cloturer("403")
 
    def run(self):
        verrou.acquire()
        etat = "0" 
        try: 
            self.contenu = self.contenu.splitlines()
            ReCompile = re.compile("^([A-Za-z0-9\-\_]+)\: (.*)$")
            contenu = {} 
            reference = "generique" 
            for ligne in self.contenu: 
                ligne_r = ReCompile.match(ligne.decode()) 
                if ligne_r!=None:
                   contenu[ligne_r.group(1)] = ligne_r.group(2)
                   if ligne_r.group(1)=="reference" and re.search("^([a-zA-Z0-9\.\-]+)$",ligne_r.group(2))!=None:
                       reference = ligne_r.group(1)
            fichierNoSQL.write(
                (strftime("#%Y:%m:%d:%H:%M:%S|", gmtime())+reference+"|"+self.adresse+"#"+json.dumps(contenu)+"\n").encode("UTF8")
            ) 
            fichierNoSQL.flush()
            self.cloturer("200") 
        except:
            self.cloturer("500") 
        verrou.release() 
 
    def cloturer(self,code): 
        self.client.send(("HTTP "+code+" OK\n").encode())
        self.client.send(("Content-type: text/html\n").encode())
        self.client.send(("\n").encode())
        self.client.send(("\n").encode())
        try: 
            self.client.send(code.encode())
        except:
            print("Erreur envoi du retour (r = "+code+")")
        try:
            self.client.close()
        except:
            print("Impossible de clore le client")
 
 
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind(('', 8082))
 
with open('./base.nosql', 'ab') as fichierNoSQL:
    while True: 
        socket.listen(5)
        enr = Enregistrer(socket.accept())
        enr.start()
    fichierNoSQL.close()

Le résultat dans le fichier base.nosql, lorsque je lance Firefox sur le port 8082 :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
#2015:12:18:13:57:17|generique|127.0.0.1#{"Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4", "Accept-Encoding": "gzip, deflate, sdch", "Connection": "keep-alive", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36", "Host": "127.0.0.1:8082", "Cache-Control": "max-age=0", "Upgrade-Insecure-Requests": "1"}
#2015:12:18:13:57:17|generique|127.0.0.1#{"Accept-Language": "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4", "Accept-Encoding": "gzip, deflate, sdch", "Connection": "keep-alive", "Accept": "*/*", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36", "Host": "127.0.0.1:8082", "Referer": "http://127.0.0.1:8082/", "Pragma": "no-cache", "Cache-Control": "no-cache"}

Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Viadeo Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Twitter Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Google Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Facebook Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Digg Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Delicious Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog MySpace Envoyer le billet « [Python] Mini-moteur d'enregistrement type NoSQL » dans le blog Yahoo

Catégories
Programmation , Python

Commentaires