| Masayuki Igawa | 46c688c | 2014-02-24 18:42:37 +0900 | [diff] [blame] | 1 | #    Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 2 | #    not use this file except in compliance with the License. You may obtain | 
|  | 3 | #    a copy of the License at | 
|  | 4 | # | 
|  | 5 | #         http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 6 | # | 
|  | 7 | #    Unless required by applicable law or agreed to in writing, software | 
|  | 8 | #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 9 | #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | 
|  | 10 | #    License for the specific language governing permissions and limitations | 
|  | 11 | #    under the License. | 
| Jesse Andrews | 6416407 | 2011-11-05 22:15:50 -0700 | [diff] [blame] | 12 |  | 
| Masayuki Igawa | 46c688c | 2014-02-24 18:42:37 +0900 | [diff] [blame] | 13 | import BaseHTTPServer | 
|  | 14 | import SimpleHTTPServer | 
|  | 15 | import sys | 
|  | 16 |  | 
|  | 17 |  | 
|  | 18 | def main(host, port, HandlerClass=SimpleHTTPServer.SimpleHTTPRequestHandler, | 
|  | 19 | ServerClass=BaseHTTPServer.HTTPServer, protocol="HTTP/1.0"): | 
|  | 20 | """simple http server that listens on a give address:port.""" | 
| Jesse Andrews | 6416407 | 2011-11-05 22:15:50 -0700 | [diff] [blame] | 21 |  | 
|  | 22 | server_address = (host, port) | 
|  | 23 |  | 
|  | 24 | HandlerClass.protocol_version = protocol | 
|  | 25 | httpd = ServerClass(server_address, HandlerClass) | 
|  | 26 |  | 
|  | 27 | sa = httpd.socket.getsockname() | 
| Masayuki Igawa | 46c688c | 2014-02-24 18:42:37 +0900 | [diff] [blame] | 28 | print("Serving HTTP on", sa[0], "port", sa[1], "...") | 
| Jesse Andrews | 6416407 | 2011-11-05 22:15:50 -0700 | [diff] [blame] | 29 | httpd.serve_forever() | 
|  | 30 |  | 
|  | 31 | if __name__ == '__main__': | 
|  | 32 | if sys.argv[1:]: | 
|  | 33 | address = sys.argv[1] | 
|  | 34 | else: | 
|  | 35 | address = '0.0.0.0' | 
|  | 36 | if ':' in address: | 
|  | 37 | host, port = address.split(':') | 
|  | 38 | else: | 
|  | 39 | host = address | 
|  | 40 | port = 8080 | 
|  | 41 |  | 
|  | 42 | main(host, int(port)) |