Clone of official tools
host_tests/tcpecho_client_auto.py@47:21ae3e5a7128, 2021-02-04 (annotated)
- Committer:
- Anders Blomdell
- Date:
- Thu Feb 04 17:17:13 2021 +0100
- Revision:
- 47:21ae3e5a7128
- Parent:
- 43:2a7da56ebd24
Add a few normpath calls
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 | """ |
theotherjimmy |
43:2a7da56ebd24 | 17 | from __future__ import print_function |
screamer | 0:66f3b5499f7f | 18 | |
screamer | 0:66f3b5499f7f | 19 | import sys |
screamer | 0:66f3b5499f7f | 20 | import socket |
screamer | 0:66f3b5499f7f | 21 | from sys import stdout |
theotherjimmy |
43:2a7da56ebd24 | 22 | try: |
theotherjimmy |
43:2a7da56ebd24 | 23 | from SocketServer import BaseRequestHandler, TCPServer |
theotherjimmy |
43:2a7da56ebd24 | 24 | except ImportError: |
theotherjimmy |
43:2a7da56ebd24 | 25 | from socketserver import BaseRequestHandler, TCPServer |
screamer | 0:66f3b5499f7f | 26 | |
screamer | 0:66f3b5499f7f | 27 | class TCPEchoClient_Handler(BaseRequestHandler): |
screamer | 0:66f3b5499f7f | 28 | def handle(self): |
screamer | 0:66f3b5499f7f | 29 | """ One handle per connection |
screamer | 0:66f3b5499f7f | 30 | """ |
theotherjimmy |
43:2a7da56ebd24 | 31 | print("HOST: Connection received...") |
screamer | 0:66f3b5499f7f | 32 | count = 1; |
screamer | 0:66f3b5499f7f | 33 | while True: |
screamer | 0:66f3b5499f7f | 34 | data = self.request.recv(1024) |
screamer | 0:66f3b5499f7f | 35 | if not data: break |
screamer | 0:66f3b5499f7f | 36 | self.request.sendall(data) |
screamer | 0:66f3b5499f7f | 37 | if '{{end}}' in str(data): |
screamer | 0:66f3b5499f7f | 38 | |
theotherjimmy |
43:2a7da56ebd24 | 39 | print(str(data)) |
screamer | 0:66f3b5499f7f | 40 | else: |
screamer | 0:66f3b5499f7f | 41 | if not count % 10: |
screamer | 0:66f3b5499f7f | 42 | sys.stdout.write('.') |
screamer | 0:66f3b5499f7f | 43 | count += 1 |
screamer | 0:66f3b5499f7f | 44 | stdout.flush() |
screamer | 0:66f3b5499f7f | 45 | |
screamer | 0:66f3b5499f7f | 46 | class TCPEchoClientTest(): |
screamer | 0:66f3b5499f7f | 47 | def send_server_ip_port(self, selftest, ip_address, port_no): |
screamer | 0:66f3b5499f7f | 48 | """ Set up network host. Reset target and and send server IP via serial to Mbed |
screamer | 0:66f3b5499f7f | 49 | """ |
screamer | 0:66f3b5499f7f | 50 | c = selftest.mbed.serial_readline() # 'TCPCllient waiting for server IP and port...' |
screamer | 0:66f3b5499f7f | 51 | if c is None: |
screamer | 0:66f3b5499f7f | 52 | self.print_result(selftest.RESULT_IO_SERIAL) |
screamer | 0:66f3b5499f7f | 53 | return |
screamer | 0:66f3b5499f7f | 54 | |
screamer | 0:66f3b5499f7f | 55 | selftest.notify(c.strip()) |
screamer | 0:66f3b5499f7f | 56 | selftest.notify("HOST: Sending server IP Address to target...") |
screamer | 0:66f3b5499f7f | 57 | |
screamer | 0:66f3b5499f7f | 58 | connection_str = ip_address + ":" + str(port_no) + "\n" |
screamer | 0:66f3b5499f7f | 59 | selftest.mbed.serial_write(connection_str) |
screamer | 0:66f3b5499f7f | 60 | selftest.notify(connection_str) |
screamer | 0:66f3b5499f7f | 61 | |
screamer | 0:66f3b5499f7f | 62 | # Two more strings about connection should be sent by MBED |
screamer | 0:66f3b5499f7f | 63 | for i in range(0, 2): |
screamer | 0:66f3b5499f7f | 64 | c = selftest.mbed.serial_readline() |
screamer | 0:66f3b5499f7f | 65 | if c is None: |
screamer | 0:66f3b5499f7f | 66 | selftest.print_result(self.RESULT_IO_SERIAL) |
screamer | 0:66f3b5499f7f | 67 | return |
screamer | 0:66f3b5499f7f | 68 | selftest.notify(c.strip()) |
screamer | 0:66f3b5499f7f | 69 | |
screamer | 0:66f3b5499f7f | 70 | def test(self, selftest): |
screamer | 0:66f3b5499f7f | 71 | # We need to discover SERVEP_IP and set up SERVER_PORT |
screamer | 0:66f3b5499f7f | 72 | # Note: Port 7 is Echo Protocol: |
screamer | 0:66f3b5499f7f | 73 | # |
screamer | 0:66f3b5499f7f | 74 | # Port number rationale: |
screamer | 0:66f3b5499f7f | 75 | # |
screamer | 0:66f3b5499f7f | 76 | # The Echo Protocol is a service in the Internet Protocol Suite defined |
screamer | 0:66f3b5499f7f | 77 | # in RFC 862. It was originally proposed for testing and measurement |
screamer | 0:66f3b5499f7f | 78 | # of round-trip times[citation needed] in IP networks. |
screamer | 0:66f3b5499f7f | 79 | # |
screamer | 0:66f3b5499f7f | 80 | # A host may connect to a server that supports the Echo Protocol using |
screamer | 0:66f3b5499f7f | 81 | # the Transmission Control Protocol (TCP) or the User Datagram Protocol |
screamer | 0:66f3b5499f7f | 82 | # (UDP) on the well-known port number 7. The server sends back an |
screamer | 0:66f3b5499f7f | 83 | # identical copy of the data it received. |
screamer | 0:66f3b5499f7f | 84 | SERVER_IP = str(socket.gethostbyname(socket.getfqdn())) |
screamer | 0:66f3b5499f7f | 85 | SERVER_PORT = 7 |
screamer | 0:66f3b5499f7f | 86 | |
screamer | 0:66f3b5499f7f | 87 | # Returning none will suppress host test from printing success code |
screamer | 0:66f3b5499f7f | 88 | server = TCPServer((SERVER_IP, SERVER_PORT), TCPEchoClient_Handler) |
theotherjimmy |
43:2a7da56ebd24 | 89 | print("HOST: Listening for TCP connections: %s:%s" % |
theotherjimmy |
43:2a7da56ebd24 | 90 | (SERVER_IP, str(SERVER_PORT))) |
screamer | 0:66f3b5499f7f | 91 | self.send_server_ip_port(selftest, SERVER_IP, SERVER_PORT) |
screamer | 0:66f3b5499f7f | 92 | server.serve_forever() |