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