socket上的AttributeError

时间:2012-11-26 10:07:48

标签: python python-3.x python-2.7

from socket import socket, AF_INET, SOCK_DGRAM
import struct
import sys
import time

TIME1970 = 2208988800L  

client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
data = '\x1b' + 47 * '\0'
client.sendto( data, ( sys.argv[1], 123 ))
data, address = client.recvfrom( 1024 )
if data:
    print 'Response received from:', address
    t = struct.unpack( '!12I', data )[10]
    t = TIME1970
    print '\tTime=%s' % time.ctime(t)

当我尝试执行此脚本时,我收到以下错误;请帮帮我:

File "C:\Users\Wolverine\Desktop\Apcog-python prog\test.py", line 8, in ?
    client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
AttributeError: socket
Traceback (innermost last):
  File "C:\Program Files (x86)\Python\Pythonwin\pywin\framework\scriptutils.py", line 301, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Wolverine\Desktop\Apcog-python prog\test.py", line 8, in ?
    client = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
AttributeError: socket

1 个答案:

答案 0 :(得分:1)

您将socketfrom socket import socket一起导入,以及该模块中的其他元素,因此您无需使用模块名称来引用它:

client = socket(AF_INET, SOCK_DGRAM)

或者,将导入行更改为:

import socket

并通过属性访问继续引用该模块中的项目。