728x90
반응형
# -*- coding: euc-kr -*-
# testUDPServer.py
#!/usr/bin/python
# 버전 : python 2.5.x , 또는 그 이상
#-------------------------------------------------------------------
import socket # Low-level networking interface
import array # Efficient arrays of numeric values
import time # Time access and conversions
from time import gmtime
from time import strftime
import select # Waiting for I/O completion
import thread
import threading
import errno # Standard errno system symbols
import unittest # Unit testing framework
from test import test_support # Regression tests package for Python / Utility functions for tests
#-------------------------------------------------------------------
def testUDPServer():
# UDP 소켓 만들기
s = socket.socket( socket.AF_INET, socket.SOCK_DGRAM )
# 소켓 옵션 설정
# SO_REUSEADDR : 중복 주소/포트 허용
s.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
port = 37999
try:
s.bind( ("", port) ) # UDP 서버 생성
print "Waiting on port:", port, "..."
while True: # 무한 루프
rBuf = array.array( 'b', ' '*1024 ) # 1024 바이트 바이트 배열
nByte, rAddr = s.recvfrom_into( rBuf, 1024 ) # 수신
tP = time.strftime( "%a, %d %b %Y %H:%M:%S +0000", time.localtime() ) # 현재 시간
print " Time : ", tP, " Recevied Size : ", nByte, " Addressee : ", rAddr # 수신 크기, 주소
# print " Time : ", time.clock(), " Recevied Size : ", nByte, " Addressee : ", rAddr # 보다 정밀한 시간
except socket.error, (err, msg):
self.assertEqual( err, errno.EADDRINUSE )
#-------------------------------------------------------------------
# execute
# testUDPServer()
#-------------------------------------------------------------------
728x90
반응형

+ Recent posts