Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Last active September 24, 2025 10:25
Show Gist options
  • Save bradmontgomery/2219997 to your computer and use it in GitHub Desktop.
Save bradmontgomery/2219997 to your computer and use it in GitHub Desktop.

Revisions

  1. bradmontgomery revised this gist Jan 25, 2021. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions dummy-web-server.py
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,27 @@
    curl -d "foo=bar&bin=baz" http://localhost:8000
    This code is available for use under the MIT license.
    ----
    Copyright 2021 Brad Montgomery
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
    associated documentation files (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all copies or substantial
    portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
    LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
    OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    """
    import argparse
    from http.server import HTTPServer, BaseHTTPRequestHandler
  2. bradmontgomery revised this gist Sep 27, 2019. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions dummy-web-server.py
    Original file line number Diff line number Diff line change
    @@ -30,17 +30,25 @@ def _set_headers(self):
    self.send_header("Content-type", "text/html")
    self.end_headers()

    def _html(self, message):
    """This just generates an HTML document that includes `message`
    in the body. Override, or re-write this do do more interesting stuff.
    """
    content = f"<html><body><h1>{message}</h1></body></html>"
    return content.encode("utf8") # NOTE: must return a bytes object!

    def do_GET(self):
    self._set_headers()
    self.wfile.write(b"<html><body><h1>hi!</h1></body></html>")
    self.wfile.write(self._html("hi!"))

    def do_HEAD(self):
    self._set_headers()

    def do_POST(self):
    # Doesn't do anything with posted data
    self._set_headers()
    self.wfile.write(b"<html><body><h1>POST!</h1></body></html>")
    self.wfile.write(self._html("POST!"))


    def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
  3. bradmontgomery revised this gist Sep 27, 2019. 1 changed file with 16 additions and 11 deletions.
    27 changes: 16 additions & 11 deletions dummy-web-server.py
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,23 @@
    #!/usr/bin/env python
    """
    Very simple HTTP server in python.
    Very simple HTTP server in python (Updated for Python 3.7)
    Usage::
    ./dummy-web-server.py [<port>]
    Usage:
    Send a GET request::
    curl http://localhost
    ./dummy-web-server.py -h
    ./dummy-web-server.py -l localhost -p 8000
    Send a HEAD request::
    curl -I http://localhost
    Send a GET request:
    Send a POST request::
    curl -d "foo=bar&bin=baz" http://localhost
    curl http://localhost:8000
    Send a HEAD request:
    curl -I http://localhost:8000
    Send a POST request:
    curl -d "foo=bar&bin=baz" http://localhost:8000
    """
    import argparse
    @@ -27,15 +32,15 @@ def _set_headers(self):

    def do_GET(self):
    self._set_headers()
    self.wfile.write("<html><body><h1>hi!</h1></body></html>")
    self.wfile.write(b"<html><body><h1>hi!</h1></body></html>")

    def do_HEAD(self):
    self._set_headers()

    def do_POST(self):
    # Doesn't do anything with posted data
    self._set_headers()
    self.wfile.write("<html><body><h1>POST!</h1></body></html>")
    self.wfile.write(b"<html><body><h1>POST!</h1></body></html>")


    def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
  4. bradmontgomery revised this gist Sep 27, 2019. 1 changed file with 28 additions and 13 deletions.
    41 changes: 28 additions & 13 deletions dummy-web-server.py
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -15,13 +15,14 @@
    curl -d "foo=bar&bin=baz" http://localhost
    """
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import SocketServer
    import argparse
    from http.server import HTTPServer, BaseHTTPRequestHandler


    class S(BaseHTTPRequestHandler):
    def _set_headers(self):
    self.send_response(200)
    self.send_header('Content-type', 'text/html')
    self.send_header("Content-type", "text/html")
    self.end_headers()

    def do_GET(self):
    @@ -30,22 +31,36 @@ def do_GET(self):

    def do_HEAD(self):
    self._set_headers()

    def do_POST(self):
    # Doesn't do anything with posted data
    self._set_headers()
    self.wfile.write("<html><body><h1>POST!</h1></body></html>")

    def run(server_class=HTTPServer, handler_class=S, port=80):
    server_address = ('', port)


    def run(server_class=HTTPServer, handler_class=S, addr="localhost", port=8000):
    server_address = (addr, port)
    httpd = server_class(server_address, handler_class)
    print 'Starting httpd...'

    print(f"Starting httpd server on {addr}:{port}")
    httpd.serve_forever()


    if __name__ == "__main__":
    from sys import argv

    if len(argv) == 2:
    run(port=int(argv[1]))
    else:
    run()
    parser = argparse.ArgumentParser(description="Run a simple HTTP server")
    parser.add_argument(
    "-l",
    "--listen",
    default="localhost",
    help="Specify the IP address on which the server listens",
    )
    parser.add_argument(
    "-p",
    "--port",
    type=int,
    default=8000,
    help="Specify the port on which the server listens",
    )
    args = parser.parse_args()
    run(addr=args.listen, port=args.port)
  5. bradmontgomery revised this gist Mar 27, 2012. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions dummy-web-server.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    #!/usr/bin/env python
    """
    Very simple HTTP server in python.
    @@ -40,14 +39,12 @@ def do_POST(self):
    def run(server_class=HTTPServer, handler_class=S, port=80):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'STarting httpd...'
    print 'Starting httpd...'
    httpd.serve_forever()

    if __name__ == "__main__":
    from sys import argv

    print 'argv = ', argv

    if len(argv) == 2:
    run(port=int(argv[1]))
    else:
  6. bradmontgomery created this gist Mar 27, 2012.
    54 changes: 54 additions & 0 deletions dummy-web-server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@

    #!/usr/bin/env python
    """
    Very simple HTTP server in python.
    Usage::
    ./dummy-web-server.py [<port>]
    Send a GET request::
    curl http://localhost
    Send a HEAD request::
    curl -I http://localhost
    Send a POST request::
    curl -d "foo=bar&bin=baz" http://localhost
    """
    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import SocketServer

    class S(BaseHTTPRequestHandler):
    def _set_headers(self):
    self.send_response(200)
    self.send_header('Content-type', 'text/html')
    self.end_headers()

    def do_GET(self):
    self._set_headers()
    self.wfile.write("<html><body><h1>hi!</h1></body></html>")

    def do_HEAD(self):
    self._set_headers()

    def do_POST(self):
    # Doesn't do anything with posted data
    self._set_headers()
    self.wfile.write("<html><body><h1>POST!</h1></body></html>")

    def run(server_class=HTTPServer, handler_class=S, port=80):
    server_address = ('', port)
    httpd = server_class(server_address, handler_class)
    print 'STarting httpd...'
    httpd.serve_forever()

    if __name__ == "__main__":
    from sys import argv

    print 'argv = ', argv

    if len(argv) == 2:
    run(port=int(argv[1]))
    else:
    run()