daironman daironman - 4 months ago 7
Python Question

get input in python script from a file

I am trying to write a script in python which takes input from a file, which is a list of usernames, and run

VRFY
command for smtp server. Below is the script.

I am a almost new to python. I was able to create the below script with help from internet. In this script in have to run the script with a particular username.

How can I provide a file which includes a list of usernames and the script performs
VRFY
for each of the user in the file?

!/usr/bin/python

import socket
import sys
if len(sys.argv) != 2:
print "usage: vrfy.py <username>"
sys.exit(0)

#create socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect to the server
connect=s.connect(('10.10.10.10',25))
#REceive the banner
banner=s.recv(1024)
print banner
#VRFY a user
s.send('VRFY ' + sys.argv[1] + '\r\n')
result=s.recv(1024)
print result
#close the socket
s.close


thanks,

Answer

Assuming users.txt contains the usernames, something like below will do the job.

!/usr/bin/python

import socket
import sys

# Probably u don't need it now
#if len(sys.argv) != 2:
    #print "usage: vrfy.py <username>"
    #sys.exit(0)

with open('users.txt', 'r') as f:
    users = f.readlines()

#create socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#connect to the server 
connect=s.connect(('10.10.10.10',25))
#Receive the banner
banner=s.recv(1024)
print banner

for u in users:
    user = u.strip()

    #VRFY a user
    s.send('VRFY ' + user + '\r\n')
    result=s.recv(1024)
    print result

#close the socket
s.close