On this page |
Note
This class is accessible using both hwebserver.WebServerRequest
and
hwebserver.Request
, but hwebserver.Request
is preferable.
This object is passed to your registered URL handler function as a container for information from the client request.
-
To get a dictionary of the URL arguments, call
request.GET()
. -
To get a dictionary of form arguments (in a POST request), call
request.POST()
. -
To get the full path of the request, call
request.path()
. -
If the request is a file upload, you can get a dict mapping file names to file contents using
request.files()
.
Upload example ¶
import tempfile import hwebserver import hou @hwebserver.urlHandler("/blur_image") def blur_image(request): if request.method() == "GET": return hwebserver.Response(''' <p>Upload an image</p> <form method="POST" enctype="multipart/form-data"> <input type="file" name="image_file"> <input type="submit"> </form> ''') if request.method() != "POST": return hwebserver.notFoundResponse(request) image_file = request.files().get("image_file") if image_file is None: return hwebserver.errorResponse(request, "No image was posted", 422) image_file.saveToDisk() # Use a COP2 network to load the image, blur it, and write it to a # temporary output file. cop2net = hou.node("/img").createNode("img") file_cop2 = cop2net.createNode("file") file_cop2.parm("filename1").set(image_file.temporaryFilePath()) blur_cop2 = cop2net.createNode("blur") blur_cop2.setFirstInput(file_cop2 blur_cop2.parm("blursize").set(10) rop = cop2net.createNode("rop_comp") rop.setFirstInput(blur_cop2) rop.parmTuple("f").set((1, 1, 1)) temp_output_file = tempfile.mkstemp(".jpg")[1] rop.parm("copoutput").set(temp_output_file) rop.render() cop2net.destroy() return hwebserver.fileResponse(temp_output_file, delete_file=True) hwebserver.run(8008, True)
Methods ¶
method()
→ str
Returns the HTTP method used in the request, such as "GET"
, "POST"
,
"PATCH"
, etc.
path()
→ str
Returns the path that was requested on the server. For example, if
the URI is "http://127.0.0.1:8008/asset/torus?divisions=20&tx=2"
,
the path would be "/asset/torus"
.
pathWithQueryString()
→ str
Returns the path that was requested on the server, including the
query string part. For example, if
the URI is "http://127.0.0.1:8008/asset/torus?divisions=20&tx=2"
,
the result would be "/asset/torus?divisions=20&tx=2"
.
Note that the contents of the query string in this example are also
accessible via request.GET()["divisions"]
and request.GET()["tx"]
.
queryString()
→ str
Returns the query string in the request, not including the leading ?
.
For example, if
the URI is "http://127.0.0.1:8008/asset/torus?divisions=20&tx=2"
,
the result would be "divisions=20&tx=2"
. If the URI is
"http://127.0.0.1:8008/asset/torus"
the result would be ""
.
absoluteURI(location=None)
→ str
If location
is None
, returns the full URI used to make the current
request. Otherwise, location
is a string containing a path
on the server, and returns a full URI to that path.
This method is used to build Location
headers for
hwebserver.redirect.
isSecure()
→ bool
Returns True
if the request was made using https instead of just http.
headers()
→ dict
of str
to str
Returns a dictionary of the headers provided with the request.
Calling this method generates a new copy of the dictionary each time, so you are advised to avoid calling it more times than necessary.
Note that cookies will parse the contents
of the Cookie
header.
The web server does not provide support for authentication, but you could
add it, for example, by creating decorators that inspect the contents of
the Authorization
header.
cookies()
→ dict
of str
to str
Returns the parsed cookie data from the Cookie
header sent in the request.
Calling this method generates a new copy of the dictionary each time, so you are advised to avoid calling it more times than necessary.
GET()
→ dict
of str
to str
Returns a dictionary of the parsed query string variables provided in the URL of the request.
Calling this method generates a new copy of the dictionary each time, so you are advised to avoid calling it more times than necessary.
If you need to support multiple GET values with the same key, you can use
Python’s parse_qs
function to parse the query string and extract them.
try: from urllib.parse import parse_qs except Importerror: from urlparse import parse_qs `parse_qs(request.queryString())`
POST()
→ dict
of str
to str
Returns a dictionary of the parsed query variables provided in the body of the request. The server can receive URL-encoded and multipart/form-data encoded values.
Calling this method generates a new copy of the dictionary each time, so you are advised to avoid calling it more times than necessary.
Note that files in multipart/form-data do not show up in this dictionary, are are instead accessible via files.
files()
→ dict
of str
to hwebserver.UploadedFile
Returns a dictionary of the file uploads passed in multipart/form-data. If these file uploads were large they may have been saved to disk instead of being stored in memory. See hwebserver.UploadedFile for more information.
Calling this method generates a new copy of the dictionary each time, so you are advised to avoid calling it more times than necessary.
port()
→ int
Returns the server port the request came from.
body()
→ str
Returns the fully unparsed body of the request.
Calling this method generates a new copy of the string each time, so you are advised to avoid calling it more times than necessary.
contentLength()
→ str
Returns the content length header. If none is present an empty string is returned. This is meant as a convenience method to going through the headers directly.
contentType()
→ str
Returns the content type header. If none is present an empty string is returned. This is meant as a convenience method to going through the headers directly.
host()
→ str
Returns the host found in the request host header. If none is found then an empty string is returned.
protocol()
→ str
Returns the protocol used for the request. An example would be HTTP/1.1
for an http 1.1 request.