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

Python 3.4.2 - Socket module is not callable

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

我已经检查并在其他程序中使用了它,但我无法弄清楚为什么无法创建套接字。 这段代码不漂亮也不完整,但是在我解决这个问题之前我无法继续制作它,最后一个函数 'sending_socket' 是从另一个名为 'My_TCP_callable' 的文件中调用的,尽管这不会影响我的问题我有。

您可能在模块 My_TCP_callable

中使用了 import socket

因为你使用

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 *,并且只在专门为其设计的模块上使用。