zeek/testing/scripts/httpd.py
Daniel Thayer e780ebb17a Fix httpd.py test script for Py3 compatibility
With these changes the script still works on Python 2.6/2.7.
2016-10-06 15:45:11 -05:00

59 lines
1.7 KiB
Python
Executable file

#! /usr/bin/env python
try:
# Python 2
import BaseHTTPServer
except ImportError:
# Python 3
import http.server as BaseHTTPServer
class MyRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
if "/empty" in self.path:
self.wfile.write(b"")
else:
self.wfile.write(b"It works!")
def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
if "/empty" in self.path:
self.wfile.write(b"")
else:
self.wfile.write(b"It works!")
def version_string(self):
return "1.0"
def date_time_string(self):
return "July 22, 2013"
if __name__ == "__main__":
from optparse import OptionParser
p = OptionParser()
p.add_option("-a", "--addr", type="string", default="localhost",
help=("listen on given address (numeric IP or host name), "
"an empty string (the default) means INADDR_ANY"))
p.add_option("-p", "--port", type="int", default=32123,
help="listen on given TCP port number")
p.add_option("-m", "--max", type="int", default=-1,
help="max number of requests to respond to, -1 means no max")
options, args = p.parse_args()
httpd = BaseHTTPServer.HTTPServer((options.addr, options.port),
MyRequestHandler)
if options.max == -1:
httpd.serve_forever()
else:
served_count = 0
while served_count != options.max:
httpd.handle_request()
served_count += 1