Clone of official tools
host_tests/tcpecho_server.py@13:ab47a20b66f0, 2016-07-14 (annotated)
- Committer:
- screamer
- Date:
- Thu Jul 14 20:21:19 2016 +0100
- Revision:
- 13:ab47a20b66f0
- Parent:
- 0:66f3b5499f7f
Apply latest tools
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:66f3b5499f7f | 1 | """ |
screamer | 0:66f3b5499f7f | 2 | mbed SDK |
screamer | 0:66f3b5499f7f | 3 | Copyright (c) 2011-2013 ARM Limited |
screamer | 0:66f3b5499f7f | 4 | |
screamer | 0:66f3b5499f7f | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:66f3b5499f7f | 6 | you may not use this file except in compliance with the License. |
screamer | 0:66f3b5499f7f | 7 | You may obtain a copy of the License at |
screamer | 0:66f3b5499f7f | 8 | |
screamer | 0:66f3b5499f7f | 9 | http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:66f3b5499f7f | 10 | |
screamer | 0:66f3b5499f7f | 11 | Unless required by applicable law or agreed to in writing, software |
screamer | 0:66f3b5499f7f | 12 | distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:66f3b5499f7f | 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:66f3b5499f7f | 14 | See the License for the specific language governing permissions and |
screamer | 0:66f3b5499f7f | 15 | limitations under the License. |
screamer | 0:66f3b5499f7f | 16 | """ |
screamer | 0:66f3b5499f7f | 17 | from SocketServer import BaseRequestHandler, TCPServer |
screamer | 0:66f3b5499f7f | 18 | from time import time |
screamer | 0:66f3b5499f7f | 19 | |
screamer | 13:ab47a20b66f0 | 20 | from mbed_settings import LOCALHOST |
screamer | 0:66f3b5499f7f | 21 | |
screamer | 0:66f3b5499f7f | 22 | MAX_INDEX = 126 |
screamer | 0:66f3b5499f7f | 23 | MEGA = float(1024 * 1024) |
screamer | 0:66f3b5499f7f | 24 | |
screamer | 0:66f3b5499f7f | 25 | class TCP_EchoHandler(BaseRequestHandler): |
screamer | 0:66f3b5499f7f | 26 | def handle(self): |
screamer | 0:66f3b5499f7f | 27 | print "\nconnection received" |
screamer | 0:66f3b5499f7f | 28 | start = time() |
screamer | 0:66f3b5499f7f | 29 | bytes = 0 |
screamer | 0:66f3b5499f7f | 30 | index = 0 |
screamer | 0:66f3b5499f7f | 31 | while True: |
screamer | 0:66f3b5499f7f | 32 | data = self.request.recv(1024) |
screamer | 0:66f3b5499f7f | 33 | if not data: break |
screamer | 0:66f3b5499f7f | 34 | |
screamer | 0:66f3b5499f7f | 35 | bytes += len(data) |
screamer | 0:66f3b5499f7f | 36 | for n in map(ord, data): |
screamer | 0:66f3b5499f7f | 37 | if n != index: |
screamer | 0:66f3b5499f7f | 38 | print "data error %d != %d" % (n , index) |
screamer | 0:66f3b5499f7f | 39 | index += 1 |
screamer | 0:66f3b5499f7f | 40 | if index > MAX_INDEX: |
screamer | 0:66f3b5499f7f | 41 | index = 0 |
screamer | 0:66f3b5499f7f | 42 | |
screamer | 0:66f3b5499f7f | 43 | self.request.sendall(data) |
screamer | 0:66f3b5499f7f | 44 | t = time() - start |
screamer | 0:66f3b5499f7f | 45 | b = float(bytes * 8) * 2 |
screamer | 0:66f3b5499f7f | 46 | print "Throughput: (%.2f)Mbits/s" % ((b/t)/MEGA) |
screamer | 0:66f3b5499f7f | 47 | |
screamer | 0:66f3b5499f7f | 48 | server = TCPServer((LOCALHOST, 7), TCP_EchoHandler) |
screamer | 0:66f3b5499f7f | 49 | print "listening for connections" |
screamer | 0:66f3b5499f7f | 50 | server.serve_forever() |