Skip to content

Instantly share code, notes, and snippets.

@ra1u

ra1u/client.py Secret

Created July 19, 2012 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ra1u/3144857 to your computer and use it in GitHub Desktop.
Save ra1u/3144857 to your computer and use it in GitHub Desktop.
updating 1000 udp servers
#!/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()
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