-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: serve files via twisted.web.static
- Loading branch information
Showing
2 changed files
with
38 additions
and
27 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,56 @@ | ||
#!/usr/bin/env python | ||
|
||
""" | ||
Simple static file server. | ||
""" | ||
"""Simple static file server.""" | ||
|
||
import argparse | ||
import os | ||
import SimpleHTTPServer | ||
import SocketServer | ||
import subprocess | ||
import time | ||
from contextlib import contextmanager | ||
|
||
from twisted.internet import reactor | ||
from twisted.web.server import Site | ||
from twisted.web.static import File | ||
|
||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument('port', type=int, help='Port number to listen at') | ||
parser.add_argument('directory', type=str, help='Directory to serve') | ||
import requests | ||
|
||
|
||
class ReusingTCPServer(SocketServer.TCPServer): | ||
allow_reuse_address = True | ||
|
||
|
||
class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | ||
def address_string(self): | ||
return "fileserver" | ||
parser = argparse.ArgumentParser("") | ||
parser.add_argument('--port', type=int) | ||
parser.add_argument('--directory', help='Directory to be served') | ||
|
||
|
||
@contextmanager | ||
def serve_files(port, directory): | ||
"""Serve files from current directory statically in a subprocess.""" | ||
site_server = subprocess.Popen(['python', '-m', __name__, | ||
str(port), directory]) | ||
def serve_files(port, directory, logfile=None): | ||
"""Serve files from specified directory statically in a subprocess.""" | ||
command = ['twistd', | ||
'-n', # don't daemonize | ||
'web', # start web component | ||
'--port', str(int(port)), | ||
'--path', os.path.abspath(directory), ] | ||
if logfile is not None: | ||
command += ['--logfile', logfile] | ||
site_server = subprocess.Popen(command) | ||
try: | ||
# It might take some time to bring up the server, wait for up to 10s. | ||
for i in xrange(100): | ||
try: | ||
requests.get('http://localhost:%d' % port) | ||
except requests.ConnectionError: | ||
time.sleep(0.1) | ||
else: | ||
break | ||
yield | ||
finally: | ||
site_server.terminate() | ||
|
||
|
||
if __name__ == '__main__': | ||
def main(): | ||
args = parser.parse_args() | ||
os.chdir(args.directory) | ||
server = ReusingTCPServer(("", args.port), RequestHandler) | ||
server.serve_forever() | ||
resource = File(os.path.abspath(args.directory)) | ||
site = Site(resource) | ||
reactor.listenTCP(args.port, site) | ||
reactor.run() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |