Last active
August 7, 2016 11:37
-
-
Save pingiun/c5edb7c4e9e869834ed14be90638c012 to your computer and use it in GitHub Desktop.
A simple http server that returns the requested error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
from __future__ import print_function | |
import argparse | |
import sys | |
try: | |
import BaseHTTPServer | |
except ImportError: | |
import http.server as BaseHTTPServer | |
try: | |
from SimpleHTTPServer import SimpleHTTPRequestHandler | |
except ImportError: | |
from http.server import SimpleHTTPRequestHandler | |
try: | |
from cStringIO import StringIO | |
except ImportError: | |
try: | |
from StringIO import StringIO | |
except ImportError: | |
from io import BytesIO as StringIO | |
ERRORCODE = 404 | |
class ErrorHandler(SimpleHTTPRequestHandler): | |
def do_GET(self): | |
self.send_response(ERRORCODE) | |
self.send_header("Content-type", "text/plain") | |
self.end_headers() | |
if sys.version_info >= (3,): | |
self.wfile.write(bytes("Code: ", 'UTF-8')) | |
self.wfile.write(bytes(str(ERRORCODE), 'UTF-8')) | |
else: | |
self.wfile.write("Code: ") | |
self.wfile.write(str(ERRORCODE)) | |
def main(): | |
global ERRORCODE | |
parser = argparse.ArgumentParser(description='Make a simple HTTP server that responds with errors') | |
parser.add_argument('-e', '--error', type=int, default=404, help="The html status that is send back") | |
parser.add_argument('-l', '--listen', dest='ip', default='127.0.0.1', help="The ip to listen on") | |
parser.add_argument('-p', '--port', type=int, default=80, help="The port to listen on") | |
args = parser.parse_args() | |
ERRORCODE = args.error | |
HandlerClass = ErrorHandler | |
ServerClass = BaseHTTPServer.HTTPServer | |
Protocol = "HTTP/1.0" | |
HandlerClass.protocol_version = Protocol | |
httpd = ServerClass((args.ip, args.port), HandlerClass) | |
sa = httpd.socket.getsockname() | |
print("Serving HTTP on", sa[0], "port", sa[1], "...") | |
httpd.serve_forever() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment