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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 Args: | 107 Args: |
108 device: A DeviceUtils instance. | 108 device: A DeviceUtils instance. |
109 device_port: Port on device we want to check. | 109 device_port: Port on device we want to check. |
110 state: String of the specified state. Default is empty string, which | 110 state: String of the specified state. Default is empty string, which |
111 means any state. | 111 means any state. |
112 | 112 |
113 Returns: | 113 Returns: |
114 True if the port on device is already used, otherwise returns False. | 114 True if the port on device is already used, otherwise returns False. |
115 """ | 115 """ |
116 base_url = '127.0.0.1:%d' % device_port | 116 base_url = '127.0.0.1:%d' % device_port |
117 netstat_results = device.old_interface.RunShellCommand( | 117 netstat_results = device.RunShellCommand('netstat') |
118 'netstat', log_result=False) | |
119 for single_connect in netstat_results: | 118 for single_connect in netstat_results: |
120 # Column 3 is the local address which we want to check with. | 119 # Column 3 is the local address which we want to check with. |
121 connect_results = single_connect.split() | 120 connect_results = single_connect.split() |
122 if connect_results[0] != 'tcp': | 121 if connect_results[0] != 'tcp': |
123 continue | 122 continue |
124 if len(connect_results) < 6: | 123 if len(connect_results) < 6: |
125 raise Exception('Unexpected format while parsing netstat line: ' + | 124 raise Exception('Unexpected format while parsing netstat line: ' + |
126 single_connect) | 125 single_connect) |
127 is_state_match = connect_results[5] == state if state else True | 126 is_state_match = connect_results[5] == state if state else True |
128 if connect_results[3] == base_url and is_state_match: | 127 if connect_results[3] == base_url and is_state_match: |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 client_error = ('Bad response: %s %s version %s\n ' % | 167 client_error = ('Bad response: %s %s version %s\n ' % |
169 (r.status, r.reason, r.version) + | 168 (r.status, r.reason, r.version) + |
170 '\n '.join([': '.join(h) for h in r.getheaders()])) | 169 '\n '.join([': '.join(h) for h in r.getheaders()])) |
171 except (httplib.HTTPException, socket.error) as e: | 170 except (httplib.HTTPException, socket.error) as e: |
172 # Probably too quick connecting: try again. | 171 # Probably too quick connecting: try again. |
173 exception_error_msgs = traceback.format_exception_only(type(e), e) | 172 exception_error_msgs = traceback.format_exception_only(type(e), e) |
174 if exception_error_msgs: | 173 if exception_error_msgs: |
175 client_error = ''.join(exception_error_msgs) | 174 client_error = ''.join(exception_error_msgs) |
176 # Only returns last client_error. | 175 # Only returns last client_error. |
177 return (False, client_error or 'Timeout') | 176 return (False, client_error or 'Timeout') |
OLD | NEW |