Posts: 15
Threads: 7
Joined: Oct 2016
Oct-17-2016, 05:32 PM
(This post was last modified: Oct-17-2016, 05:44 PM by Yoriz.)
Hello guys, I'm trying to build a simple port scanner, but I constantly got this error : ''str' object is not callable"
I really don't know why I'm getting it, I did not assign a var as str, e.g : str= 'hello'. I tried to review my script quite few times now, I looked the problem up on the web, still nothing. Thank you for your help. (P.S : just had to create a brand new account, cuz the forum moved and I wasn't able to login anymore, or recuperate my password)
from socket import *
def port_scanner(host,port):
socket = ' '
try:
socket = socket(AF_INET,SOCK_STREAM)
socket.connect(host,int(port)) # tried to double the parentheses here, still did not work
result = '[>]'+host+' :is opened on:'+str(port)
print result
except Exception,e:
print e
print '[!]'+host+' is not opened on'+str(port)
def main():
totalchars = 0
totalpoints = 0
listofports = ['80','21','22']
host = gethostbyname('www.xxxx.com')
for char in host:
totalchars = 1+totalchars
if char == '.':
totalpoints = 1+totalpoints
if totalpoints == 3:
hostfixed = host[:totalchars]
for num in range(1,255):
num1 = str(num)
host1 = hostfixed+num1#I've also tried ...+str(num) and it does not work.
for port in listofports:
port_scanner(host1,port)
main()
Posts: 16
Threads: 2
Joined: Oct 2016
Oct-17-2016, 05:36 PM
(This post was last modified: Oct-17-2016, 05:40 PM by Yoriz.)
try
host1 = str(hostfixed)+str(num1)
Posts: 15
Threads: 7
Joined: Oct 2016
(Oct-17-2016, 05:36 PM)demosthenesk Wrote: try
host1 = str(hostfixed)+str(num1)
Did not work, m8 :( thank you anyway :)
Posts: 2,168
Threads: 35
Joined: Sep 2016
Oct-17-2016, 05:47 PM
(This post was last modified: Oct-17-2016, 05:52 PM by Yoriz.)
When posting an error, please posts the full traceback error in error tags.
Temporarily remove/comment out the try/except clause to get the full traceback error to show.
Posts: 15
Threads: 7
Joined: Oct 2016
(Oct-17-2016, 05:47 PM)Yoriz Wrote: When posting an error, please posts the full traceback error in error tags.
Error: 'str' object is not callable
That's the full traceback error :)
Posts: 2,168
Threads: 35
Joined: Sep 2016
That is not the full traceback error, its doesn't show the line number the error occurred, the line that caused the error and what lead up to the error.
That's the result of a catch all exception.
Temporarily remove/comment out the try/except clause to get the full traceback error to show.
Posts: 2,342
Threads: 62
Joined: Sep 2016
That's not a traceback. How are you running your code? We need to know the line number the problem is occurring on.
By the way, the code you posted looks like it'd have an indentation error before being able to have a TypeError (as I suspect you are experiencing; it's hard to know for certain without the traceback).
Posts: 15
Threads: 7
Joined: Oct 2016
Oct-17-2016, 06:15 PM
(This post was last modified: Oct-17-2016, 06:28 PM by peterkl.)
(Oct-17-2016, 05:59 PM)Yoriz Wrote: That is not the full traceback error, its doesn't show the line number the error occurred, the line that caused the error and what lead up to the error.
That's the result of a catch all exception.
Temporarily remove/comment out the try/except clause to get the full traceback error to show.
You're right, sorry for that.
Error: Traceback (most recent call last):
File "Port_scanner.py", line 37, in <module>
main()
File "Port_scanner.py", line 35, in main
port_scanner(host1,port)
File "Port_scanner.py", line 7, in port_scanner
socket = socket(AF_INET,SOCK_STREAM)
TypeError: 'str' object is not callable
(Oct-17-2016, 06:01 PM)micseydel Wrote: That's not a traceback. How are you running your code? We need to know the line number the problem is occurring on.
By the way, the code you posted looks like it'd have an indentation error before being able to have a TypeError (as I suspect you are experiencing; it's hard to know for certain without the traceback).
It got indentation problems while I copied it. I'll fix the code on my post now.
With the traceback I was able to solve the problem my self. Completely forgot to fully checked the traceback and not only the Exception msg.
Since the error was on the line 7 , socket = socket(AF_INET,SOCK_STREAM), i changed it to : socketkl = socket(AF_INET,SOCK_STREAM) and it worked fine. I'm not able to explain why, but I think it's because I was calling the function "socket" from the lib socket twice : here 'socket =' and here ' socket(AF_INET....)' ; thank you for your patient, I will make more attention in indentation next time, as well as posting the traceback and not only the exception msg.
Posts: 2,168
Threads: 35
Joined: Sep 2016
You have over written the imported socket.
from socket import *
def port_scanner(host,port):
socket = ' '
Posts: 15
Threads: 7
Joined: Oct 2016
(Oct-17-2016, 06:30 PM)Yoriz Wrote: You have over written the imported socket.
from socket import *
def port_scanner(host,port):
socket = ' '
Thank you :)
|