Guido van Rossum | 44f5c75 | 1998-01-29 04:04:04 +0000 | [diff] [blame] | 1 | # Echo server demo using Unix sockets (handles one connection only) |
2 | # Piet van Oostrum | ||||
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 3 | |
Skip Montanaro | 9463792 | 2003-08-01 14:20:02 +0000 | [diff] [blame] | 4 | import os |
Guido van Rossum | dd918a9 | 1998-01-28 16:54:00 +0000 | [diff] [blame] | 5 | from socket import * |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 6 | |
7 | FILE = 'unix-socket' | ||||
Guido van Rossum | dd918a9 | 1998-01-28 16:54:00 +0000 | [diff] [blame] | 8 | s = socket(AF_UNIX, SOCK_STREAM) |
9 | s.bind(FILE) | ||||
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 10 | |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 11 | print('Sock name is: ['+s.getsockname()+']') |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 12 | |
13 | # Wait for a connection | ||||
Guido van Rossum | dd918a9 | 1998-01-28 16:54:00 +0000 | [diff] [blame] | 14 | s.listen(1) |
15 | conn, addr = s.accept() | ||||
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 16 | |
17 | while True: | ||||
Guido van Rossum | dd918a9 | 1998-01-28 16:54:00 +0000 | [diff] [blame] | 18 | data = conn.recv(1024) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 19 | if not data: |
20 | break | ||||
Guido van Rossum | dd918a9 | 1998-01-28 16:54:00 +0000 | [diff] [blame] | 21 | conn.send(data) |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | dd918a9 | 1998-01-28 16:54:00 +0000 | [diff] [blame] | 23 | conn.close() |
Skip Montanaro | 9463792 | 2003-08-01 14:20:02 +0000 | [diff] [blame] | 24 | os.unlink(FILE) |