Connection only works with manually set values

Asked

Viewed 36 times

0

I have a Socket class that is working normally this way:

class Socket():
    __ffChatSocket = ""
    __meuTelefone = ""
    __WhatsappAPI = ""
    __dest = ()

    def __init__(self, telefone, WhatsappAPI):
        ip = str(self.getFileConfigFF('infoserver','ip'))
        port = int(self.getFileConfigFF('infoserver','port'))
        self.__dest = (ip, port)
        self.__meuTelefone = telefone
        self.socketConnect()
        self.__WhatsappAPI = WhatsappAPI

    def socketConnect(self):
        try:
            self.__ffChatSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.__ffChatSocket.connect(("172.20.50.243", 4321)))
            self.__ffChatSocket.send(self.criarEvento("onCompleteConnection", [self.getNumeroFuturoFone()]))
            thread.start_new_thread(self.socketReceiveData, ())
        except Exception as erro:
            self.logErro("Conexao falhou ")
            self.logErro("O erro foi: ")
            self.logErro(erro)
            self.reconectarSocket()

    def getFileConfigFF(self, tag, subTag):
        Config = configparser.ConfigParser()
        pastaLocal = os.path.dirname(os.path.abspath(__file__))
        Config.read(pastaLocal + '\config.ini')
        caminho = re.findall('"([^"]*)"', Config[tag][subTag])
        return caminho[0]

But I want to take the ip and the port for a file . ini, I have performed several tests using the getFileConfigFF method and it takes the values normally.

I tried to assign values to the connect method in many ways like:

dest = (ip, port)
self.__ffChatSocket.connect(dest)

or

self.__ffChatSocket.connect((ip, port))

I tried to take the values using cast (str and int) and also without the cast, but it always presents the same error when trying to connect:

[Errno 10061] Nenhuma conexÒo p¶de ser feita porque a mßquina de destino as recusou ativamente

This is the last code I tested:

class Socket():
    __ffChatSocket = ""
    __meuTelefone = ""
    __WhatsappAPI = ""
    __dest = ()

    def __init__(self, telefone, WhatsappAPI):
        ip = str(self.getFileConfigFF('infoserver','ip'))
        port = int(self.getFileConfigFF('infoserver','port'))
        self.__dest = (ip, port)
        self.__meuTelefone = telefone
        self.socketConnect()
        self.__WhatsappAPI = WhatsappAPI

    def socketConnect(self):
        try:
            self.__ffChatSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            pprint(self.__dest)
            self.__ffChatSocket.connect(self.__dest)
            self.__ffChatSocket.send(self.criarEvento("onCompleteConnection", [self.getNumeroFuturoFone()]))
            thread.start_new_thread(self.socketReceiveData, ())
        except Exception as erro:
            self.logErro("Conexao falhou ")
            self.logErro("O erro foi: ")
            self.logErro(erro)
            self.reconectarSocket()

    def getFileConfigFF(self, tag, subTag):
        Config = configparser.ConfigParser()
        pastaLocal = os.path.dirname(os.path.abspath(__file__))
        Config.read(pastaLocal + '\config.ini')
        caminho = re.findall('"([^"]*)"', Config[tag][subTag])
        return caminho[0]

I put pprint(self.__dest) before the connection to show the value that was set to self. __dest and what is shown on the screen is:

('172.20.50.247', 4433)

That is, the ip and the port are right, but I don’t understand why it’s not connected and why it normally connects when the values are set manually.

  • Have you managed to solve your problem ? Your code is not returning any errors when you try to use the settings ?

  • The problem was time to get the configuration file, it was already solved.

1 answer

0


The problem was with Encode, of how it was caught the values of the port and ip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.