-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-udp.py
42 lines (33 loc) · 1.23 KB
/
client-udp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# Python Network Programming Cookbook, Second Edition -- Chapter - 1
# This program is optimized for Python 2.7.12 and Python 3.5.2.
# It may run on any other version with/without modifications.
import socket
import sys
import argparse
host = 'localhost'
data_payload = 2048
def echo_client(port):
""" A simple echo client """
# Create a UDP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (host, port)
print ("Connecting to %s port %s" % server_address)
message = 'This is the message. It will be repeated.'
try:
# Send data
message = "Test message. This will be echoed"
print ("Sending %s" % message)
sent = sock.sendto(message.encode('utf-8'), server_address)
# Receive response
data, server = sock.recvfrom(data_payload)
print ("received %s" % data)
finally:
print ("Closing connection to the server")
sock.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Socket Server Example')
parser.add_argument('--port', action="store", dest="port", type=int, required=True)
given_args = parser.parse_args()
port = given_args.port
echo_client(port)