Last active
August 29, 2015 14:27
-
-
Save GameXG/77d9035c35b72856271c to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# utf-8 中文编码 | |
import logging | |
import socket | |
import time | |
import struct | |
import thread | |
SIZE = 1400 | |
address = ('www.chenwang.net', 31500) | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.settimeout(2) | |
def send_loop(sock,count): | |
for i in range(count): | |
try: | |
t = int(time.time()*1000) | |
data_t = struct.pack('!qq',t,i) | |
data = data_t + '0'*(SIZE - len(data_t)) | |
s.sendto(data, address) | |
except: | |
logging.exception('send_loop') | |
thread.start_new(send_loop ,(s,99999999,)) | |
old_i=None | |
while True: | |
try: | |
data, addr = s.recvfrom(2048) | |
t,i = struct.unpack_from('!qq',data,0) | |
if old_i == None: | |
old_i = i | |
else: | |
ping = int(time.time()*1000) - t | |
if i == old_i+1: | |
print u'ping %s size:%s ping_time:%sms time:%s'%(i,len(data),ping,time.time()) | |
elif i > old_i+1: | |
for a in range(old_i+1,i): | |
print u'ping %s 丢包'% a | |
elif i == old_i: | |
print u'ping %s 重复包 size:%s ping_time:%sms time:%s'%(i,len(data),ping,time.time()) | |
elif i < old_i: | |
print u'ping %s 错顺包 size:%s ping_time:%sms time:%s'%(i,len(data),ping,time.time()) | |
old_i=i | |
except socket.timeout: | |
print u'ping 超时 time:%s'%time.time() | |
except: | |
logging.exception(u'ping err time:%s' %time.time()) | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment