Socket has no attribute AF_INET?
Asked Answered
O

1

8

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?

Omeromero answered 3/4, 2014 at 16:29 Comment(1)
Check how you imported socket and how you are accessing it.Horal
N
12
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)
Nessus answered 3/4, 2014 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.