程序无法处理错误

发布于 2025-02-13 08:03:40 字数 2045 浏览 1 评论 0原文

  1. 这是快速主机端口扫描仪的源代码...我添加了一些错误处理...但是该程序无法读取错误处理内容,而当主机无效时,它只是给出了正常的Python错误。 ..我在哪里弄错了?

  2. 如何将此程序的输出打印到文本文件? (我在最后尝试过,但它不起作用。

对任何帮助都非常感谢!

源代码:

#!/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()

当前输出IM(当ertleing无效主机):

输出SS

  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?

  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):

OUTPUT SS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北城挽邺 2025-02-20 08:03:43

您没有处理错误!您的错误在此线上发生。除了块之外,不在尝试中

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

You are not handling the error! Your error happens on this line. not inside the try except block

# Ask for input
remoteServer = input("Enter a remote host to scan: ")
remoteServerIP  = socket.gethostbyname(remoteServer)  # this one.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文