728x90
반응형
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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
반응형
'Python' 카테고리의 다른 글
python embed (0) | 2021.10.03 |
---|---|
python embed test (0) | 2021.10.03 |
python input key example (0) | 2021.10.03 |
python 컴포넌트를 pip로 손쉽게 업그레이드 (0) | 2018.12.29 |
Ubuntu 에서 python3와 pip3를 기본(default) python으로 설정하는 방법 (0) | 2018.12.24 |