Jeff Abrahamson on 19 Oct 2004 19:07:03 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] python and sockets, weird error


I'm trying to send a UDP packet in python.

Below is a short code snippet I pulled from the web.  It looks right
to me.  Other examples look basically the same.

When I run it, I get an error, "AF_INET" is not defined.

Anyone see what I might be doing wrong?

BTW, is there a difference between the "import socket" and "from
socket import *" ?  I had thought they were the same thing, pick one.
But I've seen some code on the net recently that does both.

Thanks in advance.

    jeff@asterix:hw2a-jeffa $ ./s.py
    Traceback (most recent call last):
      File "./s.py", line 3, in ?
	import socket
      File "[...path...]/socket.py", line 13, in ?
	UDPSock = socket(AF_INET, SOCK_DGRAM)
    NameError: name 'AF_INET' is not defined
    jeff@asterix:hw2a-jeffa $


Here's the code:

    #!/usr/bin/python

    import socket
    from socket import *


    # Set the socket parameters
    host = "localhost"
    port = 2000
    buf = 1024
    addr = (host,port)

    # Create socket
    UDPSock = socket(AF_INET, SOCK_DGRAM)

    def_msg = "===Enter message to send to server===";
    print "\n",def_msg

    # Send messages
    while (1):
	    data = raw_input('>> ')
	    if not data:
		    break
	    else:
		    if(UDPSock.sendto(data,addr)):
			    print "Sending message '",data,"'.....<done>"

    # Close socket
    UDPSock.close()

-- 
 Jeff

 Jeff Abrahamson  <http://www.purple.com/jeff/>    +1 215/837-2287
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B

 A cool book of games, highly worth checking out:
 http://www.amazon.com/exec/obidos/ASIN/1931686963/purple-20

Attachment: signature.asc
Description: Digital signature