Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 22825e8 | 1991-07-01 18:32:32 +0000 | [diff] [blame] | 2 | |
| 3 | # Client and server for udp (datagram) echo. |
| 4 | # |
| 5 | # Usage: udpecho -s [port] (to start a server) |
| 6 | # or: udpecho -c host [port] <file (client) |
| 7 | |
| 8 | import sys |
| 9 | from socket import * |
| 10 | |
| 11 | ECHO_PORT = 50000 + 7 |
| 12 | BUFSIZE = 1024 |
| 13 | |
| 14 | def main(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 15 | if len(sys.argv) < 2: |
| 16 | usage() |
| 17 | if sys.argv[1] == '-s': |
| 18 | server() |
| 19 | elif sys.argv[1] == '-c': |
| 20 | client() |
| 21 | else: |
| 22 | usage() |
Guido van Rossum | 22825e8 | 1991-07-01 18:32:32 +0000 | [diff] [blame] | 23 | |
| 24 | def usage(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 25 | sys.stdout = sys.stderr |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 26 | print('Usage: udpecho -s [port] (server)') |
| 27 | print('or: udpecho -c host [port] <file (client)') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 28 | sys.exit(2) |
Guido van Rossum | 22825e8 | 1991-07-01 18:32:32 +0000 | [diff] [blame] | 29 | |
| 30 | def server(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 31 | if len(sys.argv) > 2: |
| 32 | port = eval(sys.argv[2]) |
| 33 | else: |
| 34 | port = ECHO_PORT |
| 35 | s = socket(AF_INET, SOCK_DGRAM) |
| 36 | s.bind(('', port)) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 37 | print('udp echo server ready') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 38 | while 1: |
| 39 | data, addr = s.recvfrom(BUFSIZE) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 40 | print('server received %r from %r' % (data, addr)) |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 41 | s.sendto(data, addr) |
Guido van Rossum | 22825e8 | 1991-07-01 18:32:32 +0000 | [diff] [blame] | 42 | |
| 43 | def client(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 44 | if len(sys.argv) < 3: |
| 45 | usage() |
| 46 | host = sys.argv[2] |
| 47 | if len(sys.argv) > 3: |
| 48 | port = eval(sys.argv[3]) |
| 49 | else: |
| 50 | port = ECHO_PORT |
| 51 | addr = host, port |
| 52 | s = socket(AF_INET, SOCK_DGRAM) |
| 53 | s.bind(('', 0)) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 54 | print('udp echo client ready, reading stdin') |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 55 | while 1: |
| 56 | line = sys.stdin.readline() |
| 57 | if not line: |
| 58 | break |
| 59 | s.sendto(line, addr) |
| 60 | data, fromaddr = s.recvfrom(BUFSIZE) |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 61 | print('client received %r from %r' % (data, fromaddr)) |
Guido van Rossum | 22825e8 | 1991-07-01 18:32:32 +0000 | [diff] [blame] | 62 | |
| 63 | main() |