Created
March 26, 2014 08:49
-
-
Save dansimau/9779050 to your computer and use it in GitHub Desktop.
HTTP server that accepts connections and then hangs. Used to simulate and test slow API connections.
This file contains 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
#!/usr/bin/python | |
import sys | |
import time | |
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler | |
class RequestHandler(BaseHTTPRequestHandler): | |
def do_GET(s): | |
while True: | |
time.sleep(1000) | |
class TimeoutServer(HTTPServer): | |
def __init__(self, hostname, port): | |
HTTPServer.__init__(self, server_address=(hostname, port), | |
RequestHandlerClass=RequestHandler) | |
if __name__ == '__main__': | |
server = TimeoutServer('127.0.0.1', 9000) | |
print 'Will listen on %s:%i' % server.server_address | |
if len(sys.argv) > 1 and sys.argv[1] == '--ssl': | |
import ssl | |
server.socket = ssl.wrap_socket(server.socket, certfile=sys.argv[2], | |
server_side=True) | |
print 'SSL enabled.' | |
try: | |
server.serve_forever() | |
except KeyboardInterrupt: | |
pass |
Perfect solution for a problem I was trying to test against.
Also, I later found another tool for this (and more), called Bane. But it requires Ruby, so the above Python script worked perfectly for my needs.
https://github.com/danielwellman/bane
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Above is python2. Here is python3,