无法使用s将字符串列表迭代到connect函数中

2024-10-02 18:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试确定是否有一个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服务器列表。正在阅读亚历克斯推荐的图书馆链接。你知道吗


Tags: incomhost列表forscanportdef
2条回答

您需要在一个元组中将这两个参数作为一对传递。你知道吗

>>> import socket
>>> s = socket.socket()
>>> help(s.connect)

connect(...) method of socket._socketobject instance
    connect(address)

    Connect the socket to a remote address.  For IP sockets, the address
    is a pair (host, port).

>>> s.connect('127.0.0.1', 80)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alexhall/.pyenv/versions/2.7.10/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
TypeError: connect() takes exactly one argument (2 given)
>>> s.connect(('127.0.0.1', 80))  # works

在通过亚历克斯的建议后,我能够扩展我以前想做的事情。请把这个作为一个完整的例子,用那些mods来编写原始源代码。你知道吗

#!/usr/bin/python

import socket, urllib2

httpList = ['www.espn.com', 'www.google.com', 'www.spoon.com']
smtpList = ['smtp.gmail.com', 'aspmx.l.google.com']
scanHttp = []
scanSmtp = []

def resolveHost(x):
    try:
        h = socket.gethostbyname(x)
        print "Resolved %s to %s" % (x, h)
        return h
    except:
        print "Could not resolve %s" % x

def scan(host,port):
    hostR = socket.gethostbyname(host)#resolveHost(host)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(3)
    s.connect((hostR,port))
    try:
        if (port == 80):
            r = urllib2.urlopen('http://'+str(host)).read()
            if 'DOCTYPE' in r:
                print "%s is up" % host
            elif 'doctype' in r:
                print "%s is up" % host
        elif (port == 25):
            r = s.recv(1024)
            if '220' in r:
                print "%s is up" % host
    except:
        print "Connection to %s timed out" % x


for x in httpList:
    scan(x,80)

for x in smtpList:
    scan(x,25)

这将产生预期的检查结果,即两个列表是否可通过所选端口访问:

C:\Python27\python.exe C:/Users/Casey/Desktop/projects/service_check.py
www.espn.com is up
www.google.com is up
www.spoon.com is up
smtp.gmail.com is up
aspmx.l.google.com is up

Process finished with exit code 0

相关问题 更多 >