[Tutor] Udp socket questio

Phil phil_lor at bigpond.com
Tue Mar 26 12:30:28 CET 2013


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'

-- 
Regards,
Phil


More information about the Tutor mailing list