簡體   English   中英

socket.gaierror:[Errno -2] Python3不知道的名稱或服務

[英]socket.gaierror: [Errno -2] Name or service not known with Python3

我正在嘗試使用端口掃描程序。

import socket
import subprocess
import sys
from datetime import datetime

subprocess.call('clear', shell=True)

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

print( "-" * 60)
print( "Please wait, scanning remote host", remoteServerIP)
print( "-" * 60)

t1 = datetime.now()

try:
    for port in range(1,1025):  
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((remoteServerIP, port))
        if result == 0:
            print( "Port {}:     Open".format(port))
        sock.close()

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( "Couldn't connect to server")
    sys.exit()

t2 = datetime.now()
total =  t2 - t1
print( 'Scanning Completed in: ', total)

但它沒有用。

Enter a remote host to scan: http://www.osjajinci.com/
Traceback (most recent call last):
  File "portscanner.py", line 12, in <module>
    remoteServerIP  = socket.gethostbyname(remoteServer)
socket.gaierror: [Errno -2] Name or service not known

我試圖了解更多關於套接字的信息,我是begginer。我已經仔細檢查了Python3代碼並且找不到任何錯誤。

socket.gethostbyname需要主機名而不是URL。 您必須提供www.osjajinci.com而不是http://www.osjajinci.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM