comparison jb2_webserver.py @ 50:f350467f9433 draft

planemo upload for repository https://github.com/usegalaxy-eu/temporary-tools/tree/master/jbrowse2 commit 44df75b3714aa9e02983e0b67ef43fc0eee4a8d4
author fubar
date Thu, 07 Mar 2024 04:51:43 +0000
parents 460d5b6c5d98
children
comparison
equal deleted inserted replaced
49:7e867ff86e44 50:f350467f9433
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
42 import webbrowser 41 import webbrowser
43 from http.server import SimpleHTTPRequestHandler 42 from http.server import SimpleHTTPRequestHandler
44 43
45 44
46 DEFAULT_PORT = 8443 45 DEFAULT_PORT = 8081
47 46
48 47
49 def copy_byte_range(infile, outfile, start=None, stop=None, bufsize=16 * 1024): 48 def copy_byte_range(infile, outfile, start=None, stop=None, bufsize=16 * 1024):
50 """Like shutil.copyfileobj, but only copy a range of the streams. 49 """Like shutil.copyfileobj, but only copy a range of the streams.
51 50
152 151
153 152
154 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): 153 class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
155 allow_reuse_address = True 154 allow_reuse_address = True
156 155
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)
168 156
169 if __name__ == "__main__": 157 if __name__ == "__main__":
170 parser = argparse.ArgumentParser( 158 parser = argparse.ArgumentParser(
171 description="Tiny Python Web Server supporting range requests, for local viewing of unzipped Galaxy JBrowse2 configurations" 159 description="Tiny Python Web Server supporting range requests, for local viewing of unzipped Galaxy JBrowse2 configurations"
172 ) 160 )
182 help=f"Port to listen on (default: {DEFAULT_PORT})", 170 help=f"Port to listen on (default: {DEFAULT_PORT})",
183 ) 171 )
184 parser.add_argument( 172 parser.add_argument(
185 "--bind", 173 "--bind",
186 default="127.0.0.1", 174 default="127.0.0.1",
187 help="IP address to bind to (default: 127.0.0.1)", 175 help="IP address to bind to (default: 127.0.0.1 - use 0.0.0.0 to allow access on your network)",
188 ) 176 )
189 args = parser.parse_args() 177 args = parser.parse_args()
190 178
191 handler = functools.partial(RangeRequestHandler, directory=args.root) 179 handler = functools.partial(RangeRequestHandler, directory=args.root)
192 180
193 webbrowser.open(f"https://{args.bind}:{args.port}") 181 webbrowser.open(f"http://{args.bind}:{args.port}")
194 182
195 with ThreadedTCPServer((args.bind, args.port), handler) as httpd: 183 with ThreadedTCPServer((args.bind, args.port), handler) as httpd:
196 print( 184 print(
197 f"Serving HTTPS on {args.bind} port {args.port} (https://{args.bind}:{args.port}/)" 185 f"Serving HTTP on {args.bind} port {args.port} (http://{args.bind}:{args.port}/)"
198 ) 186 )
199 httpd.serve_forever() 187 httpd.serve_forever()