Skip to content

fileserver

ngtools.local.fileserver

Local fileserver to access local files through HTTP.

This module implements local file servers, that can be used to server local files to a local neuroglancer instance.

Classes:

Name Description
LocalFileServer

A fileserver that serves local files

LocalFileServerInBackground

A fileserver that runs in a background process

LocalFileServer

LocalFileServer(port=0, ip='localhost', app=None, start=True)

A fileserver that serves local files.

All paths should be absolute!

Parameters:

Name Type Description Default
port int

Port number to use

0
ip str

IP address

'localhost'
app callable

Application

None
start bool

Start straight away.

True

get_url

get_url(regular=True)

URL of the fileserver.

start

start()

Start server and server forever.

is_running

is_running()

Check if server is running.

stop

stop()

Shutdown server.

Application

Application(handlers=None)

An application.

__call__

__call__(environ, start_response)

Serve by deferring to appropriate handlers.

make_response classmethod

make_response(start_response, handler)

Make WSGI response.

Status

Bases: str

Well formatted HTTP status.

Environ dataclass

Environ(_environ=None, method=None, app=None, path=None, query=None, content_type=None, content_length=None, server_name=None, server_port=None, server_protocol=None, headers=Headers())

WSGI environ, as a dataclass.

method class-attribute instance-attribute

method = None

The HTTP request method, such as GET or POST. This cannot ever be an empty string, and so is always required.

app class-attribute instance-attribute

app = None

The initial portion of the request URL's "path" that corresponds to the application object, so that the application knows its virtual "location". This may be an empty string, if the application corresponds to the root" of the server.

path class-attribute instance-attribute

path = None

The remainder of the request URL's "path", designating the virtual “location” of the request's target within the application. This may be an empty string, if the request URL targets the application root and does not have a trailing slash.

query class-attribute instance-attribute

query = None

The portion of the request URL that follows the "?", if any. May be empty or absent.

content_type class-attribute instance-attribute

content_type = None

The contents of any Content-Type fields in the HTTP request. May be empty or absent.

content_length class-attribute instance-attribute

content_length = None

The contents of any Content-Length fields in the HTTP request. May be empty or absent.

server_port class-attribute instance-attribute

server_port = None

When combined with SCRIPT_NAME and PATH_INFO, these variables can be used to complete the URL. Note, however, that HTTP_HOST, if present, should be used in preference to SERVER_NAME for reconstructing the request URL. See the URL Reconstruction section below for more detail. SERVER_NAME and SERVER_PORT can never be empty strings, and so are always required.

server_protocol class-attribute instance-attribute

server_protocol = None

The version of the protocol the client used to send the request. Typically this will be something like "HTTP/1.0" or "HTTP/1.1" and may be used by the application to determine how to treat any HTTP request headers. (This variable should probably be called REQUEST_PROTOCOL, since it denotes the protocol used in the request, and is not necessarily the protocol that will be used in the server's response. However, for compatibility with CGI we have to keep the existing name.)

headers class-attribute instance-attribute

headers = Headers()

Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.

from_environ classmethod

from_environ(environ)

Convert environ dictionary to Environ object.

to_environ

to_environ()

Convert Environ object to environ dictionary.

request_uri

request_uri()

Return the full request URI, optionally including the query string, using the algorithm found in the “URL Reconstruction” section of PEP 3333. If include_query is false, the query string is not included in the resulting URI.

shift_path

shift_path()

Shift a single name from PATH_INFO to SCRIPT_NAME and return the name. The environ dictionary is modified in-place; use a copy if you need to keep the original PATH_INFO or SCRIPT_NAME intact.

If there are no remaining path segments in PATH_INFO, None is returned.

Handler

Handler(app, environ=None, **kwargs)

Class that handles requests.

status property writable

status

Status, as a well formatted string.

initialize

initialize(**kwargs)

Initialize handler.

prepare

prepare()

Perform steps before the main request.

on_finish

on_finish()

Perform steps after the main request.

send_error

send_error(status, msg)

Make the returned request an error request.

StaticFileHandler

StaticFileHandler(app, environ=None, **kwargs)

Bases: Handler

Serves static files.

status property writable

status

Status, as a well formatted string.

initialize

initialize(**kwargs)

Initialize handler.

prepare

prepare()

Perform steps before the main request.

on_finish

on_finish()

Perform steps after the main request.

send_error

send_error(status, msg)

Make the returned request an error request.

ErrorHandler

ErrorHandler(app, code, msg=None)

Bases: Handler

Mock handler for HTTP errors.

status property writable

status

Status, as a well formatted string.

initialize

initialize(**kwargs)

Initialize handler.

prepare

prepare()

Perform steps before the main request.

on_finish

on_finish()

Perform steps after the main request.

send_error

send_error(status, msg)

Make the returned request an error request.

MethodNotAllowedHandler

MethodNotAllowedHandler(app, msg=None)

Bases: ErrorHandler

Mock handler for error 405.

status property writable

status

Status, as a well formatted string.

initialize

initialize(**kwargs)

Initialize handler.

prepare

prepare()

Perform steps before the main request.

on_finish

on_finish()

Perform steps after the main request.

send_error

send_error(status, msg)

Make the returned request an error request.

ServerErrorHandler

ServerErrorHandler(app, msg=None)

Bases: ErrorHandler

Mock handler for error 500.

status property writable

status

Status, as a well formatted string.

initialize

initialize(**kwargs)

Initialize handler.

prepare

prepare()

Perform steps before the main request.

on_finish

on_finish()

Perform steps after the main request.

send_error

send_error(status, msg)

Make the returned request an error request.

FileNotFoundHandler

FileNotFoundHandler(app, msg=None)

Bases: ErrorHandler

Mock handler for error 405.

status property writable

status

Status, as a well formatted string.

initialize

initialize(**kwargs)

Initialize handler.

prepare

prepare()

Perform steps before the main request.

on_finish

on_finish()

Perform steps after the main request.

send_error

send_error(status, msg)

Make the returned request an error request.