Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

Python socket: passing in IP list?

This is probably a simple fix but I can't figure out how to format the input in the connect function below. Using an IP in single quotes works i.e. connect = s.connect(('192.168.1.2',25)) however I am trying to pass in a list of IPs into the function instead of a single IP. This list of IPs is a simple text file with one IP per line.

#!/usr/bin/python

import socket
import sys

# for loop cycling through file
with open(sys.argv[2], "r") as a_file:
    for line in a_file:

        ip_addr = line

        # Create a Socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # Connect to the Server
        connect = s.connect((ip_addr,25))

        # Receive the banner
        banner = s.recv(1024)

        print banner

        # VRFY a user
        s.send('VRFY ' + sys.argv[1] + '
')
        result = s.recv(1024)

        print result

        # Close the socket
        s.close()

This is my error output

./smtp-vrfy-loop.py user1 smtp-open.txt

Traceback (most recent call last):
  File "./smtp-vrfy-loop.py", line 17, in <module>
    connect = s.connect((ip_addr,25))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.gaierror: [Errno -2] Name or service not known

Contents of the file

$cat smtp-open.txt
192.168.1.2
192.168.1.3
192.168.1.4

Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...