套接字没有属性AF_INET?

use*_*554 6 python sockets firewall udpclient

我正在尝试从我的一本教科书中进行套接字编程分配.. UDP连接..

UDPServer.py

from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print('The server is ready to receive:')
while 1:
    message, clientAddress = serverSocket.recvfrom(2048)
    modifiedMessage = message.upper()
    serverSocket.sendto(modifiedMessage, clientAddress)
Run Code Online (Sandbox Code Playgroud)

UDPClient.py

from socket import *
serverName = 'localhost'
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()
Run Code Online (Sandbox Code Playgroud)

为什么我在运行客户端时遇到此错误?

Traceback (most recent call last):
  File "UDPClient.py", line 4, in <module>
    clientSocket = (socket.AF_INET, socket.SOCK_DGRAM)
AttributeError: type object 'socket' has no attribute 'AF_INET'
Run Code Online (Sandbox Code Playgroud)

我查看了这个论坛,有人遇到了类似的问题,但他们的问题是他们有自己的socket.py文件导入.我没有,我正在使用标准的Python ...

这也是一个旁注......

为什么我不允许在Windows 8上访问Python,它目前因某些原因被阻止,我是这台计算机上的管理员和唯一帐户,当我点击允许程序通过防火墙更改设置时(这不是灰色的,所以它证明我是管理员),什么都没有出现..请帮忙吗?

d_r*_*z90 9

clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)
Run Code Online (Sandbox Code Playgroud)

如果您打算像这样打电话给AF_INET,那么您应该import socket而不是from socket import *其他方式

clientSocket = socket(AF_INET, SOCK_DGRAM)
Run Code Online (Sandbox Code Playgroud)