超时后Telnet重新连接

时间:2018-10-31 11:14:54

标签: sockets python-3.6 telnet

我有一个代码,可以从telnet检索数据。在超时异常情况下如何重新连接?

import socket
from time import sleep


def telnet_connect(host, port):
    try:
        recv_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        recv_socket.settimeout(10)
        recv_socket.connect((host, port))
        print('Socket connection established...\r\n')

        while True:
            data = recv_socket.recv(1024)
            #     Some code here
            sleep(0.5)

    except socket.timeout as err:
        print(f"Connection closed by timeout" + str(err.args))

    except TimeoutError as err:
        print(f"Connection closed by timeout" + str(err.args))

0 个答案:

没有答案