76. plugins.http_server
— Local http server and html file viewer¶
This module provides functionality to view a local html file in the browser using the ‘http:’ transport mechanism instead of ‘file:’. It was created to allow viewing WebGL models from a local directory.
76.1. Classes defined in module plugins.http_server¶
- class plugins.http_server.HttpServer(path, port=None)[source]¶
A specialized http server to serve local files.
This server is intended to serve local files to a browser. It is meant as a replacement for the ‘file:’ transport mechanism. For security reasons modern browsers often do not allow to include files (especially script types) from another origin. With the file: protocol any other file, even in the same directory, may be considered as a foreign origin. A CORS error is raised in such cases.
The solution is to use a local http server and access the files over ‘http:’ protocol. The HttpServer is very lightweight class which can serve a directory and all its files and subdirectories to the local machine. It is not intended to be exposed directly to the network. It uses the
http.server
from the Python standard library.- Parameters:
path (path_like) – The path of the local directory to be served. The user should have read access to this directory.
port (int | None) – The TCP port on which the server will be listening. This should be an unused port number in the high rang (>= 1024). If not provided, a random free port number will be used.
Every successfully created HttpServer is registered by adding it to the list HttpServer._servers. When pyFormex exits, all these servers will be topped. The user can stop a server at any time though. If you want a server to continue after pyFormex exits, remove it from the list. The following attributes of the HttpServer provide useful information:
- path:
Path
The path of the directory with accessible files.
- port: int:
The port number on which the server is listening. In your browser, use
http://localhost:PORT/SOMEFILE
to view the contents of SOMEFILE.- P:
subprocess.Popen
The Popen instance of the running server. Its attribute P.pid gives the process id of the server.
- connect(url='', browser=None)[source]¶
Show an url in the browser.
- Parameters:
url (path_like) – The path of the file to be shown in the browser. The path is relative to the served directory path. An empty string or a single ‘/’ will serve the directory itself, showing the contents of the directory.
browser (str) – The name of the browser command. If not provided, the value from the settings is used. It can be configured in the Settings menu.
76.2. Functions defined in module plugins.http_server¶
- plugins.http_server.get_free_socket()[source]¶
Find and return a random free port number.
A random free port number in the upper range 1024-65535 is found. The port is immediately bound with the reuse option set. This avoids a race condition (where another process could bind to the port before we had the change to do so) while still keeping the port bindable for our purpose.
- plugins.http_server.showHtml(path)[source]¶
Show a local .html file in the browser.
Creates a local web server (
HttpServer
) to serve an html file over the http: protocol to a browser on the local machine. The browser command is configurable in the settings.This is a convenient wrapper function if you have a single file to show. If you need to show multiple files from the same directory, you may want to create a single
HttpServer
for the directory and use multiple calls to itsconnect()
method.- Parameters:
path (path_like) – The path of the file to be displayed. This should normally be a file with suffix
.html
.