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