1998-01-29 00:04:04 -04:00
|
|
|
# Echo server demo using Unix sockets (handles one connection only)
|
|
|
|
# Piet van Oostrum
|
2006-06-03 20:59:36 -03:00
|
|
|
|
2003-08-01 11:20:02 -03:00
|
|
|
import os
|
1998-01-28 12:54:00 -04:00
|
|
|
from socket import *
|
2006-06-03 20:59:36 -03:00
|
|
|
|
|
|
|
FILE = 'unix-socket'
|
1998-01-28 12:54:00 -04:00
|
|
|
s = socket(AF_UNIX, SOCK_STREAM)
|
|
|
|
s.bind(FILE)
|
2006-06-03 20:59:36 -03:00
|
|
|
|
1998-01-28 12:54:00 -04:00
|
|
|
print 'Sock name is: ['+s.getsockname()+']'
|
2006-06-03 20:59:36 -03:00
|
|
|
|
|
|
|
# Wait for a connection
|
1998-01-28 12:54:00 -04:00
|
|
|
s.listen(1)
|
|
|
|
conn, addr = s.accept()
|
2006-06-03 20:59:36 -03:00
|
|
|
|
|
|
|
while True:
|
1998-01-28 12:54:00 -04:00
|
|
|
data = conn.recv(1024)
|
2006-06-03 20:59:36 -03:00
|
|
|
if not data:
|
|
|
|
break
|
1998-01-28 12:54:00 -04:00
|
|
|
conn.send(data)
|
2006-06-03 20:59:36 -03:00
|
|
|
|
1998-01-28 12:54:00 -04:00
|
|
|
conn.close()
|
2003-08-01 11:20:02 -03:00
|
|
|
os.unlink(FILE)
|