I am writing some codes to scan ports with Python. However, the error keeps showing up, I am not sure how to fix.
Error is listed below:
[+] Scan Results for: ubuntu
[-] 80/tcp closed
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "portscanner.py", line 23, in connScan
connSkt.close()
UnboundLocalError: local variable 'connSkt' referenced before assignment
I did some research and appeared that I did not declare the variable properly, but I double checked the code and could not see what is wrong with it. Here is the tutorial I followed:
https://www.youtube.com/watch?v=IOvvjNi8OdU&list=PL1A2CSdiySGLtKwqBnqj9BON6QQjWkP4n&index=5
def connScan(tgtHost,tgtPort):
try:
connSkt = socket(AD_INET, SOCKET_STREAM)
connSkt.connect((tgtHost,tgtPort))
connSkt.send('Hello\r\n')
results = connSkt.recv(100)
screenLock.acquire()
print "[+] "+ str(tgtPort) + "/tcp open"
except:
screenLock.acquire()
print "[-] "+ str(tgtPort) + "/tcp closed"
finally:
screenLock.release()
connSkt.close()`
any advise would be highly appreciated. Thanks in advance!