Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tcpecho_client_auto.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """
  2. mbed SDK
  3. Copyright (c) 2011-2013 ARM Limited
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. """
  14. import sys
  15. import socket
  16. from sys import stdout
  17. from SocketServer import BaseRequestHandler, TCPServer
  18. class TCPEchoClient_Handler(BaseRequestHandler):
  19. def handle(self):
  20. """ One handle per connection
  21. """
  22. print "HOST: Connection received...",
  23. count = 1;
  24. while True:
  25. data = self.request.recv(1024)
  26. if not data: break
  27. self.request.sendall(data)
  28. if '{{end}}' in str(data):
  29. print
  30. print str(data)
  31. else:
  32. if not count % 10:
  33. sys.stdout.write('.')
  34. count += 1
  35. stdout.flush()
  36. class TCPEchoClientTest():
  37. def send_server_ip_port(self, selftest, ip_address, port_no):
  38. """ Set up network host. Reset target and and send server IP via serial to Mbed
  39. """
  40. c = selftest.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...'
  41. if c is None:
  42. self.print_result(selftest.RESULT_IO_SERIAL)
  43. return
  44. selftest.notify(c.strip())
  45. selftest.notify("HOST: Sending server IP Address to target...")
  46. connection_str = ip_address + ":" + str(port_no) + "\n"
  47. selftest.mbed.serial_write(connection_str)
  48. selftest.notify(connection_str)
  49. # Two more strings about connection should be sent by MBED
  50. for i in range(0, 2):
  51. c = selftest.mbed.serial_readline()
  52. if c is None:
  53. selftest.print_result(self.RESULT_IO_SERIAL)
  54. return
  55. selftest.notify(c.strip())
  56. def test(self, selftest):
  57. # We need to discover SERVEP_IP and set up SERVER_PORT
  58. # Note: Port 7 is Echo Protocol:
  59. #
  60. # Port number rationale:
  61. #
  62. # The Echo Protocol is a service in the Internet Protocol Suite defined
  63. # in RFC 862. It was originally proposed for testing and measurement
  64. # of round-trip times[citation needed] in IP networks.
  65. #
  66. # A host may connect to a server that supports the Echo Protocol using
  67. # the Transmission Control Protocol (TCP) or the User Datagram Protocol
  68. # (UDP) on the well-known port number 7. The server sends back an
  69. # identical copy of the data it received.
  70. SERVER_IP = str(socket.gethostbyname(socket.getfqdn()))
  71. SERVER_PORT = 7
  72. # Returning none will suppress host test from printing success code
  73. server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler)
  74. print "HOST: Listening for TCP connections: " + SERVER_IP + ":" + str(SERVER_PORT)
  75. self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT)
  76. server.serve_forever()