无法导入套接字模块

Cant import socket module

我在 PyCharm IDE 上编写了以下脚本:

import socket
import time

sock = socket(socket.AF_INET, socket.SOCK_DGRAM)

hexpacket "00 FF"

ip = raw_input('Target IP: ')
port = input('Port: ')
duration = ('Number of seconds to send packets: ')
timeout = time.time() + duration
sent = 0

while True:
    if time.time() > timeout:
        break
    else:
        pass
    sock.sendto(hexpacket,(ip,port))
    sent = sent +1
    print "Send %s packet to %s through ports %s"(send, ip, port)

我从控制台得到输出:

  TypeError: 'module' object is not callable

我曾尝试将 "import socket" 语句更改为 "from socket import socket" 和 "from socket import *",但都没有帮助。

我也尝试使用 "pip install socket" 但我得到 "could not find any downloads that satisfy the requirement socket"。

知道如何解决这个简单的问题吗?我认为套接字是每个 python 下载都附带的基本模块。

感谢您的回答..!

sock = socket(socket.AF_INET, socket.SOCK_DGRAM)

你直接使用socket object 这是错误的,它正在抛出错误 TypeError: 'module' object is not callable

试试这个::

client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)