我正在尝试确定是否有一个Web服务器列表正在侦听具有套接字库的端口。但是,我无法使迭代在列表中成功运行:
#!/usr/bin/python
import socket
myList = ['www.google.com', 'www.spoon.com']
scanList = []
port = 80
def resolveHost(x):
try:
h = socket.gethostbyname(x)
except:
pass
return h
def scan(host,port):
s = socket.socket()
s.connect(host,port)
print s.recv(3)
for x in myList:
scanList.append(resolveHost(x))
print scanList
for x in scanList:
scan(x,25)
This is returning:
Traceback (most recent call last):
['216.58.199.196', '207.32.184.61']
File "C:/Users/Casey/Desktop/projects/dsid_check.py", line 28, in <module>
scan(x,25)
File "C:/Users/Casey/Desktop/projects/dsid_check.py", line 18, in scan
s.connect(host,port)
File "C:\Python27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
TypeError: connect() takes exactly one argument (2 given)
Process finished with exit code 1
我希望看到的是那些页面的回复,但我没有。你知道吗
----------编辑的源----------------
因此,我修改了我的源代码:
#!/usr/bin/python
import socket
myList = ['www.espn.com', 'www.google.com', 'www.spoon.com']
scanList = []
port = 80
def resolveHost(x):
try:
h = socket.gethostbyname(x)
return h
except:
print "Could not resolve %s" % x
def scan(host,port):
hostR = resolveHost(host)
s = socket.socket()
s.settimeout(3)
s.connect((hostR,port))
try:
print s.recv(1024)
except:
print "Connection to %s timed out" % x
for x in myList:
scanList.append(resolveHost(x))
for x in scanList:
scan(x,port)
有了这个,我无法连接到一个应该在线的web服务器列表。正在阅读亚历克斯推荐的图书馆链接。你知道吗
您需要在一个元组中将这两个参数作为一对传递。你知道吗
在通过亚历克斯的建议后,我能够扩展我以前想做的事情。请把这个作为一个完整的例子,用那些mods来编写原始源代码。你知道吗
这将产生预期的检查结果,即两个列表是否可通过所选端口访问:
相关问题 更多 >
编程相关推荐