Skip to content

Instantly share code, notes, and snippets.

@andreisavu
Created November 29, 2010 11:19
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andreisavu/719843 to your computer and use it in GitHub Desktop.
Save andreisavu/719843 to your computer and use it in GitHub Desktop.
Dummy HTTP server useful for testing
#!/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())
@amcgregor
Copy link

https://gist.github.com/719889 is an example of a similar mock HTTP server written using marrow.server (for connection handling) and marrow.script (for command line processing). The main difference is that the marrow-based mock server requires a complete status code (not just integer) as the status and should be Python 3.1+ compatible, though this has not been tested.

@andreisavu
Copy link
Author

I like it. Thanks for sharing. Could you tell more about the Marrow Open Source Projects?

@amcgregor
Copy link

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!

@andreisavu
Copy link
Author

Nice! I will take a closer look at the source code.

@jar-o
Copy link

jar-o commented Jun 1, 2017

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