1 #!/usr/bin/env python 2 3 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 import BaseHTTPServer 8 import urlparse 9 10 class TestEndpointHandler(BaseHTTPServer.BaseHTTPRequestHandler): 11 """ 12 A web server that is used by cellular tests. It serves up the following 13 pages: 14 - http://<ip>/generate_204 15 This URL is used by shill's portal detection. 16 17 - http://<ip>/download?size=%d 18 Tests use this URL to download an arbitrary amount of data. 19 20 """ 21 GENERATE_204_PATH = '/generate_204' 22 DOWNLOAD_URL_PATH = '/download' 23 SIZE_PARAM = 'size' 24 25 def do_GET(self): 26 """Handles GET requests.""" 27 url = urlparse.urlparse(self.path) 28 print 'URL: %s' % url.path 29 if url.path == self.GENERATE_204_PATH: 30 self.send_response(204) 31 elif url.path == self.DOWNLOAD_URL_PATH: 32 parsed_query = urlparse.parse_qs(url.query) 33 if self.SIZE_PARAM not in parsed_query: 34 pass 35 self.send_response(200) 36 self.send_header('Content-type', 'application/octet-stream') 37 self.end_headers() 38 self.wfile.write('0' * int(parsed_query[self.SIZE_PARAM][0])) 39 else: 40 print 'Unsupported URL path: %s' % url.path 41 42 43 def main(): 44 """Main entry point when this script is run from the command line.""" 45 try: 46 server = BaseHTTPServer.HTTPServer(('', 80), TestEndpointHandler) 47 server.serve_forever() 48 except KeyboardInterrupt: 49 server.socket.close() 50 51 52 if __name__ == '__main__': 53 main() 54