Statistics
| Branch: | Tag: | Revision:

root / devel / webserver @ ab6536ba

History | View | Annotate | Download (1.6 kB)

1 c46a9ffb Michael Hanselmann
#!/usr/bin/python
2 c46a9ffb Michael Hanselmann
#
3 c46a9ffb Michael Hanselmann
4 c46a9ffb Michael Hanselmann
# Copyright (C) 2013 Google Inc.
5 c46a9ffb Michael Hanselmann
#
6 c46a9ffb Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 c46a9ffb Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 c46a9ffb Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 c46a9ffb Michael Hanselmann
# (at your option) any later version.
10 c46a9ffb Michael Hanselmann
#
11 c46a9ffb Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 c46a9ffb Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 c46a9ffb Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 c46a9ffb Michael Hanselmann
# General Public License for more details.
15 c46a9ffb Michael Hanselmann
#
16 c46a9ffb Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 c46a9ffb Michael Hanselmann
# along with this program; if not, write to the Free Software
18 c46a9ffb Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 c46a9ffb Michael Hanselmann
# 02110-1301, USA.
20 c46a9ffb Michael Hanselmann
21 c46a9ffb Michael Hanselmann
import sys
22 c46a9ffb Michael Hanselmann
import BaseHTTPServer
23 c46a9ffb Michael Hanselmann
import SimpleHTTPServer
24 c46a9ffb Michael Hanselmann
25 c46a9ffb Michael Hanselmann
26 c46a9ffb Michael Hanselmann
def main():
27 c46a9ffb Michael Hanselmann
  if len(sys.argv) == 2:
28 c46a9ffb Michael Hanselmann
    host = "127.0.0.1"
29 c46a9ffb Michael Hanselmann
30 c46a9ffb Michael Hanselmann
    (_, port) = sys.argv
31 c46a9ffb Michael Hanselmann
32 c46a9ffb Michael Hanselmann
  elif len(sys.argv) == 3:
33 c46a9ffb Michael Hanselmann
    (_, port, host) = sys.argv
34 c46a9ffb Michael Hanselmann
35 c46a9ffb Michael Hanselmann
  else:
36 c46a9ffb Michael Hanselmann
    sys.stderr.write("Usage: %s <port> [<host>]\n" % sys.argv[0])
37 c46a9ffb Michael Hanselmann
    sys.stderr.write("\n")
38 c46a9ffb Michael Hanselmann
    sys.stderr.write("Provides an HTTP server on the specified TCP port")
39 c46a9ffb Michael Hanselmann
    sys.stderr.write(" exporting the current working directory. Binds to")
40 c46a9ffb Michael Hanselmann
    sys.stderr.write(" localhost by default.\n")
41 c46a9ffb Michael Hanselmann
    sys.exit(1)
42 c46a9ffb Michael Hanselmann
43 c46a9ffb Michael Hanselmann
  try:
44 c46a9ffb Michael Hanselmann
    port = int(port)
45 c46a9ffb Michael Hanselmann
  except (ValueError, TypeError), err:
46 c46a9ffb Michael Hanselmann
    sys.stderr.write("Invalid port '%s': %s\n" % (port, err))
47 c46a9ffb Michael Hanselmann
    sys.exit(1)
48 c46a9ffb Michael Hanselmann
49 c46a9ffb Michael Hanselmann
  handler = SimpleHTTPServer.SimpleHTTPRequestHandler
50 c46a9ffb Michael Hanselmann
51 c46a9ffb Michael Hanselmann
  server = BaseHTTPServer.HTTPServer((host, port), handler)
52 c46a9ffb Michael Hanselmann
  server.serve_forever()
53 c46a9ffb Michael Hanselmann
54 c46a9ffb Michael Hanselmann
55 c46a9ffb Michael Hanselmann
if __name__ == "__main__":
56 c46a9ffb Michael Hanselmann
  main()