The example in this article describes how Python loops over remote ports. Share to everyone for your reference. Specifically as follows:
In Ip.txt, one IP address and port number per line, the code can loop through the specified port for these IP addresses.
#!/usr/bin/env python
#-*-CODING:GBK-*-
Import socket,time while
1:
file_obj = open (' Ip.txt ')
For line in File_obj:
try:
sc=socket.socket (Socket.af_inet,socket. SOCK_STREAM)
IP = line.split () [0]
port = Int (Line.split () [1])
print ip,port
#设置超时时间 (0.0)
Sc.settimeout (2)
Sc.connect ((ip,port))
timenow=time.localtime ()
Datenow = Time.strftime ('%y-%m-%d %h:%m:%s ', timenow)
logstr= '%s:%s connection succeeded->%s \ n '% (ip,port,datenow)
print logstr
sc.close ()
Except:
file = open ("Log.txt", "a")
Timenow=time.localtime ()
Datenow = Time.strftime ('%y-%m-%d%h:%m: %s ', timenow)
logstr= '%s:%s connection failed->%s \ n '% (ip,port,datenow)
print logstr
file.write (LOGSTR)
File.close ()
print "Sleep ..."
Time.sleep (10)
Ip.txt format File:
192.168.1.100 33001
192.168.1.101 33001
I hope this article will help you with your Python programming.