Bonjour

je met en place une com par socket
J'ai un fichier client.py qui envoie les data vers le server avec une boucle while + input pour donner la commande mais ma boucle envoie une fois la command et ne reviens pas sur l'input
La parti server répond bien a mes commandes.
Je pense que c'est la partie socket qui bloque mais je ne vois pas quoi ?

Nom : pbsocket.png
Affichages : 126
Taille : 6,8 Ko

server.py
Code : 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import socket
import configparser
from psu import Get_Tension_V1,Get_Tension_V2, Set_Tension_V1, Set_Tension_V2
from psu import Get_Intensite_V1, Get_Intensite_V2, Set_Intensite_V1, Set_Intensite_V2
from psu import V1_ON, V1_OFF, V2_ON, V2_OFF
import logging
import time
import threading
 
class plugin():
    def __init__(self) -> None:
        self.IP,self.PORT = "",""
        self.rep = ""
        self.s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        self.parametrage()
        self.loggerinit()
        self.SocketConnect()
 
 
    def startlistening(self):
        self.t = threading.Thread(target=self.Listening)
        self.t.start()
 
    def date_time_log(self,sep="/"):
        now = time.localtime(time.time())
        annee = str(now.tm_year)
        mois = str(now.tm_mon)
        jour = str(now.tm_mday)
        heure = str(now.tm_hour)
        minute = str(now.tm_min)
        seconde = str(now.tm_sec)
        return jour.zfill(2) +sep + mois.zfill(2)+ sep + annee.zfill(4) + " " + heure.zfill(2) + ":" + minute.zfill(2) + ":" + seconde.zfill(2)
 
    def date_time(self):
        now = time.localtime(time.time())
        annee = str(now.tm_year)
        mois = str(now.tm_mon)
        jour = str(now.tm_mday)
        heure = str(now.tm_hour)
        minute = str(now.tm_min)
        seconde = str(now.tm_sec)
        return jour.zfill(2) +"_" + mois.zfill(2)+ "_" + annee.zfill(4)
 
    def parametrage(self):
        try:
            #param
            FileIni = "server.ini"
            config = configparser.ConfigParser()
            config.read(FileIni)
            self.IP = config["PARAMETRE"]["IP"]
            self.PORT = config["PARAMETRE"]["PORT"] 
        except Exception as exc:
            logging.critical(f"{self.date_time_log()} ==> {exc}")
 
    def loggerinit(self):
        logging.basicConfig(filename='Log_Server'  + str(self.date_time()) + ".txt",  level=logging.DEBUG)
 
    def SocketConnect(self):
        try:
            # Bind and listen
            self.s.bind((self.IP,int(self.PORT)))
            self.s.listen()
            print(f"Server Listening IP {self.IP} PORT {self.PORT}")
 
        except Exception as exc:
            logging.critical(f"{self.date_time_log()} ==> {exc}")
 
 
    def Listening(self):
        logging.info(str(self.date_time_log())  + f" : Server Listening IP {self.IP} PORT {self.PORT}")
        # Accept connections
        while(True):
            try:
                (clientConnected, clientAddress) = self.s.accept()
                print("Accepted a connection request from %s:%s"%(clientAddress[0], clientAddress[1]))
 
                dataFromClient = clientConnected.recv(1024).decode("utf-8")
                dataFromClient = dataFromClient.replace("\r","").replace("\n","")
                print(dataFromClient)
 
                app = str(dataFromClient).split(";")[0]
                cmd = str(dataFromClient).split(";")[1]
                logging.info(str(self.date_time_log())  + ' : Receive From BTMA : ' + str(dataFromClient))
                exec("self.rep = " + cmd)
                logging.info(str(self.date_time_log())  + ' : Receive From Plugin : ' + str(self.rep))
                clientConnected.send(self.rep.encode())
 
            except Exception as exc:
                logging.critical(f"{self.date_time_log()} ==> {exc}")
 
if __name__ == "__main__":
    plug = plugin()
    plug.startlistening()
client.py

Code : 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import socket
import time
import configparser
import logging
import time
 
#param
FileIni = "server.ini"
config = configparser.ConfigParser()
config.read(FileIni)
HOST = config["PARAMETRE"]["IPCPX"]
PORT = config["PARAMETRE"]["PORTCPX"] 
 
 
def date_time_log(sep="/"):
    now = time.localtime(time.time())
    annee = str(now.tm_year)
    mois = str(now.tm_mon)
    jour = str(now.tm_mday)
    heure = str(now.tm_hour)
    minute = str(now.tm_min)
    seconde = str(now.tm_sec)
    return jour.zfill(2) +sep + mois.zfill(2)+ sep + annee.zfill(4) + " " + heure.zfill(2) + ":" + minute.zfill(2) + ":" + seconde.zfill(2)
 
def date_time():
    now = time.localtime(time.time())
    annee = str(now.tm_year)
    mois = str(now.tm_mon)
    jour = str(now.tm_mday)
    heure = str(now.tm_hour)
    minute = str(now.tm_min)
    seconde = str(now.tm_sec)
    return jour.zfill(2) +"_" + mois.zfill(2)+ "_" + annee.zfill(4)
 
logging.basicConfig(filename='Log_Server'  + str(date_time()) + ".txt",  level=logging.DEBUG)
 
# TENSION
 
def Get_Tension_V1():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        s.send(b"V1?")
        retour = s.recv(1024).decode("utf-8")
        val = retour.replace("\r","").replace("\n","").split(" ")
        if len(val) > 1:
 
            return f"OK;{val[1]}"
 
        return "FAIL"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
 
def Set_Tension_V1(val):
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "V1 {}".format(val)
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return("OK")
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def V1_ON():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "OP1 1"
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return "OK"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def V1_OFF():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "OP1 0"
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return "OK"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def Get_Tension_V2():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        s.send(b"V2?")
        retour = s.recv(1024).decode("utf-8")
        val = retour.replace("\r","").replace("\n","").split(" ")
        if len(val) > 1:
            return f"OK;{val[1]}"
        else:
            return "FAIL"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def Set_Tension_V2(val):
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "V2 {}".format(val)
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return "OK"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def V2_ON():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "OP2 1"
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return "OK"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def V2_OFF():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "OP2 0"
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return "OK"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
# INTENSITE
 
def Get_Intensite_V1():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        s.send(b"I1?")
        retour = s.recv(1024).decode("utf-8")
        val = retour.replace("\r","").replace("\n","").split(" ")
        if len(val) > 1:
 
            return f"OK;{val[1]}"
 
        return "FAIL"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def Set_Intensite_V1(val):
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "I1 {}".format(val)
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return("OK")
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def Get_Intensite_V2():
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        s.send(b"I2?")
        retour = s.recv(1024).decode("utf-8")
        val = retour.replace("\r","").replace("\n","").split(" ")
        if len(val) > 1:
            return f"OK;{val[1]}"
        else:
            return "FAIL"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")
 
def Set_Intensite_V2(val):
    s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, int(PORT)))
    try:
        cmd = "I2 {}".format(val)
        b = bytes(cmd, 'utf-8') 
        s.send(b)
        return "OK"
    except Exception as exc:
        logging.critical(f"{date_time_log()} ==> {exc}")