Created
November 29, 2010 11:19
-
-
Save andreisavu/719843 to your computer and use it in GitHub Desktop.
Dummy HTTP server useful for testing
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/env python | |
""" Test HTTP Server | |
This script starts a http server that will respond to HTTP requests | |
with a predefined response. | |
Usage: | |
./http_server.py --port=8080 --code=404 --content="Page not Found" | |
./http_server.py --port=8080 --code=500 --content="Internal Server Error" | |
""" | |
import sys, os | |
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler | |
from optparse import OptionParser | |
class APIHandler(BaseHTTPRequestHandler, object): | |
def __init__(self, *args, **kwargs): | |
super(APIHandler, self).__init__(*args, **kwargs) | |
def do_request(self): | |
global options | |
self.send_response(int(options.code)) | |
self.send_header('Content-type', options.type) | |
self.end_headers() | |
self.wfile.write(options.content) | |
def do_GET(self): | |
return self.do_request() | |
def do_POST(self): | |
return self.do_request() | |
def main(): | |
global options | |
options, args = parse_cli() | |
server_address = ('', int(options.port)) | |
httpd = HTTPServer(server_address, APIHandler) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
httpd.socket.close() | |
def parse_cli(): | |
parser = OptionParser() | |
parser.add_option('-p', '--port', default=8000) | |
parser.add_option('', '--code', default=200, \ | |
help="request return code") | |
parser.add_option('', '--type', default='text/html', \ | |
help="content type") | |
parser.add_option('', '--content', default='') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
sys.exit(main()) |
Nice! I will take a closer look at the source code.
hm, seems like an old conversation, but maybe even more relevant now -- with the current "microservice" craze, I found myself writing silly little one-off servers a lot just to provide integration testing in dev environments. I finally got sick of it and made
https://github.com/jar-o/dumdum
which allows you to mock out servers quickly without having to write actual code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Marrow is a "collective" (similar to the Zope collective, or repoze.* collective) name for a collection of projects designed to replace/supersede existing web development meta-component collections like Paste, WebOb, WebError, etc. The primary goal is to conform to the draft WSGI 2 spec (PEP 444) and bring Python 3 compatibility to the Python web world by brute forcing the chicken-and-egg problem. Packages should be extremely task-specific and have few external dependencies.
Thus Marrow includes command line script creation, configuration parsing, a highly performant HTTP/1.1 compliant server, various bits of middleware (and filters, a new concept in my modified PEP 444) and more. As a requirement for 1.0 release of each package, the package must have complete documentation, 100% unit test coverage, and must include examples of usage.
Packages currently released include: marrow.io, marrow.util, marrow.server, marrow.server.http, marrow.script, and marrow.blueprint is in heavy development. Not all of these are 1.0 yet.
Currently I'm going it alone, but the more interest I can generate the better!