Shammer's Philosophy

My private adversaria

UDP Server by Python

シンプルな実装だが、メモ的な目的で記載。

#!/usr/bin/env python
from time import ctime
import socket
from contextlib import closing

def main():
  host = '192.168.1.10'
  port = 12345
  bufsize = 4096

  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  with closing(sock):
    sock.bind((host, port))
    while True:
      message, client = sock.recvfrom(bufsize)
      print message
      sock.sendto('[%s] %s' % (ctime(), message), client)
  return

if __name__ == '__main__':
  main()