AF_UNIX socket not supported

G

gracemia

Hello !

I'm trying to develop with AF_UNIX socket type in python (2.5) but
python says that AF_UNIX is not supported
How can I do for work with AF_UNIX sockets?

Thank you
 
P

Peter Otten

gracemia said:
File "prueba.py", line 4, in <module>
sock = socket(AF_UNIX, SOCK_STREAM)
NameError: name 'AF_UNIX' is not defined

code:

import socket
sock = socket(AF_UNIX, SOCK_STREAM)

You need to qualify the variable names with the module name:

import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

Have you worked through the tutorial

http://docs.python.org/tutorial/index.html

already?
 
B

Benjamin Kaplan

This is the simple code:

I think you're having a bit of trouble with Python's namespaces. doing
"import socket" does not give you all of the socket module's stuff in
your current namespace. For that, you do "from socket import *", but
that's bad form because you can end up clobbering a lot of stuff.
There's two ways to do it:


import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

from socket import socket, AF_UNIX, SOCK_STREAM
sock = socket(AF_UNIX, SOCK_STREAM)
 
C

Chris Rebert

 File "prueba.py", line 4, in <module>
   sock = socket(AF_UNIX, SOCK_STREAM)
NameError: name 'AF_UNIX' is not defined

code:

import socket
sock = socket(AF_UNIX, SOCK_STREAM)

You need to qualify all those names. Normal `import` doesn't dump all
the importee's names into the importer's namespace; it only imports
the name of the module itself. Thus:

import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

I would recommend (re-)reading the relevant part of the tutorial:
http://docs.python.org/tutorial/modules.html

Cheers,
Chris
 
G

gracemia

You need to qualify all those names. Normal `import` doesn't dump all
the importee's names into the importer's namespace; it only imports
the name of the module itself. Thus:

import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

I would recommend (re-)reading the relevant part of the tutorial:http://docs.python.org/tutorial/modules.html

Cheers,
Chris

Sorry about my reply numbers, my browser blocked.

Thank you very much. It works for me.
 
G

Grant Edwards

You need to qualify all those names. Normal `import` doesn't dump all
the importee's names into the importer's namespace;

Unfortunately, it's somewhat common for example code in various places
to show the above usage (where somebody did a "from <whatever> import
*). IIRC there used to be examples like that even in the official
docs, but I haven't seen any there in a while.

But examples that look like that are still pretty easy to stumble
across using Google.

I'm guessing he copied the code from a "tutorial" example like this:

http://www.evolt.org/node/60276
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top