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

时间:2016-04-29 01:11:23

标签: python sockets

我正在尝试确定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服务器列表。现在阅读Alex推荐的图书馆链接。

2 个答案:

答案 0 :(得分:0)

您需要在一个元组中将两个参数作为一对传递。

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

答案 1 :(得分:0)

在完成Alex的建议之后,我能够扩展我以前想做的事情。请将此视为原始源代码的那些mod的完整示例。

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