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?

2 Answers2

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)
drez90
  • 814
  • 5
  • 17
-1

Change the import to

import socket

Then, if you only do

clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM)

like d_rez90 suggests, you'll get something like

TypeError: 'module' object is not callable

Since socket is a module, containing the class socket, you actually need to do

clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145