[Tutor] Udp socket questio

Dave Angel davea at davea.name
Tue Mar 26 14:15:04 CET 2013


On 03/26/2013 07:30 AM, Phil wrote:
> Thank you for reading this.
>
> I'm a bit out of my depth here. I'm attempting to set up a simple udp
> client.
>
> The example that follows results in an error message and I'm wondering
> if I should be importing a different module. A Google search doesn't
> support that idea.
>
> ''
>      udp socket client
>      Silver Moon
> '''
>
> import socket    #for sockets
> import sys    #for exit
>
> # create dgram udp socket
> try:
>      s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> except socket.error:
>      print 'Failed to create socket'
>      sys.exit()
>
> host = 'localhost';
> port = 8888;
>
> while(1) :
>      msg = raw_input('Enter message to send : ')
>
>      try :
>          #Set the whole string
>          s.sendto(msg, (host, port))
>
>          # receive data from client (data, addr)
>          d = s.recvfrom(1024)
>          reply = d[0]
>          addr = d[1]
>
>          print 'Server reply : ' + reply
>
>      except socket.error, msg:
>          print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
>          sys.exit()
>
>
> Traceback (most recent call last):
>    File "socket2.py", line 6, in <module>
>      import socket       #for sockets
>    File "/home/phil/Python/socket.py", line 7, in <module>
>      s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> AttributeError: 'module' object has no attribute 'AF_INET'
>

Looks to me like you have your own file socket.py  in /home/phil/Python

and that it has the line that's causing the exception.

That file hides the one in the system library, so please rename it to 
something else.


-- 
DaveA


More information about the Tutor mailing list