alexb7711 / SIG

Create sockets for specified data types based on a YAML file for Python, C/C++, and Rust!
GNU General Public License v3.0
1 stars 0 forks source link

Generate Python Interface #1

Open alexb7711 opened 2 months ago

alexb7711 commented 2 months ago
alexb7711 commented 6 days ago

This is a way to test the Python UDP packets. The test can just create the client/server and use the interface to send/receive data packets to/from the testing environment.

#!/usr/bin/env python3
# Foundations of Python Network Programming, Third Edition
# https://github.com/brandon-rhodes/fopnp/blob/m/py3/chapter02/udp_local.py
# UDP client and server on localhost

import argparse, socket
from datetime import datetime

MAX_BYTES = 65535

def server(port):
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  sock.bind(('127.0.0.1', port))
  print('Listening at {}'.format(sock.getsockname()))
  while True:
     data, address = sock.recvfrom(MAX_BYTES)
     text = data.decode('ascii')
     print('The client at {} says {!r}'.format(address, text))
     text = 'Your data was {} bytes long'.format(len(data))
     data = text.encode('ascii')
     sock.sendto(data, address)

def client(port):
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  text = 'The time is {}'.format(datetime.now())
  data = text.encode('ascii')
  sock.sendto(data, ('127.0.0.1', port))
  print('The OS assigned me the address {}'.format(sock.getsockname()))
  data, address = sock.recvfrom(MAX_BYTES) # Danger!
  text = data.decode('ascii')
  print('The server {} replied {!r}'.format(address, text))

if __name__ == '__main__':
  choices = {'client': client, 'server': server}
  parser = argparse.ArgumentParser(description='Send and receive UDP locally')
  parser.add_argument('role', choices=choices, help='which role to play')
  parser.add_argument('-p', metavar='PORT', type=int, 
  default=1060,
  help='UDP port (default 1060)')
  args = parser.parse_args()
  function = choices[args.role]
  function(args.p)