Python Forum

Full Version: Your p“Timeout !!!!” when I enter a known good IP address when running port scanner
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can anyone offer some assistance because i am working on this port scanner and it seems to run fine but when i sent it to another person they said they are getting a time out error. If anyone doesn't mind looking at the code i can share it with them Big Grin

Check code below. thanks guys and girls


#This program does the port scanning from 0 to 1025.

#All results will write in file.

#Errors is managed by exception and will write to file.



import concurrent.futures

import socket

import threading

import time

import sys







print_lock = threading.Lock()



ip = input("Enter the IP to scan: ") #ask user ip for scanning



f = open("scanresult.txt", "w") #open file to write results

print("Scanned IP: ", ip, file=f)





def scan(ip, port):

    scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    scanner.settimeout(1)

    try:

        scanner.connect((ip, port))

        scanner.close()

        with print_lock:

            print(f"[{port}]" + f" is open", file=f)

    except KeyboardInterrupt: #manage exception

        print("\n Exiting Program !!!!", file=f)

        sys.exit()

    except socket.gaierror:

        print("\n Host name Could Not Be Resolved !!!!", file=f)

        sys.exit()

    except socket.herror:

        print("\n Host is not available !!!!", file=f)

        sys.exit()

    except socket.timeout:

        print("\n TIMEOUT !!!!", file=f)

        sys.exit()

    except:

        pass



startscan = time.time() #time when scan started

print(time.ctime(), file=f) #print date and time when scan started

with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor: #actual scanning

    for port in range(1025):

        executor.submit(scan, ip, port + 1)

        break

    

endscan = time.time() #time when scan ended

print(time.ctime(), file=f) #print date and time when scan ended

print("Scan time is: ", int(endscan) - int(startscan), file=f)



f.close() #close file after writing





scan(ip, port)
Why didn't you share the code (minimal reproducible example)?
(May-13-2022, 10:35 AM)buran Wrote: [ -> ]Why didn't you share the code (minimal reproducible example)?

Hi Buran

I guess rookie at this but here it is below


#This program does the port scanning from 0 to 1025.

#All results will write in file.

#Errors is managed by exception and will write to file.



import concurrent.futures

import socket

import threading

import time

import sys







print_lock = threading.Lock()



ip = input("Enter the IP to scan: ") #ask user ip for scanning



f = open("scanresult.txt", "w") #open file to write results

print("Scanned IP: ", ip, file=f)





def scan(ip, port):

scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

scanner.settimeout(1)

try:

scanner.connect((ip, port))

scanner.close()

with print_lock:

print(f"[{port}]" + f" is open", file=f)

except KeyboardInterrupt: #manage exception

print("\n Exiting Program !!!!", file=f)

sys.exit()

except socket.gaierror:

print("\n Host name Could Not Be Resolved !!!!", file=f)

sys.exit()

except socket.herror:

print("\n Host is not available !!!!", file=f)

sys.exit()

except socket.timeout:

print("\n TIMEOUT !!!!", file=f)

sys.exit()

except:

pass



startscan = time.time() #time when scan started

print(time.ctime(), file=f) #print date and time when scan started

with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor: #actual scanning

for port in range(1025):

executor.submit(scan, ip, port + 1)

break



endscan = time.time() #time when scan ended

print(time.ctime(), file=f) #print date and time when scan ended

print("Scan time is: ", int(endscan) - int(startscan), file=f)



f.close() #close file after writing





scan(ip, port)
Hello All, Was off this for a while but yes as for output there isn't. any. The program stalls and the response from the instructor after running the program was "Your program gives me a “Timeout !!!!” when I enter a known good IP address."

Below is what the program does it just sits there with no output after you enter a IP address.

Python 3.9.5 (v3.9.5:0a7dcbdb13, May 3 2021, 13:05:53)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
>>>
= RESTART: /Users/james2000k/Library/Containers/com.apple.mail/Data/Library/Mail Downloads/6259D512-5E2F-4F5B-8F7B-E6224753CF0F/port_scan_socket.py
Enter the IP to scan: 172.*.*.*
>>>
Quote:Thanks for the response. I posted what the output was but to be honest there isn't any other than the fact that it shows timeout

When I enter the IP address of my own computer for a test it just times out. Do you have a port scanner that is working because thats all I basically need a working port scanner that you type the IP address into and it writes results to a file

Please do not send private mail when the community will benefit from your query.
There are many port scanners available, you will have to try until you find one that meets your needs.
See: https://pypi.org/search/?q=%22port+scanner%22
(May-13-2022, 10:35 AM)buran Wrote: [ -> ]Why didn't you share the code (minimal reproducible example)?

Hey Buran

THis is the entire code

# This program does the port scanning from 0 to 1025.

# All results will write in file.

# Errors is managed by exception and will write to file.


import concurrent.futures

import socket

import threading

import time

import sys

print_lock = threading.Lock()

ip = input("Enter the IP to scan: ")  # ask user ip for scanning

f = open("scanresult.txt", "w")  # open file to write results

print("Scanned IP: ", ip, file=f)


def scan(ip):
    scanner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    scanner.settimeout(1)

    try:

        scanner.connect((ip, port))

        scanner.close()

        with print_lock:

            print(f"[{port}]" + f" is open", file=f)

    except KeyboardInterrupt:  # manage exception

        print("\n Exiting Program !!!!", file=f)

        sys.exit()

    except socket.gaierror:

        print("\n Host name Could Not Be Resolved !!!!", file=f)

        sys.exit()

    except socket.herror:

        print("\n Host is not available !!!!", file=f)

        sys.exit()

    except socket.timeout:

        print("\n TIMEOUT !!!!", file=f)

        sys.exit()

    except:

        pass


startscan = time.time()  # time when scan started

print(time.ctime(), file=f)  # print date and time when scan started

with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:  # actual scanning

    for port in range(1025):
        executor.submit(scan, ip, port + 1)

        break

endscan = time.time()  # time when scan ended

print(time.ctime(), file=f)  # print date and time when scan ended

print("Scan time is: ", int(endscan) - int(startscan), file=f)

f.close()  # close file after writing

scan()
When i enter a valid IP address the output is below


Error:
"/Users/james2000k/PycharmProjects/pythonProject4/test hello/bin/python" /Users/james2000k/Desktop/Scanner1241.py Enter the IP to scan: 192.168.1.13 Traceback (most recent call last): File "/Users/james2000k/Desktop/Scanner1241.py", line 90, in <module> scan() TypeError: scan() missing 1 required positional argument: 'ip' Process finished with exit code 1
Your function expects an argument def scan(ip):, then at the end you have scan(), i.e. you don't supply the expected argument. What do you expect to happen?
That said, there are also other problems - e.g. when using ThreadPoolExecutor.
At this point this program that i posted probably has too many bugs to fix. So can anyone either send or post a simple port scanner that will write results to a file. I have to go back and revisit this one with the bugs but for a quick fix i will use a working one if someone has or knows where i can find one


thank you
see post 5: https://python-forum.io/thread-37213-pos...#pid159011
It has a links to all PyPi packages related to port scanning. ( https://pypi.org/search/?q=%22port+scanner%22 )