Created
October 12, 2013 03:28
-
-
Save istrike/6945452 to your computer and use it in GitHub Desktop.
用udp 接收多条数据会出现instence,导至程序出错
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
#!/bin/env python | |
from twisted.internet.protocol import DatagramProtocol | |
from twisted.internet import reactor | |
from twisted.internet import defer | |
from twisted.protocols.policies import TimeoutMixin | |
from optparse import OptionParser | |
import gis_test_case as case | |
import simplejson | |
import sys | |
import time | |
usage = "usage : %prog [options] arg1" | |
parser = OptionParser(usage = usage) | |
parser.add_option("-c", "--case", dest="test_case", help="which test case to use") | |
parser.add_option("-p", "--parallel", dest="parallel", | |
default = 1, help="how much parallel data transfer,default = 1") | |
(options, args) = parser.parse_args() | |
if len(sys.argv) == 1: | |
print usage | |
sys.exit(1) | |
class gis_protocol(DatagramProtocol,TimeoutMixin): | |
def __init__(self,deferred): | |
self.timeout=3 | |
self.host = "192.168.0.186" | |
self.port = 12300 | |
self.recv_data=[] | |
self.deferred = deferred | |
def startProtocol(self): | |
self.setTimeout(self.timeout) | |
self.transport.connect(self.host,self.port) | |
print "we can send to %s now" % str((self.host, self.port)) | |
send_data = case.gen(case_id=options.test_case)["query"] | |
print simplejson.dumps(send_data) | |
self.transport.write(simplejson.dumps(send_data)) | |
def datagramReceived(self,data, (host, port)): | |
self.resetTimeout() | |
self.recv_data.append(simplejson.loads(data)) | |
def connectionRefused(self): | |
print "no one listen check the internet" | |
reactor.stop() | |
def timeoutConnection(self): | |
#if self.deferred is not None: | |
#d, self.deferred = self.deferred, None | |
self.deferred.callback(self.recv_data) | |
def time_out(recv_data): | |
print "time out" | |
return recv_data | |
def cmp_result(recv_data): | |
result = case.gen(case_id=options.test_case) | |
if recv_data == None: | |
result = "error" | |
return recv_data,result | |
if result.has_key("error") and recv_data[0].has_key("error"): | |
print "double error OK" | |
reason = "success" | |
return recv_data,reason | |
if result.has_key("success") and recv_data[0].has_key("error") \ | |
or result['success'].sort() != recv_data: | |
print "one is error" | |
result = "error" | |
return recv_data,result | |
if result.has_key("success") and result['success'] == recv_data : | |
print "OK" | |
result = "success" | |
return recv_data,result | |
if result.has_key("error") and recv_data[0].has_key("result"): | |
print "recv_data should error" | |
result = "error" | |
return recv_data,result | |
def write_file(recv_data): | |
print type(recv_data) | |
print recv_data[1] | |
f = open("log","a+") | |
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) | |
f.write("%s %s: %s\n" %(localtime,options.test_case,recv_data[1])) | |
if recv_data[1] == "error": | |
f.write(str(recv_data[0])+"\n") | |
f.close() | |
#reactor.stop() | |
errs = [] | |
def err_write(err): | |
errs.append(err) | |
return errs | |
dl = [] | |
start_time = time.time() | |
for i in range(int(options.parallel)): | |
d = defer.Deferred() | |
protocol = gis_protocol(d) | |
reactor.listenUDP(0,protocol) | |
d.addCallback(time_out) | |
d.addBoth(cmp_result) | |
d.addBoth(write_file) | |
dl.append(d) | |
dlist = defer.DeferredList(dl) | |
end_time = time.time() | |
use_time = end_time - start_time | |
print "use time : %s" %use_time | |
print errs | |
dlist.addCallback(lambda r:reactor.stop()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment