Mercurial > repos > fubar > jbrowse2
comparison jb2_webserver.py @ 48:460d5b6c5d98 draft
planemo upload for repository https://github.com/usegalaxy-eu/temporary-tools/tree/master/jbrowse2 commit 3a43e9e0ffce0966101203102e769d1ced28618a-dirty
| author | fubar |
|---|---|
| date | Wed, 06 Mar 2024 10:37:49 +0000 |
| parents | 4181e97c70a7 |
| children | f350467f9433 |
comparison
equal
deleted
inserted
replaced
| 47:3e53204c2419 | 48:460d5b6c5d98 |
|---|---|
| 36 import argparse | 36 import argparse |
| 37 import functools | 37 import functools |
| 38 import os | 38 import os |
| 39 import re | 39 import re |
| 40 import socketserver | 40 import socketserver |
| 41 import ssl | |
| 41 import webbrowser | 42 import webbrowser |
| 42 from http.server import SimpleHTTPRequestHandler | 43 from http.server import SimpleHTTPRequestHandler |
| 43 | 44 |
| 44 | 45 |
| 45 DEFAULT_PORT = 8081 | 46 DEFAULT_PORT = 8443 |
| 46 | 47 |
| 47 | 48 |
| 48 def copy_byte_range(infile, outfile, start=None, stop=None, bufsize=16 * 1024): | 49 def copy_byte_range(infile, outfile, start=None, stop=None, bufsize=16 * 1024): |
| 49 """Like shutil.copyfileobj, but only copy a range of the streams. | 50 """Like shutil.copyfileobj, but only copy a range of the streams. |
| 50 | 51 |
| 151 | 152 |
| 152 | 153 |
| 153 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): | 154 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): |
| 154 allow_reuse_address = True | 155 allow_reuse_address = True |
| 155 | 156 |
| 157 def server_bind(self): | |
| 158 ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) | |
| 159 ctx.load_cert_chain('cert.pem', 'key.pem') | |
| 160 socketserver.TCPServer.server_bind(self) | |
| 161 self.socket = ctx.wrap_socket( self.socket, server_hostname = "127.0.0.1", server_side=False,) | |
| 162 | |
| 163 | |
| 164 def get_request(self): | |
| 165 (socket, addr) = socketserver.TCPServer.get_request(self) | |
| 166 socket.do_handshake() | |
| 167 return (socket, addr) | |
| 156 | 168 |
| 157 if __name__ == "__main__": | 169 if __name__ == "__main__": |
| 158 parser = argparse.ArgumentParser( | 170 parser = argparse.ArgumentParser( |
| 159 description="Tiny Python Web Server supporting range requests, for local viewing of unzipped Galaxy JBrowse2 configurations" | 171 description="Tiny Python Web Server supporting range requests, for local viewing of unzipped Galaxy JBrowse2 configurations" |
| 160 ) | 172 ) |
| 169 default=DEFAULT_PORT, | 181 default=DEFAULT_PORT, |
| 170 help=f"Port to listen on (default: {DEFAULT_PORT})", | 182 help=f"Port to listen on (default: {DEFAULT_PORT})", |
| 171 ) | 183 ) |
| 172 parser.add_argument( | 184 parser.add_argument( |
| 173 "--bind", | 185 "--bind", |
| 174 default="0.0.0.0", | 186 default="127.0.0.1", |
| 175 help="IP address to bind to (default: 0.0.0.0)", | 187 help="IP address to bind to (default: 127.0.0.1)", |
| 176 ) | 188 ) |
| 177 args = parser.parse_args() | 189 args = parser.parse_args() |
| 178 | 190 |
| 179 handler = functools.partial(RangeRequestHandler, directory=args.root) | 191 handler = functools.partial(RangeRequestHandler, directory=args.root) |
| 180 | 192 |
| 181 webbrowser.open(f"http://{args.bind}:{args.port}") | 193 webbrowser.open(f"https://{args.bind}:{args.port}") |
| 182 | 194 |
| 183 with ThreadedTCPServer((args.bind, args.port), handler) as httpd: | 195 with ThreadedTCPServer((args.bind, args.port), handler) as httpd: |
| 184 print( | 196 print( |
| 185 f"Serving HTTP on {args.bind} port {args.port} (http://{args.bind}:{args.port}/)" | 197 f"Serving HTTPS on {args.bind} port {args.port} (https://{args.bind}:{args.port}/)" |
| 186 ) | 198 ) |
| 187 httpd.serve_forever() | 199 httpd.serve_forever() |
