0

I want to find all the ports that are open on my localhost using telnet. I have set up a server using sockets on port 1234. I am using the following code to check whether a port is open or not

import getpass
import sys
import telnetlib

HOST = "127.0.0.1"
for port in range (1,65535):
    try:
        if(telnetlib.Telnet(HOST,port)):
            print(port)
    except ConnectionRefusedError as err:
        print("connection refused")

However I get no output when I run this code. Appreciate any corrections in the code.Thanks

Anuj Kulkarni
  • 137
  • 10
  • 1-65535 = -65534 (look at "range" usage) Have nice day – Damian Jan 15 '19 at 19:53
  • yes thank you i have corrected this small mistake.However the problem still persists I am trying to telnet to each port on the IP to find which ports are open. – Anuj Kulkarni Jan 15 '19 at 20:17
  • That's wierd, it works for me. I just needed to remove this nasty "print("connection refused")" which was spamming nonsense (I replaced it with "pass"), now I see only open ports ;) – Damian Jan 15 '19 at 20:21
  • Ok it seems to be working now. I just restarted sublime text and it worked.Thanks – Anuj Kulkarni Jan 15 '19 at 21:09

2 Answers2

1

Arguments passed to a function should be delimited by a comma, not a dash:

for port in range(1, 65536):
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Modify the exception handling line without assigning the exception to a variable such below.

except ConnectionRefusedError:
        print("connection refused")

It worked for me without any error.