Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获得;TypeError:参数类型为';连接端口错误';是不合适的”;尝试重试Telnet连接时_Python_Error Handling_Try Catch_Typeerror_Telnet - Fatal编程技术网

Python 获得;TypeError:参数类型为';连接端口错误';是不合适的”;尝试重试Telnet连接时

Python 获得;TypeError:参数类型为';连接端口错误';是不合适的”;尝试重试Telnet连接时,python,error-handling,try-catch,typeerror,telnet,Python,Error Handling,Try Catch,Typeerror,Telnet,提供完整的上下文:我的任务是连接到只能通过telnet进行通信的远程设备,提交一些命令,然后从中检索和解析数据流。我将设备列表从数据库拉到一个数组中,并在其中循环,每个设备都有一个telnet连接 当telnet连接意外断开时,我出现以下错误: ConnectionAbortedError:[WinError 10053]主机中的软件中止了已建立的连接 我在telnet连接代码周围放了一个try-catch片段,但是当连接中止时,我现在也会遇到这个错误: TypeError:类型为“Connec

提供完整的上下文:我的任务是连接到只能通过telnet进行通信的远程设备,提交一些命令,然后从中检索和解析数据流。我将设备列表从数据库拉到一个数组中,并在其中循环,每个设备都有一个telnet连接

当telnet连接意外断开时,我出现以下错误:

ConnectionAbortedError:[WinError 10053]主机中的软件中止了已建立的连接

我在telnet连接代码周围放了一个try-catch片段,但是当连接中止时,我现在也会遇到这个错误:

TypeError:类型为“ConnectionAbortedError”的参数不可编辑

除此之外,我不确定我能做什么。该进程正常运行了一段时间,但每次代码的
tn.write(str(command+“\n”).encode(“ascii”)
部分出现错误

    Host = "XX.XX.XX.XX"
    Port = "10" + devicenumber

    print(Host)
    print(Port)

    # Letting you know we're attempting a connection :)
    print(colored("Connecting to..." + Host + " " + Port, 'green'))

    # Allow up to 3 retries to execute the telnet connection
    for i in range(0, 3):
        while True:
            try:
                # Set variable to the Telnet class, passing in the host and port defined above
                tn = telnetlib.Telnet(Host, Port)
                tn.set_debuglevel(9)
                # Each sleep is to allow time for the stream to complete
                time.sleep(1)
                # \x01 is the ^A command you would enter if running commands manually.
                tn.write("\x01".encode("ascii"))
                time.sleep(1)
                # Write the supplied command, press enter
                tn.write(str(command + "\n").encode("ascii"))
            except socket.error as err:
                if "Operation timed out" in err:
                    print
                    "1st if", self.host, err  # progress checking
                    dialTestResult[self.host] = ["", "connection timed out"]
                elif "getaddrinfo failed" in err:
                    print
                    "2nd if", self.host, err  # progress checking
                    dialTestResult[self.host] = ["", "DNS resolution failed"]
                print("There was an error and we're trying again.")
                time.sleep(3)
                continue
            except WindowsError:
                print(colored("The local machine closed the connection. We're trying again.", 'red'))
                time.sleep(3)
                continue
            except ConnectionAbortedError:
                print(colored("Connection aborted error! We're trying again.", 'red'))
                time.sleep(3)
                continue
            break
我只想在连接断开后重试几次,但显然,我不确定如何执行此操作,也不完全确定try语句中的“except”部分如何与oserror一起工作。所以我想我的问题有两个:

  • 为什么这个错误发生在流程中的同一个地方,但是在它已经正常运行了一段时间,并且成功地运行了那部分代码之后
  • 如何在try语句中处理OSError异常
  • 非常感谢您的指导。谢谢

    编辑:对不起,忘了回溯

     File "C:\Users\Zeb\PycharmProjects\VeederReadings\Telnet.py", line 40, in getveederstream
       tn.write(str(command + "\n").encode("ascii"))
     File "C:\Users\Zeb\AppData\Local\Programs\Python\Python37\lib\telnetlib.py", line 290, in write
       self.sock.sendall(buffer)
    ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
     File "C:/Users/Zeb/PycharmProjects/VeederReadings/GetVeederReadings.py", line 8, in <module>
       class GetVeederReadings:
     File "C:/Users/Zeb/PycharmProjects/VeederReadings/GetVeederReadings.py", line 50, in GetVeederReadings
       i37400Stream = Telnet.VeederTelnet.getveederstream("i37400", storeList[storeListCounter])
     File "C:\Users\Zeb\PycharmProjects\VeederReadings\Telnet.py", line 49, in getveederstream
       if "Operation timed out" in err:
    TypeError: argument of type 'ConnectionAbortedError' is not iterable
    
    Process finished with exit code 1
    
    getveederstream中第40行的文件“C:\Users\Zeb\PycharmProjects\VeederReaders\Telnet.py” tn.write(str(命令+“\n”).encode(“ascii”)) 文件“C:\Users\Zeb\AppData\Local\Programs\Python\Python37\lib\telnetlib.py”,第290行,写入 self.sock.sendall(缓冲区) ConnectionAbortedError:[WinError 10053]主机中的软件中止了已建立的连接 在处理上述异常期间,发生了另一个异常: 回溯(最近一次呼叫最后一次): 文件“C:/Users/Zeb/PycharmProjects/VeederReadings/GetVeederReadings.py”,第8行,在 课堂阅读: 文件“C:/Users/Zeb/PycharmProjects/VeederReadings/GetVeederReadings.py”,第50行,在GetVeederReadings中 i37400Stream=Telnet.VeederTelnet.getveederstream(“i37400”,storeList[storeListCounter]) 文件“C:\Users\Zeb\PycharmProjects\VeederReadings\Telnet.py”,第49行,在getveederstream中 如果错误中的“操作超时”: TypeError:类型为“ConnectionAbortedError”的参数不可编辑 进程已完成,退出代码为1
    我们至少需要查看回溯。@Davidsherring抱歉,谢谢。我添加了回溯。该回溯不是来自您显示的代码(行存在,但数字不一致)。但无论如何,为什么要在异常对象中而不是在其消息中查找字符串?如果str(err)中的“操作超时”,请尝试
    -异常不是iterable,但其字符串表示形式是。