HSL 2.0 framework
Description of the framework
Public Member Functions |List of all members
hsl20_4_http_server.hsl20_4_http_server.Server Class Reference

All methods of the HTTP server More

..

Public Member Functions

def__init__ (self, framework, context_map)
Constructor. More...
defset_authentication (self, user=None, pw=None, digest=False)
Sets the HTTP authentication: More...
defset_address (self, ip, port)
Sets the IP address and IP port. More...
defset_error_callback (self, callback)
Sets a callback method in case of an error. More...
defset_max_post_size (self, size)
Sets the allowed size of the body for POST/PUT calls. More...
defset_logger (self, logger)
Assigns an hsl20_4.hsl20_4.Logger instance to the server. More...
defstart_server (self)
Starts the server. More...
defregister_uri (self, method, path, callback)
Registers a URI on the server. More...
defset_server_name (self, name)
Sets the server name. More...

Detailed Description

All methods of the HTTP server

Deprecated:
This class should no longer be used

Provides a server service for communication via HTTP.

If the HTTP server was generated by the framework and bound to an IP and port, it can be started
If calls are now made by a client, there are two possibilities:

After processing the respective callback method, the server sends the generated response to the remote terminal. If no status code is set in the response object, the server answers with status code 501

Note
The class works asynchronously. None of the methods offered are blocked.

HTTP-Request-Methoden

When registering a URI, one or more HTTP methods (by logical OR, e.g. hsl20_4_http_server.GET|hsl20_4_http_server.PUT) can be defined directly, alternatively you can also assign hsl20_4_http_server.ALL, then the callback method will be called for each method. The HTTP server provides direct support for the following methods:

With the methods POST and PUT a body element is supported in the request. For this purpose the field "Content-length" must be set in the header. If the client exceeds the maximum allowed size, the server will disconnect the connection
See also hsl20_4_http_server.Server::set_max_post_size

Timeouts

Please note the following timeouts, when they occur, the connection will be disconnected:

Authentifizierung

It is possible to secure access to the server with user/password. Authentication can be enabled and disabled using the method hsl20_4_http_server.Server.set_authentication. The following two methods are available for transmitting user data:

HA1 = MD5(USERNAME + ":" + REALM + ":" + PASSWORD) HA2 = MD5(UPPERCASE(HTTP_METHOD) + ":" + URI) RESULT = MD5(HA1 + ":" + NONCE_SERVER + ":" + NONCE_COUNTER + ":" + NONCE_CLIENT + ":auth:" + HA2)

Note
Only one user/password is supported
If authentication is enabled, this affects all accesses (paths) to the server.

Code-Beispiel

The use of this class by means of a short source code excerpt

class my_demo_module(hsl20.BaseModule):
...
def start_my_server(self):
if self.server == None:
self.server = self.FRAMEWORK.create_http_server()
self.server.set_address("" , 8080)
self.server.set_authentication("user" , "pw" , 1)
self.server.register_uri(hsl20_4_http_server.GET, "/" , self.on_index_page)
self.server.start_server()
def on_index_page(self, request, response):
if request.get_method() == "GET" :
response.set_status_code(200)
response.set_header("Content-type" , "text/html" )
response.set_body("<html><body>Hello World!</body></html>" )

Constructor & Destructor Documentation

◆ __init__()

def hsl20_4_http_server.hsl20_4_http_server.Server.__init__ ( self,
framework,
context_map
)

Constructor

Warning
This class should not be instantiated directly

Member Function Documentation

◆ register_uri()

def hsl20_4_http_server.hsl20_4_http_server.Server.register_uri ( self,
method,
path,
callback
)

Registers a URI on the server

If this URI is called by a client, the transferred callback is executed

Parameters
methodint
HTTP retrieval methods. OR multiple options per URI are supported. The following constants are available:
  • hsl20.framework.http_server.GET
  • hsl20.framework.http_server.POST
  • hsl20.framework.http_server.PUT
  • hsl20.framework.http_server.DELETE
  • hsl20.framework.http_server.ALL
pathstring
Path (e.g.: /index)
callbackfunction
Callback. The method passed here requires the following parameters

◆ set_address()

def hsl20_4_http_server.hsl20_4_http_server.Server.set_address ( self,
ip,
port
)

Sets the IP address and IP port

Parameters
ipstring
IP address to which the server is bound. An empty string binds the server to all network addresses of the HS/FS
portint
IP port under which the server can be reached

◆ set_authentication()

def hsl20_4_http_server.hsl20_4_http_server.Server.set_authentication ( self,
user = None,
pw = None,
digest = False
)

Sets the HTTP authentication

  • If both parameters are supplied, HTTP authentication is enabled, otherwise it is disabled.

Authentication counts for all registered URIs

Parameters
userstring
User
pwstring
Password
digestbool
Optional. If TRUE, "Digest Access Authentication" is used as the authentication mechanism, otherwise "Basic Authentication" is used
From version 2.0.3:
Parameter pw is now correctly specified in the documentation

◆ set_error_callback()

def hsl20_4_http_server.hsl20_4_http_server.Server.set_error_callback ( self,
callback
)

Sets a callback method in case of an error

Called when the client calls a URL not registered with register_uri. Evaluated by the HTTP server after this method is completed.

Parameters
callbackfunction
The callback method requires the following parameters:
  • error code int
    Error code that describes the error that occurred.
  • request string
    HTTP request object that provides all information regarding the HTTP request. Is generated by the HTTP server.
  • response string
    HTTP Response Object, which must contain all information related to the HTTP response

◆ set_logger()

def hsl20_4_http_server.hsl20_4_http_server.Server.set_logger ( self,
logger
)

Assigns a hsl20_4.hsl20_4.Logger instance to the server

Parameters
loggerhsl20_4.logger
Logger object

◆ set_max_post_size()

def hsl20_4_http_server.hsl20_4_http_server.Server.set_max_post_size ( self,
size
)

Sets the allowed size of the body for POST/PUT calls

If the value is exceeded, the call is aborted.

Default value: 1,000,000 bytes
Maximum permissible value (MAX VALUE): 10,000,000 bytes

Parameters
sizeint
Size in bytes. If size > MAXVALUE, size = MAXVALUE is set!

◆ set_server_name()

def hsl20_4_http_server.hsl20_4_http_server.Server.set_server_name ( self,
name
)

Sets the server name

If set, this value is used as the default value for the 'Server' field in an HTTP request. The value set here can be overwritten with response.set_header. If no value is specified by set_server_name and no value is specified by response.set_header, the context is transmitted as server name

Parameters
namestring
New server name

◆ start_server()

def hsl20_4_http_server.hsl20_4_http_server.server.start_server ( self)

Starts the server

Exceptions
AttributeErrorIs triggered if no valid address is specified

The documentation for this class was generated from the following file: