| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Functions that deal with local and device ports.""" | 5 """Functions that deal with local and device ports.""" |
| 6 | 6 |
| 7 import contextlib | 7 import contextlib |
| 8 import fcntl | 8 import fcntl |
| 9 import httplib | 9 import httplib |
| 10 import logging | 10 import logging |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 Args: | 103 Args: |
| 104 device: A DeviceUtils instance. | 104 device: A DeviceUtils instance. |
| 105 device_port: Port on device we want to check. | 105 device_port: Port on device we want to check. |
| 106 state: String of the specified state. Default is empty string, which | 106 state: String of the specified state. Default is empty string, which |
| 107 means any state. | 107 means any state. |
| 108 | 108 |
| 109 Returns: | 109 Returns: |
| 110 True if the port on device is already used, otherwise returns False. | 110 True if the port on device is already used, otherwise returns False. |
| 111 """ | 111 """ |
| 112 base_url = '127.0.0.1:%d' % device_port | 112 base_urls = ('127.0.0.1:%d' % device_port, 'localhost:%d' % device_port) |
| 113 netstat_results = device.RunShellCommand('netstat') | 113 netstat_results = device.RunShellCommand( |
| 114 ['netstat', '-a'], check_return=True, large_output=True) |
| 114 for single_connect in netstat_results: | 115 for single_connect in netstat_results: |
| 115 # Column 3 is the local address which we want to check with. | 116 # Column 3 is the local address which we want to check with. |
| 116 connect_results = single_connect.split() | 117 connect_results = single_connect.split() |
| 117 if connect_results[0] != 'tcp': | 118 if connect_results[0] != 'tcp': |
| 118 continue | 119 continue |
| 119 if len(connect_results) < 6: | 120 if len(connect_results) < 6: |
| 120 raise Exception('Unexpected format while parsing netstat line: ' + | 121 raise Exception('Unexpected format while parsing netstat line: ' + |
| 121 single_connect) | 122 single_connect) |
| 122 is_state_match = connect_results[5] == state if state else True | 123 is_state_match = connect_results[5] == state if state else True |
| 123 if connect_results[3] == base_url and is_state_match: | 124 if connect_results[3] in base_urls and is_state_match: |
| 124 return True | 125 return True |
| 125 return False | 126 return False |
| 126 | 127 |
| 127 | 128 |
| 128 def IsHttpServerConnectable(host, port, tries=3, command='GET', path='/', | 129 def IsHttpServerConnectable(host, port, tries=3, command='GET', path='/', |
| 129 expected_read='', timeout=2): | 130 expected_read='', timeout=2): |
| 130 """Checks whether the specified http server is ready to serve request or not. | 131 """Checks whether the specified http server is ready to serve request or not. |
| 131 | 132 |
| 132 Args: | 133 Args: |
| 133 host: Host name of the HTTP server. | 134 host: Host name of the HTTP server. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 163 client_error = ('Bad response: %s %s version %s\n ' % | 164 client_error = ('Bad response: %s %s version %s\n ' % |
| 164 (r.status, r.reason, r.version) + | 165 (r.status, r.reason, r.version) + |
| 165 '\n '.join([': '.join(h) for h in r.getheaders()])) | 166 '\n '.join([': '.join(h) for h in r.getheaders()])) |
| 166 except (httplib.HTTPException, socket.error) as e: | 167 except (httplib.HTTPException, socket.error) as e: |
| 167 # Probably too quick connecting: try again. | 168 # Probably too quick connecting: try again. |
| 168 exception_error_msgs = traceback.format_exception_only(type(e), e) | 169 exception_error_msgs = traceback.format_exception_only(type(e), e) |
| 169 if exception_error_msgs: | 170 if exception_error_msgs: |
| 170 client_error = ''.join(exception_error_msgs) | 171 client_error = ''.join(exception_error_msgs) |
| 171 # Only returns last client_error. | 172 # Only returns last client_error. |
| 172 return (False, client_error or 'Timeout') | 173 return (False, client_error or 'Timeout') |
| OLD | NEW |