Skip to content

Instantly share code, notes, and snippets.

@rikonor
Created April 21, 2016 19:44
Show Gist options
  • Save rikonor/f0edd054beda191cba709aea210c18f3 to your computer and use it in GitHub Desktop.
Save rikonor/f0edd054beda191cba709aea210c18f3 to your computer and use it in GitHub Desktop.
import socket
import sys
from time import time, sleep
import SimpleHTTPServer
import SocketServer
class Handle200(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
class Handle502(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
while True:
# Sleep 100ms
sleep(0.1)
class Handle500(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_error(500)
class Handle502(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_error(502)
class Handle503(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_error(503)
class HandleTimeout(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
while True:
pass
if len(sys.argv) < 2:
print "Please select a mode [200, 500, 502, 503, timeout]"
sys.exit(1)
handleMode = sys.argv[1]
modeDict = {
'200': Handle200,
'500': Handle500,
'502': Handle502,
'503': Handle503,
'timeout': HandleTimeout
}
defaultHandler = Handle200
selectedHandler = modeDict.get(handleMode, defaultHandler)
port = 8080
if len(sys.argv) == 3:
port = int(sys.argv[2])
class MyTCPServer(SocketServer.TCPServer):
def server_bind(self):
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
httpd = MyTCPServer(('localhost', port), selectedHandler)
print('Serving http://{addr[0]}:{addr[1]}/'.format(addr=httpd.socket.getsockname()))
try:
httpd.serve_forever()
except KeyboardInterrupt:
print('\nKeyboard interrupt received, exiting.')
httpd.server_close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment