Socket has no attribute AF_INET?

user3476554 picture user3476554 · Apr 3, 2014 · Viewed 15.8k times · Source

I am trying to do a socket programming assignment from one of my textbooks.. UDP connection..

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)

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()

Why am I getting this error when running the Client??

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'

I looked on this forum and someone had a similar problem, but their problem was that they had their own socket.py file they were importing. I do not, I am using the standard Python one...

Also this is a sidenote....

Why can't I allow access to Python on Windows 8, it is currently blocked for some reason and I am the administrator and only account on this computer and when I click Change Settings in Allow Program through Firewall (which is not greyed out so it proves I am admin), nothing appears.. Any help please?

Answer

d_rez90 picture d_rez90 · Apr 3, 2014
clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)

if you were intending to call AF_INET like this, you should import socket and not from socket import * otherwise just do

clientSocket = socket(AF_INET, SOCK_DGRAM)