class UDP_SNIFFER(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
while 1:
choice = raw_input("menu> ")
if choice.strip() == "":
continue
if choice.strip() == "exit":
break
if choice.strip() == "start":
print "Start to capture packets..."
self.startCapture()
continue
print "Exiting..."
def startCapture(self):
global captured_list
global start_msginx
global missing_cnt
global expected_msginx
global captured_cnt
# try to capture N packets.
start_time = time.time()
sniff(prn=pkg_callback, filter="udp and port %d" % (UDP_PORT), count=10000)
end_time = time.time()
gap = end_time - start_time
# print result
print "Sniffing is done. Result ( %d / %d ) during %f" % ( missing_cnt, captured_cnt, gap )
if __name__ == "__main__":
upr = UDP_SNIFFER()
upr.start()
upr.join()
exit(0)
속독한 후 알게된 사실.
1. Byte String(문자열)과 유니코드는 다르다.
2. Byte String(문자열)을 읽어 유니코드로 변환하는 과정을 decoding 이라고 한다.
3. 유니코드를 Byte String(문자열)로 변환하는 과정을 encoding 이라고 한다.
4. 파일/소켓/stdin에서 데이터를 읽거나 수신하였다면 decoding 과정을 거쳐 유니코드로 변환하자.
5. 파일/소켓/화면(stdout)으로 데이터를 쓰거나 전송하고싶다면 encoding 과정을 거쳐 Byte String으로 변환하자.