Python Forum
'str' object is not callable
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'str' object is not callable
#1
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()
Reply
#2
try
host1 = str(hostfixed)+str(num1)
Reply
#3
(Oct-17-2016, 05:36 PM)demosthenesk Wrote: try
host1 = str(hostfixed)+str(num1)

Did not work, m8 :( thank you anyway :)
Reply
#4
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.
Reply
#5
(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 :)
Reply
#6
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.
Reply
#7
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).
Reply
#8
(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.
Reply
#9
You have over written the imported socket.
from socket import *

def port_scanner(host,port):
  socket = ' '
Reply
#10
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What is error: "TypeError: 'module' object is not callable" phutran 3 4,107 May-12-2020, 08:07 PM
Last Post: h1dd3n

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020