Sunday, 15 April 2012

networking - Simple way to simulate a slow network in python -


Scenario I have clients with two network connections from the server One connection uses a mobile phone, and the second one wlan connection Is using.

The way I solved it, the server has to listen on two ports. However, the mobile connection should be slower than the wlan connection. The data that is sent is usually just a line of text. I have solved a slow connection problem by allowing the mobile connection to send data in the interval of five seconds, the wlan connection is a second interval.

But is there a better way to do recession? Maybe sending small packets, that is, I need to send more data?

Any ideas on good ways to slow down any connection?

Orjanp

A simple client example with only one connection.

  def client (): import system, time, socket port = 11111 host = '127.0.0.1' buf_size = 1024 Try: mySocket = socket.socket (Socket.AF_INET, socket.SOCK_STREAM) Socket.error, except mySocket.connect (host, port) (except for value, message): if mySocket: mySocket.close () could not open print 'socket: exit' + message sys (1) mySocket.send ('Hello, server') data = mySocket.recv (buf_size) print data time. Sleep (5) mySocket.close () Client ()  

A simple server is listening to a port.

  def server (): import system, OS, socket port = 11111 host = '' backlog = 5 buf_size = 1024 try: listening_socket = socket.socket (socket .AF_INET, sock et.SOCK_STREAM Listening.Sossocotock (socket.SOLCKET, socket.UUUsADRDR, 1) LOSSING_SOCATE.PEND ((host, port) listening_socket.listen (backlog) excluding socket.error, (value, message): if listening_socket: listening_socket off () Print 'socket can not be opened:' + message sys.exit (1) while correct: acceptable_socket, adress = listening_socket.accept () data = accept If the data: accept_socket.send ('hello, and bye.') Approved_socket.close -text "itemprop =" text "> 

In addition to using an external, to imitate the kind of network you are interested in, use a good method to implement the implementation of a socket option.

It involves making socket creation in your function instead of creating a socket module and using it directly for normal operation, you will pass in the actual socket type, but when you have different adverse network If you want to test the situations, you can pass in those implementations that follow those situations. For example, you create a socket type. Parameters of latency and bandwidth (untested code, careful):

  import time, socket class controllable socket: def __init __ (self, latency, bandwidth): self. _latency = latency self._bandwidth = bandwidth self._bytesSent = 0 self._timeCreated = time.time () self._socket = socket.socket () send drf (auto, bytes): now = time.time () connectionsignment = now - Self ._timated themselves ._bitscent + = lane (bytes) # How many bytes have we sent with our # given bandwidth limit, how long will it take to send it? Expected period = self._bytesSent / self._bandwidth time.sleep (maximum (required duration - connection duration, self .latansi)) self-self._socket.send return (bytes)  

If you Apply other socket methods, connect, recv, etc., you can substitute an example of this class for an example of the real socket type. This completely releases all of your programs from any knowledge of your simulation, making it simple, while it tries to do many different network configurations by applying a new socket type to you.

This idea is one of the reasons to turn away, which clearly separates the idea of ​​"protocol" - objects that interpret the bytes from the network and generate new bytes to send to the network Know about - "Transports" - objects that get the bytes from the network and put it on the bytes. Separation makes testing easier and allows such novel configurations, where simulation of some other network conditions (which can be difficult to actually produce) by transport provides.


No comments:

Post a Comment