I have written a program that gets the information of a server. However, the program says that leng is referenced before assignment. How can I prevent this?
The program first scans for open ports. Then it connects to the first open port it finds. It then gets information about the server. The program then gets the IP address of the program and finally it closes the connection.
import socket
VER = None
portlist = [21,22,25,80,110,443]
ports_test = [21,22,25,80,110,443]
length = 0
leng = 0
siteORip = input("input either a site <eg:www.anysite.com> or an ip address<eg:123.45.67.890>>")
def get_ban():
print("[CONNECTING>]Establishing connection...")
while True:
try:
port = portlist.pop(0)
s = socket.socket()
socket.setdefaulttimeout(5)
s.connect((siteORip,port))
s.send("x".encode())
data = s.recv(1024)
print("[SERVER BANNER>]",data)
break
except:
s.close()
length = length + 1
if length == 6:
print("[ERROR>]Failed to connect")
break
else:
continue
def get_info():
try:
info = socket.gethostbyaddr(siteORip)
print("[SERVER INFO:([HOST,ADDITIONAL HOST,IP ADDRESS])>]",info)
except:
print("[ERROR>]Unknown error retrieving server info")
def get_ip():
try:
IP = socket.gethostbyname(siteORip)
except:
IP = socket.gethostbyname(siteORip)
try:
print("[IP ADDRESS>]",IP)
except:
print("[ERROR>]Unable to access IP")
VER = 1
s.close()
def ports_open():
while True:
try:
for x in ports_test:
s = socket.socket()
socket.setdefaulttimeout(5)
s.connect((siteORip,x))
print("[TESTING...>]Port",x,"is open")
s.close()
except:
print("[TESTING...>]Port",x,"is closed")
s.close()
leng = leng + 1
if leng == 6:
break
else:
continue
def main():
ports_open()
get_ban()
get_info()
get_ip()
print("[<PROGRAM CONNECTION ENDED>]")
main()
wait = input("")