예제 #1
0
    def sendString(self, string):
        ret = NetworkObject()
        try:
            if self.noServer:
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sslSock = ssl.wrap_socket(
                    self.socket,
                    cert_reqs=ssl.CERT_REQUIRED,
                    ca_certs="network/certs/serverCert.pem")
                self.sslSock.connect((consts.TCP_IP, consts.TCP_PORT))
                self.noServer = False

            self.sslSock.send(str.encode(string + '\0'))

            reading = True
            frags = []
            while reading:
                chunk = self.sslSock.recv(4096).decode()
                if chunk[len(chunk) - 1:len(chunk)] == '\0' or chunk == "":
                    reading = False
                    frags.append(
                        chunk[0:len(chunk) - 1]
                    )  # remove the null, it'll mess up NetworkObject.initialize()
                else:
                    frags.append(chunk)
            response = ''.join(frags)

            ret.initialize(response)
        except ConnectionRefusedError:
            self.noServer = True
            ret.error = consts.ERROR_CONNECTION_FAILED
        finally:
            return ret
예제 #2
0
    def sendString(self, string):
        ret = NetworkObject()
        try:
            if self.noServer:
                self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                self.sslSock = ssl.wrap_socket(
                    self.socket,
                    cert_reqs=ssl.CERT_REQUIRED,
                    ca_certs="network/certs/serverCert.pem")
                self.sslSock.connect((consts.TCP_IP, consts.TCP_PORT))
                self.noServer = False

            self.sslSock.write(str.encode(string))
            response = self.sslSock.recv(self.bufferSize).decode(
            )  # make sure to convert from aob to string
            ret.initialize(response)
        except ConnectionRefusedError:
            self.noServer = True
            ret.error = consts.ERROR_CONNECTION_FAILED
        finally:
            return ret