diff options
author | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-30 21:33:07 (GMT) |
commit | 7fafbc95c0c963438197c9a43fe893c4ea6fe759 (patch) | |
tree | 5c9ceda4bdc5260236a230554b9ed56b8c0cdbd3 /Demo/sockets/udpecho.py | |
parent | 6f17e2df29a865a29447531e89fb22be710e382d (diff) | |
download | cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.zip cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.gz cpython-7fafbc95c0c963438197c9a43fe893c4ea6fe759.tar.bz2 |
More cleanup: Move some demos into a dedicated Tools/demo dir, move 2to3 demo to Tools, and remove all the other Demo content.
Diffstat (limited to 'Demo/sockets/udpecho.py')
-rwxr-xr-x | Demo/sockets/udpecho.py | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/Demo/sockets/udpecho.py b/Demo/sockets/udpecho.py deleted file mode 100755 index 6983a1f..0000000 --- a/Demo/sockets/udpecho.py +++ /dev/null @@ -1,64 +0,0 @@ -#! /usr/bin/env python3 - -# Client and server for udp (datagram) echo. -# -# Usage: udpecho -s [port] (to start a server) -# or: udpecho -c host [port] <file (client) - -import sys -from socket import * - -ECHO_PORT = 50000 + 7 -BUFSIZE = 1024 - -def main(): - if len(sys.argv) < 2: - usage() - if sys.argv[1] == '-s': - server() - elif sys.argv[1] == '-c': - client() - else: - usage() - -def usage(): - sys.stdout = sys.stderr - print('Usage: udpecho -s [port] (server)') - print('or: udpecho -c host [port] <file (client)') - sys.exit(2) - -def server(): - if len(sys.argv) > 2: - port = eval(sys.argv[2]) - else: - port = ECHO_PORT - s = socket(AF_INET, SOCK_DGRAM) - s.bind(('', port)) - print('udp echo server ready') - while 1: - data, addr = s.recvfrom(BUFSIZE) - print('server received %r from %r' % (data, addr)) - s.sendto(data, addr) - -def client(): - if len(sys.argv) < 3: - usage() - host = sys.argv[2] - if len(sys.argv) > 3: - port = eval(sys.argv[3]) - else: - port = ECHO_PORT - addr = host, port - s = socket(AF_INET, SOCK_DGRAM) - s.bind(('', 0)) - print('udp echo client ready, reading stdin') - while 1: - line = sys.stdin.readline() - if not line: - break - print('addr = ', addr) - s.sendto(bytes(line, 'ascii'), addr) - data, fromaddr = s.recvfrom(BUFSIZE) - print('client received %r from %r' % (data, fromaddr)) - -main() |