Skip to content

Instantly share code, notes, and snippets.

@llamaonsecurity
Forked from bradmontgomery/dummy-web-server.py
Last active April 27, 2018 13:05
Show Gist options
  • Select an option

  • Save llamaonsecurity/2fdc9d4e4afb042a78d406aed9cd3bba to your computer and use it in GitHub Desktop.

Select an option

Save llamaonsecurity/2fdc9d4e4afb042a78d406aed9cd3bba to your computer and use it in GitHub Desktop.

Revisions

  1. llamaonsecurity revised this gist Apr 27, 2018. 1 changed file with 39 additions and 15 deletions.
    54 changes: 39 additions & 15 deletions dummy-web-server.py
    Original file line number Diff line number Diff line change
    @@ -1,51 +1,75 @@
    #!/usr/bin/env python
    """
    Very simple HTTP server in python.
    Usage::
    ./dummy-web-server.py [<port>]
    ./dummy-web-server.py [<port>] [<file_to_serve>] [<content_type>]
    Send a GET request::
    curl http://localhost
    curl http://localhost -v
    Send a HEAD request::
    curl -I http://localhost
    curl -I http://localhost -v
    Send a POST request::
    curl -d "foo=bar&bin=baz" http://localhost
    curl -d "foo=bar&bin=baz" http://localhost -v
    """

    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import os
    import SocketServer

    class S(BaseHTTPRequestHandler):
    class RequestHandlerClass(BaseHTTPRequestHandler):
    served_File = None
    content_type = None

    def _set_headers(self):
    self.send_response(200)
    self.send_header('Content-type', 'text/html')
    self.send_header('Content-Type', self.content_type)
    self.send_header('Content-Length', os.path.getsize(self.served_File))
    self.end_headers()

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

    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(getContent(self.served_File))

    def run(server_class=HTTPServer, handler_class=S, port=80):
    def getContent(fromFile):
    f = open(fromFile,"r")
    content=f.read()
    print content
    return content

    class Server(HTTPServer):
    def serve_forever(self, served_File,content_Type):
    self.RequestHandlerClass.content_type = content_Type
    self.RequestHandlerClass.served_File = served_File

    print served_File
    print content_Type
    HTTPServer.serve_forever(self)


    def run(server_class=Server, handler_class=RequestHandlerClass, port=80,served_File="test.xml",content_Type="text/plain"):
    server_address = ('', port)

    httpd = server_class(server_address, handler_class)


    print 'Starting httpd...'
    httpd.serve_forever()
    httpd.serve_forever(served_File,content_Type)

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

    if len(argv) == 2:
    run(port=int(argv[1]))
    elif len(argv) == 3:
    run(port=int(argv[1]),served_File=argv[2])
    elif len(argv) == 4:
    run(port=int(argv[1]),served_File=argv[2],content_Type=argv[3])
    else:
    run()
  2. @bradmontgomery 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:
  3. @bradmontgomery 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()