meta data for this page

Transport programming with python \ CSPA

with UDP

Socket module documentation

BSD Socket interface

  • Common framework for networking usually on unix based systems
  • Many protocols, network addressing and transport, can be used through socket framework, TCP, UDP, local, ..
  • Forces in some degree into client - server model
  • Python interface is modeled along C library interface
  • More about sockets from Network Programming course
  • Python version 2 uses str type for data management, python 3 uses buffer type

Task A

Create echo service with UDP by python. Echo server replies a message content back to the client.

Task B

Create sum service with UDP by python. Sum server takes two separate messages containing number and returns sum of those numbers as a results message back to the client.

Task A process to point

not yet working

main_server.py

import socket
import logging 
log = logging.getLogger(__name__)

echo_host = "localhost"
echo_port_server = 23456

# echo service
def run():
  log.debug("server running ..")
  s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  s.bind( (echo_host,echo_port_server) )
  # loop
  while True:
    msg, sender = s.recvfrom(4096) 
    log.debug("msg: " + msg)
    log.debug("sender " + sender[0])
    s.sendto(msg, (sender[0], echo_port_server) )
  s.close()
  

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()

main_client.py

import socket
import logging 
log = logging.getLogger(__name__)

echo_host = "localhost"
echo_port_client = 23456 + 1

def run():
  log.debug("client running ..")
  s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
  ## send and receive a message 
  send_msg = "ECHO MESSAGE"
  log.debug("Sending " + send_msg)
  s.sendto(send_msg, (echo_host, echo_port_client) )
  recv_msg = s.recv(4096)
  log.debug("Received " + recv_msg) 
  s.close()

if __name__ == '__main__':
  logging.basicConfig(level=logging.DEBUG)
  run()