gpt4 book ai didi

python 套接字,sock.connect 错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:15:26 25 4
gpt4 key购买 nike

import socket
from datetime import datetime

#User input
ServerIP = raw_input("Enter server IP: ")
RemoteServerIP = socket.gethostbyaddr(ServerIP)

print "-" * 60
print "Please wait, scanning remote host", RemoteServerIP
print "-" * 60

#Starting to scan
StartTime = datetime.now()

for port in range(1,1025):#ports 1-1024
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
result = sock.connect_ex((RemoteServerIP, port))
if result == 0:
print "Port {}: \t Open".format(port)
sock.close()

#caculate the time and print to screen
EndTime = datetime.now()
TotalTime = StartTime - EndTime
print 'Scanning Completed in: ', TotalTime

错误:

    result = sock.connect_ex((RemoteServerIP, port))

File "C:\Python27\lib\socket.py", line 228, in meth

return getattr(self._sock,name)(*args)

TypeError: coercing to Unicode: need string or buffer, tuple found

最佳答案

socket.gethostbyaddr() returns a tuple 。访问ipaddrlist中的第一项应该可以满足您的需要:

RemoteServerIP = socket.gethostbyaddr(ServerIP)[2][0]

关于python 套接字,sock.connect 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com