def main(args): """Fetch workflows if requested and serve on localhost Args: args: arguments passed to this script """ if args.fetch or args.tool_names: with open("hosts.json") as file: hosts = json.load(file) with ThreadPoolExecutor(max_workers=args.max_workers) as tpe: pbar = tqdm(hosts.items()) result = tpe.map( partial(fetch_workflows, args.max_workflows, args.tool_names, pbar), pbar, ) result = list(result) print("Total number of workflows fetched: ", len(result)) with open("workflows.json", "w") as file: json.dump(result, file, indent=4, sort_keys=True) test(CORSRequestHandler, HTTPServer, port=8082)
def Start(handler,port=8000): server.test( HandlerClass=handler, ServerClass=server.ThreadingHTTPServer, port=port, bind=None, )
def _start(self): from http import server os.chdir(self.work_dir) server.test(HandlerClass=server.SimpleHTTPRequestHandler, bind=self.bind, port=self.port) RuntimeError('The HTTP server exits without calling stop()')
def test_server_test_localhost(self, _): mock_server = self.mock_server_class() server.test(ServerClass=mock_server, bind="localhost") self.assertIn( mock_server.address_family, (socket.AF_INET6, socket.AF_INET), )
def test_server_test_unspec(self, _): mock_server = self.mock_server_class() server.test(ServerClass=mock_server, bind=None) self.assertIn( mock_server.address_family, (socket.AF_INET6, socket.AF_INET), )
def test_server_test_unspec(self, _): mock_server = self.mock_server_class() server.test(ServerClass=mock_server, bind=None) self.assertIn( mock_server.address_family, (socket.AF_INET6, socket.AF_INET), )
def test_server_test_localhost(self, _): mock_server = self.mock_server_class() server.test(ServerClass=mock_server, bind="localhost") self.assertIn( mock_server.address_family, (socket.AF_INET6, socket.AF_INET), )
def run_throttled_server(port: int = 8000, threshold: int = 10**6) -> None: _ = get_dataset("smallfile") _ = get_dataset("bigfile") _ = get_dataset_bz2("smallfile") _ = get_dataset_bz2("bigfile") os.chdir(DATA_DIR) ThrottledReqHandler.threshold = threshold http_srv.test(HandlerClass=ThrottledReqHandler, port=port) # type: ignore
def _start(self): """ Equivalent to executing `python3 -m http.server` """ os.chdir(self.work_dir) server.test(HandlerClass=server.SimpleHTTPRequestHandler, bind=self.bind, port=self.port) log.error('The HTTP server exits without calling stop()') print(log.to_str())
def serve(filename, port, bind): handler_class = partial(OPENAPIRequestHandler, openapi_file=filename) test( HandlerClass=handler_class, ServerClass=DualStackServer, port=port, bind=bind, )
def run(directory=None): SimpleHTTPRequestHandler.extensions_map = { k: f"{v};charset=UTF-8" for k, v in SimpleHTTPRequestHandler.extensions_map.items() } if directory is None: directory = sys.argv[1] if len(sys.argv) > 1 else "html" os.chdir(directory) test(SimpleHTTPRequestHandler)
def main(): args = parser.parse_args() if not args.bind: args.bind = None server.test( HandlerClass=MyHTTPRequestHandler, protocol="HTTP/1.1", port=args.port, bind=args.bind, )
def main(): import sys if 2 < sys.version_info[0]: import os import http.server as hs handler_class = hs.partial(hs.SimpleHTTPRequestHandler, directory=os.getcwd()) hs.test(HandlerClass=handler_class, port=8000, bind='') else: from SimpleHTTPServer import test test()
def run_throttled_server(port=8000, threshold=10**6): _ = get_dataset('smallfile') _ = get_dataset('bigfile') _ = get_dataset_bz2('smallfile') _ = get_dataset_bz2('bigfile') os.chdir(DATA_DIR) ThrottledReqHandler.threshold = threshold if six.PY2: import sys sys.argv[1] = 8000 http_srv.test(HandlerClass=ThrottledReqHandler) else: http_srv.test(HandlerClass=ThrottledReqHandler, port=port)
def serve(self): outer = self class ClojureServer(HTTPServer): def __init__(self, *args, **kwargs): HTTPServer.__init__(self, *args, **kwargs) outer.server = self class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): self.send_header("Access-Control-Allow-Origin", "*") SimpleHTTPRequestHandler.end_headers(self) test(CORSRequestHandler, ClojureServer)
def docs(serve=False, port=5001): import subprocess from http.server import test, CGIHTTPRequestHandler from pathlib import Path docs_directory = Path(os.path.abspath(os.path.dirname(__file__))) / 'docs' with working_directory(str(docs_directory)): # Generate Documentation through Makefile subprocess.call('make html', shell=True) if serve: with working_directory(str(docs_directory / 'build/html')): test(HandlerClass=CGIHTTPRequestHandler, port=int(port), bind='')
def start_server(self, **kwargs): os.chdir(os.path.join(self.project_dir, "doc", "_build/html")) addr = self.default_addr port = self.default_port if kwargs["addrport"]: addrport = kwargs["addrport"].split(":") addr = addrport[0] if len(addrport) > 1: port = addrport[1] if not port.isnumeric(): self.stdout.write("%s is not a valid port" % (port, )) sys.exit(0) test(HandlerClass=CGIHTTPRequestHandler, port=int(port), bind=addr)
def serve(self): outer = self try: from http.server import HTTPServer, SimpleHTTPRequestHandler, test except ImportError: # Python 2 from BaseHTTPServer import HTTPServer, test from SimpleHTTPServer import SimpleHTTPRequestHandler class ClojureServer(HTTPServer): def __init__(self, *args, **kwargs): HTTPServer.__init__(self, *args, **kwargs) outer.server = self class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): self.send_header("Access-Control-Allow-Origin", "*") SimpleHTTPRequestHandler.end_headers(self) test(CORSRequestHandler, ClojureServer)
def main(preview_data=False): x, y = generate_images(nm_samples=70000, side_pixels=28) if preview_data: show_images(x[:100], [str(i) for i in y[:100]], shape=(28, 28)) x_train = x[0:-10000] y_train = y[0:-10000] x_test = x[-10000:] y_test = y[-10000:] model = None with tf.device('/GPU:0'): config = tf.ConfigProto(log_device_placement=False) sess = tf.Session(config=config) K.set_session(sess) model = learn(x_train, y_train, x_test, y_test) PredictionServer.model = model server.test(HandlerClass=PredictionServer)
def main(): """ Parse args and call TarHTTPServer.""" parser = argparse.ArgumentParser() parser.add_argument( '--bind', '-b', metavar='ADDRESS', help='Specify alternate bind address [default: all interfaces]') parser.add_argument( '--directory', '-d', default=os.getcwd(), help='Specify alternative directory [default:current directory]') parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() handler_class = partial(TarHTTPServer, directory=args.directory) test(HandlerClass=handler_class, port=args.port, bind=args.bind)
def test_server_test_ipv6(self, _): mock_server = mock.MagicMock() server.test(ServerClass=mock_server, bind="::") self.assertEqual(mock_server.address_family, socket.AF_INET6) mock_server.reset_mock() server.test(ServerClass=mock_server, bind="2001:0db8:85a3:0000:0000:8a2e:0370:7334") self.assertEqual(mock_server.address_family, socket.AF_INET6) mock_server.reset_mock() server.test(ServerClass=mock_server, bind="::1") self.assertEqual(mock_server.address_family, socket.AF_INET6)
self.send_response(206) self.send_header('Content-type', ctype) self.send_header('Accept-Ranges', 'bytes') if last is None or last >= file_len: last = file_len - 1 response_length = last - first + 1 self.send_header('Content-Range', 'bytes %s-%s/%s' % (first, last, file_len)) self.send_header('Content-Length', str(response_length)) self.send_header('Last-Modified', self.date_time_string(fs.st_mtime)) self.end_headers() return f def copyfile(self, source, outputfile): if not self.range: return SimpleHTTPRequestHandler.copyfile(self, source, outputfile) # SimpleHTTPRequestHandler uses shutil.copyfileobj, which doesn't let # you stop the copying before the end of the file. start, stop = self.range # set in send_head() copy_byte_range(source, outputfile, start, stop) if __name__ == '__main__': os.chdir('public') build() test(HandlerClass=Handler)
def run(HandlerClass=Handler, ServerClass=ThreadedHTTPServer): test(HandlerClass, ServerClass, port=PORT)
def run_server(): from http.server import test, SimpleHTTPRequestHandler os.chdir(OUTPUT) test(HandlerClass=SimpleHTTPRequestHandler)
def test_server_test_ipv4(self, _): for bind in self.ipv4_addrs: mock_server = self.mock_server_class() server.test(ServerClass=mock_server, bind=bind) self.assertEqual(mock_server.address_family, socket.AF_INET)
try: import http.server as server except ImportError: # Handle Python 2.x import SimpleHTTPServer as server class HTTPRequestHandler(server.SimpleHTTPRequestHandler): """Extend SimpleHTTPRequestHandler to handle PUT requests""" def do_PUT(self): """Save a file following a HTTP PUT request""" filename = os.path.basename(self.path) # Don't overwrite files if os.path.exists(filename): self.send_response(409, 'Conflict') self.end_headers() reply_body = '"%s" already exists\n' % filename self.wfile.write(reply_body.encode('utf-8')) return file_length = int(self.headers['Content-Length']) with open(filename, 'wb') as output_file: output_file.write(self.rfile.read(file_length)) self.send_response(201, 'Created') self.end_headers() reply_body = 'Saved "%s"\n' % filename self.wfile.write(reply_body.encode('utf-8')) if __name__ == '__main__': server.test(HandlerClass=HTTPRequestHandler)
from http.server import HTTPServer, SimpleHTTPRequestHandler, test import sys class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): self.send_header('Access-Control-Allow-Origin', '*') SimpleHTTPRequestHandler.end_headers(self) if __name__ == '__main__': test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8090) test.server_forever()
#!/usr/bin/env python from argparse import ArgumentParser from http.server import SimpleHTTPRequestHandler, test class DevelopmentRequestHandler(SimpleHTTPRequestHandler): # overrides def end_headers(self): self.add_no_cache_headers() super(SimpleHTTPRequestHandler, self).end_headers() def add_no_cache_headers(self): self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate') self.send_header('Pragma', 'no-cache') self.send_header('Expires', '0') if __name__ == '__main__': parser = ArgumentParser() parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() test(HandlerClass=DevelopmentRequestHandler, port=args.port)
def send_files( direction: DIRECTION, directory: Path, port: int = 11000, overwrite=False, password=None, ): ''' Start a web server with a file upload form, provide the URL to access the files, and save the uploaded files to the specified directory. :param direction: "publish" or "receive" :param directory: Directory to publish or to receive into :param int port: Port to serve the web server on :param bool overwrite: Whether to allow an uploaded file to overwrite existing files (only applies for "receive" direction) :param password: Set the password manually. If you don't set a password, a random password will be generated. The password is included in the URL, so you don't need to tell anyone that a password is involved. ''' # Inputs if direction == Direction.publish: if not directory.is_dir(): msg = 'The source ("%s") must be a directory. Use "." for the current directory.' raise Error(msg % directory) else: if (not overwrite) and directory.exists(): if any(directory.iterdir()): raise Error( 'The directory already exists. Choose a different destination or set -overwrite.' ) makedirs(directory, exist_ok=True) # URL if not password: password = new_password() for url in urls(port, password): yield url # Serve if direction == Direction.publish: chdir(directory) class ProtectedHTTPRequestHandler(PrefixedHTTPRequestHandler): prefix = '/' + password test(HandlerClass=ProtectedHTTPRequestHandler, port=port) else: def endpoint(file: InputBinaryFile): ''' Upload a file. :param file: File to send ''' # Get and validate name. if file.filename in {'.', '..'}: raise Error('The name "%s" is reserved.' % file.filename) elif '/' in file.filename: raise Error('Name may not include slashes.') # Check if file exists. outpath = (directory / file.filename) if overwrite: pass elif outpath.exists(): raise Error('A file named "%s" already exists.' % file.filename) # Save with outpath.open('wb') as outfile: for chunk in file: outfile.write(chunk) # Feedback return 'The file has been uploaded.' application = wsgi_form(Program({password: endpoint}, name=NAME)) make_server('0.0.0.0', port, application).serve_forever()
from http.server import CGIHTTPRequestHandler,test if __name__=='__main__': test(CGIHTTPRequestHandler) ''' run this script and then in the browser open 127.0.0.1:8000/chapter10-cgibin.html and the Submit button calls the script in the /cgi-bin folder '''
out.send(data) count = 0 else: logging.warning('Idle') if count == max_idling: break do_HEAD = do_GET do_POST = do_GET do_PUT = do_GET do_DELETE=do_GET class ThreadingHTTPServer(ThreadingMixIn, HTTPServer): pass if __name__ == '__main__': from sys import argv if argv[1:] and argv[1] in ('-h', '--help'): print(argv[0], "[port [allowed_client_name ...]]") else: if argv[2:]: allowed = [] for name in argv[2:]: client = socket.gethostbyname(name) allowed.append(client) logging.warning('Accepted: {} ({})'.format(client, name)) ProxyHandler.allowed_clients = allowed del argv[2:] else: logging.warning('Waiting for clients') test(ProxyHandler, ThreadingHTTPServer)
#!/usr/bin/python ''' Use this in the same way as Python's SimpleHTTPServer: python -m RangeHTTPServer [port] The only difference from SimpleHTTPServer is that RangeHTTPServer supports 'Range:' headers to load portions of files. This is helpful for doing local web development with genomic data files, which tend to be to large to load into the browser all at once. ''' try: # Python3 import http.server as SimpleHTTPServer except ImportError: # Python 2 import SimpleHTTPServer from . import RangeRequestHandler SimpleHTTPServer.test(HandlerClass=RangeRequestHandler)
from http.server import CGIHTTPRequestHandler, test test(CGIHTTPRequestHandler)
python -m RangeHTTPServer [port] The only difference from SimpleHTTPServer is that RangeHTTPServer supports 'Range:' headers to load portions of files. This is helpful for doing local web development with genomic data files, which tend to be to large to load into the browser all at once. ''' try: # Python3 import http.server as SimpleHTTPServer except ImportError: # Python 2 import SimpleHTTPServer from . import RangeRequestHandler import argparse parser = argparse.ArgumentParser() parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() SimpleHTTPServer.test(HandlerClass=RangeRequestHandler, port=args.port)
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"> </link> <link rel="stylesheet" type="text/css" href="https://bootswatch.com/4/darkly/bootstrap.css"> </link> </head> <body> <div class="jumbotron"> <h1 class="display-4">Hello world!</h1> <p class="lead">This illustrates the power of Bootstrap.</p> <hr class="my-4"> <p>It allows you to quickly create beautiful pages.</p> <a class="btn btn-primary btn-lg" href="#" role="button">Tell me more</a> </div> </html> """ class RequestHandler(server.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(TEMPLATE.encode("utf-8")) server.test(RequestHandler, server.HTTPServer, port=8001, bind="0.0.0.0")
import argparse from http.server import HTTPServer, SimpleHTTPRequestHandler, test class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers (self): self.send_header('Access-Control-Allow-Origin', '*') SimpleHTTPRequestHandler.end_headers(self) if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--bind', '-b', default='', metavar='ADDRESS', help='Specify alternate bind address ' '[default: all interfaces]') parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() test(HandlerClass=CORSRequestHandler, port=args.port, bind=args.bind)
#! /usr/bin/env python # Usage: python __file__.py <port> from http.server import SimpleHTTPRequestHandler, HTTPServer, test class CORSRequestHandler(SimpleHTTPRequestHandler): def do_OPTIONS(self): self.send_response(200, 'OK') self.end_headers() def end_headers(self): self.send_header('Access-Control-Allow-Origin', '*') self.send_header('Access-Control-Allow-Headers', 'x-request-timestamp, x-signature, electricitymap-token') self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate') self.send_header('Pragma', 'no-cache') self.send_header('Expires', '0') SimpleHTTPRequestHandler.end_headers(self) if __name__ == '__main__': test(CORSRequestHandler, HTTPServer)
def main(argv): global FILE, INDEX FILE = open(argv[1], 'rb') if not INDEX: INDEX = open(os.path.splitext(argv[0])[0] + '.html').read() server.test(HandlerClass=HTTPRequestHandler)
#!/usr/bin/env python from http import server class MyHTTPRequestHandler(server.SimpleHTTPRequestHandler): def end_headers(self): self.send_my_headers() server.SimpleHTTPRequestHandler.end_headers(self) def send_my_headers(self): self.send_header("Cache-Control", "no-cache, no-store, must-revalidate") self.send_header("Pragma", "no-cache") self.send_header("Expires", "0") if __name__ == '__main__': server.test(HandlerClass=MyHTTPRequestHandler)
Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application/octet-stream as a default; however it would be permissible (if slow) to look inside the data to make a better guess. """ base, ext = posixpath.splitext(path) if ext in self.extensions_map: return self.extensions_map[ext] ext = ext.lower() if ext in self.extensions_map: return self.extensions_map[ext] else: return self.extensions_map[''] if not mimetypes.inited: mimetypes.init() # try to read system mime.types extensions_map = mimetypes.types_map.copy() extensions_map.update({ '': 'application/octet-stream', # Default '.py': 'text/plain', '.c': 'text/plain', '.h': 'text/plain', }) if __name__ == '__main__': test(FSS, HTTPServer)
#!/usr/bin/env python3 from http.server import HTTPServer, SimpleHTTPRequestHandler, test import sys class CORSRequestHandler(SimpleHTTPRequestHandler): def end_headers(self): self.send_header('Access-Control-Allow-Origin', '*') SimpleHTTPRequestHandler.end_headers(self) if __name__ == '__main__': test(CORSRequestHandler, HTTPServer, port=8000)
except ImportError: from SimpleHTTPServer import SimpleHTTPRequestHandler import BaseHTTPServer class CORSRequestHandler( SimpleHTTPRequestHandler ): ''' This class provides an example server to allow local requests for the data.json file in this output area demo. Making local file requests is not/should not be possible using the XmlHttpRequest handler (for security reasons); in general javascript that fetches data from a different domain is frowned upon and is legitimately a security concern, hence XSS (cross-site scripting) is not allowed. The solution to this is to allow CORS (cross-origin resource sharing), which allows the remote data to be fetched, but with much fewer security concerns than XSS. CORS is enabled server-side (there's nothing the client can do to request it, obviously) by adding the 'Access-Control-Allow-Origin' (ACAO) header to the http response. ''' def end_headers( self ): self.send_header( 'Access-Control-Allow-Origin', '*' ) SimpleHTTPRequestHandler.end_headers(self) if __name__ == '__main__': BaseHTTPServer.test( CORSRequestHandler, BaseHTTPServer.HTTPServer )
def main(): class PoolHTTPServer(PoolMixIn, HTTPServer): pool = ThreadPoolExecutor(max_workers=40) test(HandlerClass=SimpleHTTPRequestHandler, ServerClass=PoolHTTPServer)
def test(): if sys.version_info[0] >= 3: http_server.test(RequestHandler, port=0) else: http_server.test(RequestHandler)
def test_server_test_ipv4(self, _): for bind in self.ipv4_addrs: mock_server = self.mock_server_class() server.test(ServerClass=mock_server, bind=bind) self.assertEqual(mock_server.address_family, socket.AF_INET)
self.send_header('Content-Length', str(response_length)) self.send_header('Last-Modified', self.date_time_string(fs.st_mtime)) self.end_headers() return f def copyfile(self, source, outputfile): if not self.range: return SimpleHTTPRequestHandler.copyfile(self, source, outputfile) # SimpleHTTPRequestHandler uses shutil.copyfileobj, which doesn't let # you stop the copying before the end of the file. start, stop = self.range # set in send_head() copy_byte_range(source, outputfile, start, stop) if __name__ == '__main__': if sys.version_info[0] == 2: http_server.test(HandlerClass=RangeRequestHandler) # Python2's SimpleHTTPServer.test doesn't support bind and port args else: parser = argparse.ArgumentParser() parser.add_argument('--bind', '-b', default='', metavar='ADDRESS', help='Specify alternate bind address ' '[default: all interfaces]') parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() http_server.test(HandlerClass=RangeRequestHandler, port=args.port, bind=args.bind)
The port number is optional, defaulting to 8000. Once the server is up and running with a swagger.json file in the same directory, then the url (assuming port 8000) to be used in swagger-ui would be: http://localhost:8000/swagger.json """ from http.server import HTTPServer, SimpleHTTPRequestHandler, test import sys class CORSRequestHandler(SimpleHTTPRequestHandler): """ Allows a simple HTTP server to have CORS enabled by default """ def end_headers(self): self.send_header("Access-Control-Allow-Origin", "*") SimpleHTTPRequestHandler.end_headers(self) if __name__ == "__main__": if len(sys.argv) > 1: # Allows the port to be passed in as an argument port = sys.argv[-1] else: port = 8000 test(CORSRequestHandler, HTTPServer, port=port)
#!/usr/bin/env python # encoding: utf-8 from http import server class TRACEServer(server.CGIHTTPRequestHandler): def do_TRACE(self): self.send_response(200, "Request follows") self.end_headers() self.wfile.write(self.raw_requestline) self.wfile.write(self.headers) def do_OPTIONS(self): self.send_response(200, "OK") self.send_header("Access-Control-Allow-Origin", "*") self.send_header("Access-Control-Allow-Credentials", "true") self.end_headers() if __name__ == '__main__': server.test(HandlerClass=TRACEServer, port=8000)
def test(): if sys.version_info[0] >= 3: http_server.test(RequestHandler, port=0) else: http_server.test(RequestHandler)
#/usr/bin/env python3 import argparse from http import server as httpserver class TestingRequestHandler(httpserver.SimpleHTTPRequestHandler): def translate_path(self, path): if not path.startswith("/static/"): if path == "/": path = "/global" path = "/out" + path + ".html" return super(TestingRequestHandler, self).translate_path(path) if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('port', action='store', default=8000, type=int, nargs='?', help='Specify alternate port [default: 8000]') args = parser.parse_args() handler_class = TestingRequestHandler httpserver.test(HandlerClass=handler_class, port=args.port)