-
-
Save ra1u/3144857 to your computer and use it in GitHub Desktop.
updating 1000 udp servers
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/env python | |
# Copyright (c) Twisted Matrix Laboratories. | |
# See LICENSE for details. | |
from twisted.internet.protocol import DatagramProtocol | |
from twisted.internet import reactor | |
_nclients = 0 | |
#_data = [] | |
class EchoClientDatagramProtocol(DatagramProtocol): | |
def __init__(self,datasource): | |
self.datasource = datasource | |
def startProtocol(self): | |
self.transport.connect('127.0.0.1', 62010) | |
self.sendDatagram() | |
def sendDatagram(self): | |
try: | |
while 1: | |
datagram = next(self.datasource).strip() | |
if len(datagram) > 0: | |
break | |
self.transport.write(datagram) | |
except StopIteration: | |
global _nclients | |
_nclients -= 1 | |
if _nclients == 0: | |
print("finnised") | |
reactor.stop() | |
def datagramReceived(self, datagram, host): | |
s = str(datagram).strip() | |
if s == 'OK': | |
self.sendDatagram() | |
else: | |
print s | |
def main(): | |
#protocol = EchoClientDatagramProtocol() | |
#t = reactor.listenUDP(0, protocol) | |
global _nclients | |
_nclients = 200 #upto 500, but 1000 does not | |
with open("sx.sx","r") as f: | |
ds = [line for line in f] | |
p = (EchoClientDatagramProtocol(iter(ds)) for _ in xrange(_nclients)) | |
r = [reactor.listenUDP(0, i) for i in p] | |
reactor.run() | |
if __name__ == '__main__': | |
main() |
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
from twisted.internet.protocol import DatagramProtocol | |
from twisted.internet import reactor | |
class Echo(DatagramProtocol): | |
def datagramReceived(self, data, (host, port)): | |
#print "received %r from %s:%d" % (data, host, port) | |
if host == "127.0.0.1": | |
self.transport.write("OK", (host, port)) | |
reactor.listenUDP(62010, Echo()) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment