Bidirectional communication over unix socket (named pipe)
Raúl Gómez C.
nachogomez at gmail.com
Wed Oct 17 17:20:06 EDT 2007
BTW: This is the original post:
Hi, I feel like I should apologize in advance because I must be missing
something fairly basic and fundamental here. I don't have a book on
Python network programming (yet) and I haven't been able to find an
answer on the net so far.
I am trying to create a pair of programs, one (the client) will be
short-lived (fairly) and the second (server) will act as a cache for
the client. Both will run on the same machine, so I think a simple file
socket is the easiest and most reliable method.
The problem I have is that the client can send to the server, but the
server can't send back to the client because it gets this error:
socket.error: (107, 'Transport endpoint is not connected')
This is despite the client waiting on a socket.recv() statement. Is
the client really not connected, or is the server unaware of the
connection? And how do I fix this?
I was able to get this working by switching to AF_INET, but that is not
what I want.
Unix sockets are bidirectional, correct? I have never programmed one,
but I know that programs like clamav use a socket to receive an email
to scan and return the result.
Any help would be greatly appreciated!
Jeff
*** server.py ***
#!/usr/bin/python
import socket
import os, os.path
import time
if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")
server = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
server.bind("/tmp/mysock")
while True:
datagram = server.recv(1024)
if not datagram:
break
print datagram
# the preceeding works, and I see the TEST TEST TEST statement the
client sent
time.sleep(2)
# it dies on the next statement.
server.send("Thank you\n")
server.close()
if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock")
*** client.py: ***
#!/usr/bin/python
import socket
client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
client.connect("/tmp/mysock")
TX = "TEST TEST TEST"
TX_sent = client.send(TX)
if TX_sent <> len(TX): print "TX incomplete"
while True:
print "Waiting..."
datagram = client.recv(1024)
# the client sits here forever, I see the "waiting appear" but
it doesn't advance beyond
# the recv statement.
if not datagram:
break
print "Received: ",datagram
client.close()
On 10/17/07, Raúl Gómez C. <nachogomez at gmail.com> wrote:
>
> Hi Jeffrey,
>
> I've been reading the Python mailing list and I've found a post of you
> about Unix socket with Python, you've found the answer to you're problem by
> your self, but I wonder if you still has the working code and if you would
> share it?
>
> Thanks!...
>
> Raul
>
>
> On *Wed Mar 8 18:11:11 CET 2006, **J Rice* rice.jeffrey at gmail.com<python-list%40python.org?Subject=Bidirectional%20communication%20over%20unix%20socket%20%28named%20pipe%29&In-Reply-To=1141836504.118212.173060%40z34g2000cwc.googlegroups.com>wrote:
>
> > OK, never fails that I find a solution once I post a problem.
> > If I use a stream rather than a datagram, it seems to work fine.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071017/22f20781/attachment.html>
More information about the Python-list
mailing list