0

I have been making a tool for connecting to telnet, but when I try to connect to telnet using user-defined function, I got an error. What is the difference between these 2 codes? why does the second code make an error?

1. code without error

import os, time, re, telnetlib
from time import sleep

if __name__ == '__main__':
    HOST = 'localhost'
    PORT = 14440
    cm_path = 'C:/bin'
    os.chdir(cm_path)
    tn = telnetlib.Telnet(HOST, PORT)

2. code with error

import os, time, re, telnetlib
from time import sleep

def myTelnet(path, host, port):
    os.chdir(path)
    tn = telnetlib.Telnet(host, port)

if __name__ == '__main__':
    HOST = 'localhost'
    PORT = 14440
    cm_path = 'C:/bin'

    tn = myTelnet(cm_path,HOST,PORT)

After running the code 2, I got the error message below:

ConnectionRefusedError: [WinError 10061] \u5bfe\u8c61\u306e\u30b3\u30f3\u30d4\u30e5\u30fc\u30bf\u30fc\u306b\u3088\u3063\u3066\u62d2\u5426\u3055\u308c\u305f\u305f\u3081\u3001\u63a5\u7d9a\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002

JUN
  • 1
  • 3
  • 1
    First code even did not do telnet, no meaning to compare these two. Second code tell you remote refuse you to connect. You need to try use putty to manual telnet to confirm telnet really ok? – atline Jul 10 '18 at 02:40
  • Forgot pasting " tn = telnetlib.Telnet(host, port) " on the last line of the first code. Sorry, but I am kind of new to Python. what is putty? Is it always necessary to be used for the second code? – JUN Jul 10 '18 at 07:56
  • I just wonder if you really can telnet to your server? Not use python code, use a thirdparty out-of-box tool to telnet to server to confirm if your server side really ok as you miss telnet in first code just now. – atline Jul 10 '18 at 08:29
  • Cannot see any difference, and really strange, your telnet work on `14440` not `23` port? Two code both did not report any error on my local, wait for others confirm. – atline Jul 10 '18 at 09:13
  • I have been making a simple tool to automate a simulation software. And using the first code with 2~3 lines more, can open the simulation software and make it to start/stop simulations via telnet. In order to apply the first code to my GUI, I defined a function like the second code, but it had an error as I wrote here. – JUN Jul 10 '18 at 17:06
  • After adding the line " tn.open(host, port) " to the last line, it has solved. Once it connected, could connect to telnet without the last line. – JUN Jul 12 '18 at 03:12

0 Answers0