简体   繁体   English

程序无法处理错误

[英]Program is unable to handle Errors

  1. Here's the source code for a Fast Host Port Scanner... I added some error handling... but the program is unable to read that error handling thing and it just gives out the normal python error when the host is invalid... Where am I getting it wrong?这是快速主机端口扫描器的源代码...我添加了一些错误处理...但是程序无法读取该错误处理内容,并且当主机无效时它只会给出正常的 python 错误...在哪里我弄错了吗?

  2. How do I print the output of this program to a Text File?如何将此程序的输出打印到文本文件? (I have tried it at the end but it doesnt work. (最后我试过了,但它不起作用。

Would be Thankful to any help!!将不胜感激任何帮助!

SOURCE CODE:源代码:

#!/usr/bin/env python
import socket
import concurrent.futures
import subprocess
import sys
from datetime import datetime
    
# Clear the screen
subprocess.call('clear', shell=True)

# Ask for input
remoteServer = input("Enter a remote host to scan: ")
remoteServerIP  = socket.gethostbyname(remoteServer)

# Prints a banner with info on which host we are about to scan
print ("-" * 60)
print ("Please wait, scanning remote host", remoteServerIP)
print ("-" * 60)

# Check what time the scan started
t1 = datetime.now()

def scan(remoteServerIP, port):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.settimeout(1)
    
    try:
        remoteServerIP  = socket.gethostbyname(remoteServer)
        result = sock.connect_ex((remoteServerIP, port))        
        if result == 0:
            print ("Port {}:      Open".format(port))
            sock.close()
            
    # We have also put in error handling for catching errors
    except KeyboardInterrupt:
        print ("You pressed Ctrl+C")
        sys.exit()

    except socket.gaierror:
        print ('Hostname could not be resolved. Exiting')
        sys.exit()

    except socket.error:
        print ("Host is not available",)
        sys.exit()

with concurrent.futures.ThreadPoolExecutor(max_workers=75) as executor:
    for port in range(1,1025):
        executor.submit(scan, remoteServerIP, port + 1)

# Checking the time again
t2 = datetime.now()

# Calculates the difference of time, to see how long it took to run the script
total =  t2 - t1

# Printing the information to screen
print ('Scanning Completed in: ', total)

#Text file
f = open('Hostreport.txt', 'a')
print(port,file=f)
f.close()

Current OUTPUT Im getting (When etering invalid host):我得到的当前输出(当 etering 无效主机时):

OUTPUT SS输出端

You are not handling the error!您没有处理错误! Your error happens on this line.您的错误发生在这一行。 not inside the try except block不在 try except 块内

# Ask for input
remoteServer = input("Enter a remote host to scan: ")
remoteServerIP  = socket.gethostbyname(remoteServer)  # this one.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM