Question

I am trying to generate some traffic to a server by sending TCP messages to it. For this, I am using a Python script which opens a TCP socket and then sends some data over it. After receiving a reply, the TCP connection gets closed.

Question: I would like to be able to predefine a rate with which the script will be sending the requests to the server, eg: 5 messages per second. However, I do not have a clue how to script this via Python :(.

Anyone an idea how to do this (a short example would be super ! ;) ?

Thanks in advance.

Note: I might need to add an extra difficulty: since the server has to reply, I guess I have to make the script working asynchronously ... That way, I can send the requests out without having to wait for a reply on the previous request...

Was it helpful?

Solution

What you're looking for is an implementation of the token bucket algorithm. It's analogous to a bucket with a fixed capacity, where each consumer can't perform the action until it gets a token, and the bucket is refilled at a fixed rate.

The algorithm is easy to implement, but the link below has an example:

http://code.activestate.com/recipes/511490-implementation-of-the-token-bucket-algorithm/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top