Python 客户端套接字的 AttributeError
我在运行程序的客户端时遇到了一个属性错误,我很确定我做的一切都是正确的,但显然不是。
这是我的代码:
from socket import *
serverName = 'hostname'
serverPort = 12000
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
message = raw_input('Input lowercase sentence:')
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
这是我收到的错误信息:
Traceback (most recent call last):
File "UDPClient.py", line 4, in <module>
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
AttributeError: type object '_socketobject' has no attribute 'socket'
编辑:
Traceback (most recent call last):
File "UDPClient.py", line 6, in <module>
clientSocket.sendto(message,(serverName,serverPort))
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
1 个回答
2
你理解错了。因为你用了 import *
,所以只需要用 AF_INET
和 SOCK_DGRAM
就可以了。
>>> from socket import *
>>> clientSocket = socket(AF_INET, SOCK_DGRAM)
在我的电脑上用 Py3.4 测试过了。