1# Copyright (c) 2012 Denis Bilenko. See LICENSE for details.
2"""Send a datagram to localhost:9000 and receive a datagram back.
3
4Usage: python udp_client.py MESSAGE
5
6Make sure you're running a UDP server on port 9001 (see udp_server.py).
7
8There's nothing gevent-specific here.
9"""
10from __future__ import print_function
11import sys
12from gevent import socket
13
14address = ('127.0.0.1', 9001)
15message = ' '.join(sys.argv[1:])
16sock = socket.socket(type=socket.SOCK_DGRAM)
17sock.connect(address)
18print('Sending %s bytes to %s:%s' % ((len(message), ) + address))
19sock.send(message.encode())
20data, address = sock.recvfrom(8192)
21print('%s:%s: got %r' % (address + (data, )))
22sock.close()
Next page: Example udp_server.py