Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tcpecho_server.py Source File

tcpecho_server.py

00001 """
00002 mbed SDK
00003 Copyright (c) 2011-2013 ARM Limited
00004 
00005 Licensed under the Apache License, Version 2.0 (the "License");
00006 you may not use this file except in compliance with the License.
00007 You may obtain a copy of the License at
00008 
00009     http://www.apache.org/licenses/LICENSE-2.0
00010 
00011 Unless required by applicable law or agreed to in writing, software
00012 distributed under the License is distributed on an "AS IS" BASIS,
00013 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014 See the License for the specific language governing permissions and
00015 limitations under the License.
00016 """
00017 from SocketServer import BaseRequestHandler, TCPServer
00018 from time import time
00019 
00020 from mbed_settings import LOCALHOST
00021 
00022 MAX_INDEX = 126
00023 MEGA = float(1024 * 1024)
00024 
00025 class TCP_EchoHandler(BaseRequestHandler):
00026     def handle(self):
00027         print "\nconnection received"
00028         start = time()
00029         bytes = 0
00030         index = 0
00031         while True:
00032             data = self.request.recv(1024)
00033             if not data: break
00034 
00035             bytes += len(data)
00036             for n in map(ord, data):
00037                 if n != index:
00038                     print "data error %d != %d" % (n , index)
00039                 index += 1
00040                 if index > MAX_INDEX:
00041                     index = 0
00042 
00043             self.request.sendall(data)
00044         t = time() - start
00045         b = float(bytes * 8) * 2
00046         print "Throughput: (%.2f)Mbits/s" % ((b/t)/MEGA)
00047 
00048 server = TCPServer((LOCALHOST, 7), TCP_EchoHandler)
00049 print "listening for connections"
00050 server.serve_forever()