Python 3.4.2 - 套接字模块不可调用

时间:2015-04-25 12:02:06

标签: python sockets typeerror

from socket import *
import _thread
from My_TCP_callable import *

IP_list = []
port_list = []

def IP_find(IP_list):
    IPtarget = input("Enter host to scan: ")

    IP_list = []
    print ('Starting scan on host ', IPtarget)

    for ip in range(1, 256):
        s = socket(AF_INET, SOCK_STREAM)

        addr = IPtarget + str(ip)
        result = s.connect_ex((addr, 135))
        print (ip)

        if(result == 0):
            print ('IP %d: OPEN' % (addr,))
            IP_list.append(str(addr))
        s.close()
    print("Open ports: %s" % (port_list))
    sending_socket(port_list)
    return

def port_find(port_list):
    if __name__ == '__main__':

        target = input('Enter host to scan: ')

        possible_list = []
        port_list = []

        typ = int(input(" 1.Use standard ports \n 2.Set a range of points \n     3+.Set a number of specific points "))

        if typ == 1:
            possible_list = [20, 1025]
        else:
            for number in range(typ):
                v = int(input("Set a port point: "))
                possible_list.append(v)

        if typ >= 3:
            x = (possible_list)
        else:
            x = range(min(possible_list), max(possible_list))

        targetIP = gethostbyname(target)
        print ('Starting scan on host ', targetIP)

        #scan reserved ports
        for i in x:
            s = socket(AF_INET, SOCK_STREAM)

            result = s.connect_ex((targetIP, i))

            if(result == 0):
                print ('Port %d: OPEN' % (i,))
                port_list.append(str(i))
            s.close()
        print("Open ports: %s" % (port_list))
    return port_list

def sending_socket(port_list):
    send_socket = input("Would you like to send a socket with this? ")
    if send_socket == "yes":

        port = int(input("Which port would you like to search? "))
        message = input("What would you like to send? ")

        My_TCP_callable.connect(targetIP, port, message)
    elif send_socket == "automate":
        message = "Alive"
        for ports in port_list:
            _thread.start_new_thread(connect ,(targetIP, ports, message))
    return

IP_find(IP_list)

每次调用此代码时都会出现错误:

文件" C:\ Users \ as009542 \ Desktop \ python \ Networking \ scanner.py",第81行,

<module>
    IP_find(IP_list)
  File "C:\Users\as009542\Desktop\python\Networking\scanner.py", line 15, in IP_find
    s = socket(AF_INET, SOCK_STREAM)
TypeError: 'module' object is not callable

我已经检查过并在其他程序中使用过这个但我无法弄清楚套接字无法创建的原因。 这段代码并不完整或完整,但是我无法继续制作,直到我解决了这个问题,最后一个函数是“发送插件”。正在从名为“My_TCP_callable”的另一个文件中调用。虽然这不会影响我遇到的问题。

1 个答案:

答案 0 :(得分:0)

您可能在模块import socket

中使用My_TCP_callable

因为你使用

from socket import *
from My_TCP_callable import *

导入每个模块的所有内容,并且套接字名称发生冲突(首选项来自My_TCP_callable

我用两个文件test2.py复制了这个行为,其中包含一行import socket和包含

的test.py
from socket import *
from test2 import *
socket()

一种好的编码风格是很少使用from ... import *,而且仅限于专门为其设计的模块。