meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
courses:ct30a6000:exer_udp [2012/01/09 13:17]
hevi [Transport programming with python \ CSPA]
courses:ct30a6000:exer_udp [2012/01/09 14:53] (current)
hevi
Line 26: Line 26:
 Create sum service with UDP by python. Sum server takes two separate messages containing number and 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. returns sum of those numbers as a results message back to the client.
 +
 +
 +===== Task A process to point =====
 +**not yet working**
 +
 +main_server.py
 +<​code>​
 +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()
 +</​code>​
 +
 +main_client.py
 +<​code>​
 +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()
 +</​code>​
 +
 +
 +
 +