webpie


Namewebpie JSON
Version 5.16.3 PyPI version JSON
download
home_pagehttps://webpie.github.io/
SummaryA set of useful tools built on top of standard Python threading module
upload_time2023-06-29 18:35:36
maintainer
docs_urlNone
authorIgor Mandrichenko
requires_python
licenseBSD 3-clause
keywords web service wsgi web application
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
WebPie
======

WebPie (another way of spelling web-py) is a web application development framework for Python based on the WSGI standard.
WebPie makes it simple to develop thread-safe object-oriented web applications.

Hello World in WebPie
---------------------

Here is the simplest WebPie application you can write:

.. code-block:: python

	# hello_world.py

	from webpie import WPApp, WPHandler		

	class MyHandler(WPHandler):                         # 1

	    def hello(self, request, relpath):              # 2
	        return "Hello, World!\n"                    # 3

	application = WPApp(MyHandler)                      # 4


Let's go over the code line by line:

1. We created class MyHandler, which will handle HTTP requests. In order to work with WebPie, it has to be a subclass of WPHandler class.

2. We defined one web method "hello", which will be called when a URL like http://host.org/hello is requested.

3. It will always return text "Hello, World!".

4. Finally, we created a WSGI application as an instance of WPApp class, passing it the MyHandler class as an argument.

Now we can plug our application into any WSGI framework such as uWSGI or Apache httpd, e.g.:

.. code-block:: bash

	$ uwsgi --http :8080 --wsgi-file hello_world.py


and try it:

.. code-block:: bash

	$ curl http://localhost:8080/hello
	Hello world!
	$ 


If you do not want to use uWSGI or similar framework, you can use WebPie's own HTTP server to put your application on the web:

.. code-block:: python

	# hello_world_server.py
	from webpie import WPApp, WPHandler, run_server
	import time

	class MyHandler(WPHandler):						

		def hello(self, request, relpath):				
			return "Hello, World!\n"					

	application = WPApp(MyHandler)
	application.run_server(8080)        # run the server at port 8080

.. code-block:: bash

	$ python hello_world_server.py &
	$ curl http://localhost:8080/hello
	Hello world!
    $

URL Structure
-------------
Notice that MyHandler class has single method "hello" and it maps to the URL path "hello". This is general rule in WebPie - methods of handler classes map one to one to the elements of URI path. For example, we can add another method to our server called "time":

.. code-block:: python

	from webpie import WPApp, WPHandler
	import time

	class MyHandler(WPHandler):						

		def hello(self, request, relpath):				
			return "Hello, World!\n"					

		def time(self, request, relpath):
			return time.ctime()+"\n"

	application = WPApp(MyHandler)
	application.run_server(8080)

Now our handler can handle 2 types of requests, it can say hello and it can tell local time:

.. code-block:: bash

	$ curl http://localhost:8080/hello
	Hello, World!
	$ curl http://localhost:8080/time
	Sun May  5 06:47:15 2019
	$ 

Notice that handler methods names automatically become parts of the URL path.

If you want to split your handler into different classes to organize your code better, you can have nested handler classes in your application. For example, we may want to have one handler which focuses on reporting time and the other which says hello:

.. code-block:: python

	# time_hello_split.py
	from webpie import WPApp, WPHandler
	import time

	class HelloHandler(WPHandler):						

		def hello(self, request, relpath):				
			return "Hello, World!\n"					

	class ClockHandler(WPHandler):						

		def time(self, request, relpath):			
			return time.ctime()+"\n", "text/plain"	

	class TopHandler(WPHandler):

		def __init__(self, *params):
			WPHandler.__init__(self, *params)
			self.greet = HelloHandler(*params)
			self.clock = ClockHandler(*params)


	application = WPApp(TopHandler)
	application.run_server(8080)


WebPie application is given top handler class as an argument. It will create the handler instances one per each
web request. Top handler can create child handlers recursively. This recirsive handler structure maps one-to-one to the URL structure. The URI is simply the path from the top handler through its child handlers to the method of one of them:

.. code-block:: bash

	$ curl http://localhost:8080/greet/hello
	Hello, World!
	$ curl http://localhost:8080/clock/time
	Sun May  5 06:49:14 2019
	$ 

For example, to find the method for URI "/greet/hello", WebPie starts with top handler, finds its child handler "greet" of class Greeter and then calls its "hello" method.

Non-leaf handlers in the tree can have their own methods. For example:

.. code-block:: python

	# time_hello_split2.py
	from webpie import WPApp, WPHandler
	import time

	class HelloHandler(WPHandler):						

		def hello(self, request, relpath):				
			return "Hello, World!\n"					

	class ClockHandler(WPHandler):						

		def time(self, request, relpath):			
			return time.ctime()+"\n", "text/plain"	

	class TopHandler(WPHandler):

		def __init__(self, *params, **kv):
			WPHandler.__init__(self, *params, **kv)
			self.greet = HelloHandler(*params, **kv)
			self.clock = ClockHandler(*params, **kv)

		def version(self, request, relpath):    # non-leaf handler can have a web method
		    return "1.0.3"

	application = WPApp(TopHandler)
	application.run_server(8080)


.. code-block:: bash

	$ curl  http://localhost:8080/version
	1.0.2


Application and Handler
-----------------------

The WPApp object is created *once* when the web server instance starts and it persists until the server stops whereas WPHandler object trees are created for each individual HTTP request from scratch. Handler object's App member always points to its app object. This allows the app object to keep some persistent information and let handler objects access it. For example, or clock application can also maintain number of requests it has received:

.. code-block:: python

	# time_count.py
	from webpie import WPApp, WPHandler
	import time

	class Handler(WPHandler):						

		def time(self, request, relpath):		
			self.App.Counter += 1
			return time.ctime()+"\n", "text/plain"

		def count(self, request, relpath): 
			return str(self.App.Counter)+"\n"


	class App(WPApp):

		def __init__(self, handler_class):
			WPApp.__init__(self, handler_class)
			self.Counter = 0

	application = App(Handler)
	application.run_server(8080)


.. code-block:: bash

	$ curl  http://localhost:8080/time
	Sun May  5 08:10:12 2019
	$ curl  http://localhost:8080/time
	Sun May  5 08:10:14 2019
	$ curl  http://localhost:8080/count
	2
	$ curl  http://localhost:8080/time
	Sun May  5 08:10:17 2019
	$ curl  http://localhost:8080/count
	3


Of course the way it is written, our application is not very therad-safe, but there is an easy way to fix it.
We will talk about this later.

Web Methods in Details
----------------------

The WebPie server handler method has 2 fixed arguments and optional keyword arguments.

First argiment is the request object, which encapsulates all the information about the incoming HTTP request. Currently WebPie uses WebOb library Request and Response classes to handle HTTP requests and responses.

Arguments
~~~~~~~~~

Most generally, web method looks like this:

.. code-block:: python

    def method(self, request, relpath, **url_args):
        # ...
        return response


Web method arguments are:

request
.......

request is WebOb request object built from the WSGI environment. For convenience, it is also available as the handler's
Request member.

relpath
.......

Sometimes, while walking down the tree of handlers to find the method to handle the request, there will be some
unused portion of URI after the name of the target handler method. For example, in our clock example, we may want to
structure our URL to specify the field of the current time we want to see in the following way:

.. code-block::

	http://localhost:8080/time/month    # month only
	http://localhost:8080/time/minute   # minute only
	http://localhost:8080/time          # whole day/time

In this case, we want the "time" method to hadle all types of requests and know which portion of date/time to
return. Here is the code which does this:

.. code-block:: python

	from webpie import WPApp, WPHandler
	from datetime import datetime

	class MyHandler(WPHandler):						

		def time(self, request, relpath):			
			t = datetime.now()
			if not relpath:
				return str(t)+"\n"
			elif relpath == "year":
				return str(t.year)+"\n"
			elif relpath == "month":
				return str(t.month)+"\n"
			elif relpath == "day":
				return str(t.day)+"\n"
			elif relpath == "hour":
				return str(t.hour)+"\n"
			elif relpath == "minute":
				return str(t.minute)+"\n"
			elif relpath == "second":
				return str(t.second)+"\n"

	application = WPApp(MyHandler)
	application.run_server(8080)

url_args
........

Anoter, perhaps more conventional way of doing this is to use so called query parameters to specify the
format of the date/time representation, e.g.:

.. code-block::

	http://localhost:8080/time?field=minute

WebPie always parses query parameters and passes them to the handler method as if they were keyword arguments. 
For example, we can write the method which extracts fields from current time like this:

.. code-block:: python

	# time_args.py
	from webpie import WPApp, WPHandler
	from datetime import datetime

	class MyHandler(WPHandler):						

		def time(self, request, relpath, field="all"):		
			t = datetime.now()
			if field == "all":
				return str(t)+"\n"
			elif field == "year":
				return str(t.year)+"\n"
			elif field == "month":
				return str(t.month)+"\n"
			elif field == "day":
				return str(t.day)+"\n"
			elif field == "hour":
				return str(t.hour)+"\n"
			elif field == "minute":
				return str(t.minute)+"\n"
			elif field == "second":
				return str(t.second)+"\n"

	WPApp(MyHandler).run_server(8080)


and then call it like this:

.. code-block:: bash

	$ curl  http://localhost:8080/time
	2019-05-05 08:39:49.593855
	$ curl  "http://localhost:8080/time?field=month"
	5
	$ curl  "http://localhost:8080/time?field=year"
	2019

Return Value
~~~~~~~~~~~~
The output of a web method is a Response object. Conveniently, there is a number of ways to return something from the web method. Ultimately, all of them are used to produce and return the Response object. Here is a list of possibile returns from the web oject and how the framework
converts the output to the Response object:

======================================  =================================== ==================================================================
return                                  example                             equivalent Response object
======================================  =================================== ==================================================================
Response object                         Response("OK")                      same - Response("OK")
text                                    "hello world"                       Response("hello world")
text, content type                      "OK", "text/plain"                  Response("OK", content_type="text/plain")
text, status                            "Error", 500                        Response("Error", status_code=500)
text, status, content type              "Error", 500, "text/plain"          Response("Error", status_code=500, content_type="text/plain")
text, headers                           "OK", {"Content-Type":"text/plain"} Response("OK", headers={"Content-Type":"text/plain"})
list                                    ["Hello","world"]                   Response(app_iter=["Hello","world"])
iterable                                (x for x in ["hi","there"])         Response(app_iter=(x for x in ["hi","there"]))
iterable, content_type
iterable, status, content_type
iterable, status, headers
======================================  =================================== ==================================================================

The response body can be returned either as a single string or bytes object, or as a list of strings or
bytes objects or as an iterable (generator or iterator), producing a sequence of strings or bytes objects.
If the handler method returns strings, under Python3, they will be converted to bytes using UTF-8 conversion.
If you want to use some other encoding, then you must convert your strings to bytes before returning
from the handler method.


Static Content
--------------

Sometimes the application needs to serve static content like HTML documents, CSS stylesheets, JavaScript code.
WebPie App can be configured to serve static file from certain directory in the file system.


.. code-block:: python

    class MyHandler(WPHandler):
        #...

    class MyApp(WPApp):
        #...

    application = MyApp(MyHandler, 
            static_enabled = True,
            static_path = "/static", 
            static_location = "./scripts")

    application.run_server(8002)


If you run such an application, a request for URL like "http://..../static/code.js" will result in
delivery of file local file ./scripts/code.js. static_location can be either relative to the working
directory where the application runs or an absolute path.

Because serving files from local file system is a potential security vulnerability, this
functionality must be explicitly enabled with static_enabled=True. static_path and static_locations
have defaults:

.. code-block:: python

    static_path = "/static"
    static_location = "./static"

Threaded Applications
---------------------
WebPie provides several mechanisms to build thread safe applications. When working in multithreaded environment, WebPie Handler
objects are concurrently created in their own threads, one for each request, whereas WebApp object is created only once and it
is shared by all the threads handling the requests. This feature makes it possible to use the App object for inter-handler
synchronization. The App object has its own lock object and threads can use it in 2 different ways:

atomic decorator
~~~~~~~~~~~~~~~~
Decorating a web method with "atomic" decorator makes the web method atomic in the sense that if a handler thread enters such
a method, any other handler thread of the same application will block before entering any atomic method until the first thread returns from the method.

For example:

.. code-block:: python

    from webpie import WPApp, WPHandler, atomic

    class MyApp(WPApp):

        def __init__(self, root_class):
            WPApp.__init__(self, root_class)
            self.Memory = {}

    class Handler(WPHandler):

        @atomic
        def set(self, req, relpath, name=None, value=None, **args):
            self.App.Memory[name]=value
            return "OK\n"

        @atomic
        def get(self, req, relpath, name=None, **args):
            return self.App.Memory.get(name, "(undefined)")+"\n"

    application = MyApp(Handler)
    application.run_server(8002)

You can also decorate methods of the App. For example:

.. code-block:: python

	from webpie import WPApp, WPHandler, atomic

	class MyApp(WPApp):

	    RecordSize = 10

	    def __init__(self, root_class):
	        WPApp.__init__(self, root_class)
	        self.Record = []

	    @atomic
	    def add(self, value):
	        if value in self.Record:
	            self.Record.remove(value)
	        self.Record.insert(0, value)
	        if len(self.Record) > self.RecordSize:
	            self.Record = self.Record[:self.RecordSize]

	    @atomic
	    def find(self, value):
	        try:    i = self.Record.index(value)
	        except ValueError:
	            return "not found"
	        self.Record.pop(i)
	        self.Record.insert(0, value)
	        return str(i)

	class Handler(WPHandler):

	    def add(self, req, relpath, **args):
	        return self.App.add(relpath)

	    def find(self, req, relpath, **args):
	        return self.App.find(relpath)

	application = MyApp(Handler)
	application.run_server(8002)


App object as a context manager
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Another to implement a critical section is to use the App object as the context manager:


.. code-block:: python

    from webpie import WPApp, WPHandler

    class MyApp(WPApp):

        def __init__(self, root_class):
            WPApp.__init__(self, root_class)
            self.Memory = {}

    class Handler(WPHandler):

        def set(self, req, relpath, name=None, value=None, **args):
            with self.App:
                self.App.Memory[name]=value
            return "OK\n"

        def get(self, req, relpath, name=None, **args):
            with self.App:
                return self.App.Memory.get(name, "(undefined)") + "\n"

    application = MyApp(Handler)
    application.run_server(8002)


Session Management
------------------


Jinja2 Environment
------------------

WebPie is aware of Jinja2 template library and provides some shortcuts in using it.

To make your application work with Jinja2, you need to initialize Jinja2 environment first:

.. code-block:: python

	from webpie import WPApp, WPHandler		

	class MyHandler(WPHandler):    
        # ...


    class MyApp(WPApp):
        # ...

	application = MyApp(MyHandler)
    application.initJinjaEnvironment(
        tempdirs = [...],
        filters = {...},
        globals = {...}
    )

The initJinjaEnvironment method accepts 3 arguments:

tempdirs - list of directories where to look for Jinja2 templates,

filters - dictionary with filter names and filter functions to add to the environment,

globals - dictionary with "global" variables, which will be added to the list of variables when a template is rendered


Here is an example of such an application and corresponding template:


.. code-block:: python

    # templates.py
    from webpie import WPApp, WPHandler
    import time

    Version = "1.3"

    def format_time(t):
        return time.ctime(t)

    class MyHandler(WPHandler):						

        def time(self, request, relpath):
            return self.render_to_response("time.html", t=time.time())

    application = WPApp(MyHandler)
    application.initJinjaEnvironment(
        ["samples"], 
        filters={ "format": format_time },
        globals={ "version": Version }
        )
    application.run_server(8080)

and the template samples/time.html is:

.. code-block:: html

    <html>
    <body>
    <p>Current time is {{t|format}}</p>
    <p style="float:right"><i>Version: {{version}}</i></p>
    </body>
    </html>

In this example, the application initializes the Jinja2 environment with "samples" as the templates location,
function "format_time" becomes the filter used to display numeric time as date/time string and "global"
variable "version" is set to the version of the code.

Then the handler calls the "render_to_response" method, inherited from WPHandler, to render the template "time.html"
with current time passed as the "t" argument, and implicitly "version" passed to the rendering as a global
variable. The "render_to_response" method renders the template and returns properly constructed Response
object with content type set to "text/html".

Advanced Topics
---------------

Permissions
~~~~~~~~~~~

Strict Applications
~~~~~~~~~~~~~~~~~~~

Built-in HTTP/HTTPS Server
~~~~~~~~~~~~~~~~~~~~~~~~~~



            

Raw data

            {
    "_id": null,
    "home_page": "https://webpie.github.io/",
    "name": "webpie",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "web service,wsgi,web application",
    "author": "Igor Mandrichenko",
    "author_email": "igorvm@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fb/76/a27779a1edc33eb93df920b2f48001c349a0d1731442e0c657034a5d9176/webpie-5.16.3.tar.gz",
    "platform": null,
    "description": "\nWebPie\n======\n\nWebPie (another way of spelling web-py) is a web application development framework for Python based on the WSGI standard.\nWebPie makes it simple to develop thread-safe object-oriented web applications.\n\nHello World in WebPie\n---------------------\n\nHere is the simplest WebPie application you can write:\n\n.. code-block:: python\n\n\t# hello_world.py\n\n\tfrom webpie import WPApp, WPHandler\t\t\n\n\tclass MyHandler(WPHandler):                         # 1\n\n\t    def hello(self, request, relpath):              # 2\n\t        return \"Hello, World!\\n\"                    # 3\n\n\tapplication = WPApp(MyHandler)                      # 4\n\n\nLet's go over the code line by line:\n\n1. We created class MyHandler, which will handle HTTP requests. In order to work with WebPie, it has to be a subclass of WPHandler class.\n\n2. We defined one web method \"hello\", which will be called when a URL like http://host.org/hello is requested.\n\n3. It will always return text \"Hello, World!\".\n\n4. Finally, we created a WSGI application as an instance of WPApp class, passing it the MyHandler class as an argument.\n\nNow we can plug our application into any WSGI framework such as uWSGI or Apache httpd, e.g.:\n\n.. code-block:: bash\n\n\t$ uwsgi --http :8080 --wsgi-file hello_world.py\n\n\nand try it:\n\n.. code-block:: bash\n\n\t$ curl http://localhost:8080/hello\n\tHello world!\n\t$ \n\n\nIf you do not want to use uWSGI or similar framework, you can use WebPie's own HTTP server to put your application on the web:\n\n.. code-block:: python\n\n\t# hello_world_server.py\n\tfrom webpie import WPApp, WPHandler, run_server\n\timport time\n\n\tclass MyHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef hello(self, request, relpath):\t\t\t\t\n\t\t\treturn \"Hello, World!\\n\"\t\t\t\t\t\n\n\tapplication = WPApp(MyHandler)\n\tapplication.run_server(8080)        # run the server at port 8080\n\n.. code-block:: bash\n\n\t$ python hello_world_server.py &\n\t$ curl http://localhost:8080/hello\n\tHello world!\n    $\n\nURL Structure\n-------------\nNotice that MyHandler class has single method \"hello\" and it maps to the URL path \"hello\". This is general rule in WebPie - methods of handler classes map one to one to the elements of URI path. For example, we can add another method to our server called \"time\":\n\n.. code-block:: python\n\n\tfrom webpie import WPApp, WPHandler\n\timport time\n\n\tclass MyHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef hello(self, request, relpath):\t\t\t\t\n\t\t\treturn \"Hello, World!\\n\"\t\t\t\t\t\n\n\t\tdef time(self, request, relpath):\n\t\t\treturn time.ctime()+\"\\n\"\n\n\tapplication = WPApp(MyHandler)\n\tapplication.run_server(8080)\n\nNow our handler can handle 2 types of requests, it can say hello and it can tell local time:\n\n.. code-block:: bash\n\n\t$ curl http://localhost:8080/hello\n\tHello, World!\n\t$ curl http://localhost:8080/time\n\tSun May  5 06:47:15 2019\n\t$ \n\nNotice that handler methods names automatically become parts of the URL path.\n\nIf you want to split your handler into different classes to organize your code better, you can have nested handler classes in your application. For example, we may want to have one handler which focuses on reporting time and the other which says hello:\n\n.. code-block:: python\n\n\t# time_hello_split.py\n\tfrom webpie import WPApp, WPHandler\n\timport time\n\n\tclass HelloHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef hello(self, request, relpath):\t\t\t\t\n\t\t\treturn \"Hello, World!\\n\"\t\t\t\t\t\n\n\tclass ClockHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef time(self, request, relpath):\t\t\t\n\t\t\treturn time.ctime()+\"\\n\", \"text/plain\"\t\n\n\tclass TopHandler(WPHandler):\n\n\t\tdef __init__(self, *params):\n\t\t\tWPHandler.__init__(self, *params)\n\t\t\tself.greet = HelloHandler(*params)\n\t\t\tself.clock = ClockHandler(*params)\n\n\n\tapplication = WPApp(TopHandler)\n\tapplication.run_server(8080)\n\n\nWebPie application is given top handler class as an argument. It will create the handler instances one per each\nweb request. Top handler can create child handlers recursively. This recirsive handler structure maps one-to-one to the URL structure. The URI is simply the path from the top handler through its child handlers to the method of one of them:\n\n.. code-block:: bash\n\n\t$ curl http://localhost:8080/greet/hello\n\tHello, World!\n\t$ curl http://localhost:8080/clock/time\n\tSun May  5 06:49:14 2019\n\t$ \n\nFor example, to find the method for URI \"/greet/hello\", WebPie starts with top handler, finds its child handler \"greet\" of class Greeter and then calls its \"hello\" method.\n\nNon-leaf handlers in the tree can have their own methods. For example:\n\n.. code-block:: python\n\n\t# time_hello_split2.py\n\tfrom webpie import WPApp, WPHandler\n\timport time\n\n\tclass HelloHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef hello(self, request, relpath):\t\t\t\t\n\t\t\treturn \"Hello, World!\\n\"\t\t\t\t\t\n\n\tclass ClockHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef time(self, request, relpath):\t\t\t\n\t\t\treturn time.ctime()+\"\\n\", \"text/plain\"\t\n\n\tclass TopHandler(WPHandler):\n\n\t\tdef __init__(self, *params, **kv):\n\t\t\tWPHandler.__init__(self, *params, **kv)\n\t\t\tself.greet = HelloHandler(*params, **kv)\n\t\t\tself.clock = ClockHandler(*params, **kv)\n\n\t\tdef version(self, request, relpath):    # non-leaf handler can have a web method\n\t\t    return \"1.0.3\"\n\n\tapplication = WPApp(TopHandler)\n\tapplication.run_server(8080)\n\n\n.. code-block:: bash\n\n\t$ curl  http://localhost:8080/version\n\t1.0.2\n\n\nApplication and Handler\n-----------------------\n\nThe WPApp object is created *once* when the web server instance starts and it persists until the server stops whereas WPHandler object trees are created for each individual HTTP request from scratch. Handler object's App member always points to its app object. This allows the app object to keep some persistent information and let handler objects access it. For example, or clock application can also maintain number of requests it has received:\n\n.. code-block:: python\n\n\t# time_count.py\n\tfrom webpie import WPApp, WPHandler\n\timport time\n\n\tclass Handler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef time(self, request, relpath):\t\t\n\t\t\tself.App.Counter += 1\n\t\t\treturn time.ctime()+\"\\n\", \"text/plain\"\n\n\t\tdef count(self, request, relpath): \n\t\t\treturn str(self.App.Counter)+\"\\n\"\n\n\n\tclass App(WPApp):\n\n\t\tdef __init__(self, handler_class):\n\t\t\tWPApp.__init__(self, handler_class)\n\t\t\tself.Counter = 0\n\n\tapplication = App(Handler)\n\tapplication.run_server(8080)\n\n\n.. code-block:: bash\n\n\t$ curl  http://localhost:8080/time\n\tSun May  5 08:10:12 2019\n\t$ curl  http://localhost:8080/time\n\tSun May  5 08:10:14 2019\n\t$ curl  http://localhost:8080/count\n\t2\n\t$ curl  http://localhost:8080/time\n\tSun May  5 08:10:17 2019\n\t$ curl  http://localhost:8080/count\n\t3\n\n\nOf course the way it is written, our application is not very therad-safe, but there is an easy way to fix it.\nWe will talk about this later.\n\nWeb Methods in Details\n----------------------\n\nThe WebPie server handler method has 2 fixed arguments and optional keyword arguments.\n\nFirst argiment is the request object, which encapsulates all the information about the incoming HTTP request. Currently WebPie uses WebOb library Request and Response classes to handle HTTP requests and responses.\n\nArguments\n~~~~~~~~~\n\nMost generally, web method looks like this:\n\n.. code-block:: python\n\n    def method(self, request, relpath, **url_args):\n        # ...\n        return response\n\n\nWeb method arguments are:\n\nrequest\n.......\n\nrequest is WebOb request object built from the WSGI environment. For convenience, it is also available as the handler's\nRequest member.\n\nrelpath\n.......\n\nSometimes, while walking down the tree of handlers to find the method to handle the request, there will be some\nunused portion of URI after the name of the target handler method. For example, in our clock example, we may want to\nstructure our URL to specify the field of the current time we want to see in the following way:\n\n.. code-block::\n\n\thttp://localhost:8080/time/month    # month only\n\thttp://localhost:8080/time/minute   # minute only\n\thttp://localhost:8080/time          # whole day/time\n\nIn this case, we want the \"time\" method to hadle all types of requests and know which portion of date/time to\nreturn. Here is the code which does this:\n\n.. code-block:: python\n\n\tfrom webpie import WPApp, WPHandler\n\tfrom datetime import datetime\n\n\tclass MyHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef time(self, request, relpath):\t\t\t\n\t\t\tt = datetime.now()\n\t\t\tif not relpath:\n\t\t\t\treturn str(t)+\"\\n\"\n\t\t\telif relpath == \"year\":\n\t\t\t\treturn str(t.year)+\"\\n\"\n\t\t\telif relpath == \"month\":\n\t\t\t\treturn str(t.month)+\"\\n\"\n\t\t\telif relpath == \"day\":\n\t\t\t\treturn str(t.day)+\"\\n\"\n\t\t\telif relpath == \"hour\":\n\t\t\t\treturn str(t.hour)+\"\\n\"\n\t\t\telif relpath == \"minute\":\n\t\t\t\treturn str(t.minute)+\"\\n\"\n\t\t\telif relpath == \"second\":\n\t\t\t\treturn str(t.second)+\"\\n\"\n\n\tapplication = WPApp(MyHandler)\n\tapplication.run_server(8080)\n\nurl_args\n........\n\nAnoter, perhaps more conventional way of doing this is to use so called query parameters to specify the\nformat of the date/time representation, e.g.:\n\n.. code-block::\n\n\thttp://localhost:8080/time?field=minute\n\nWebPie always parses query parameters and passes them to the handler method as if they were keyword arguments. \nFor example, we can write the method which extracts fields from current time like this:\n\n.. code-block:: python\n\n\t# time_args.py\n\tfrom webpie import WPApp, WPHandler\n\tfrom datetime import datetime\n\n\tclass MyHandler(WPHandler):\t\t\t\t\t\t\n\n\t\tdef time(self, request, relpath, field=\"all\"):\t\t\n\t\t\tt = datetime.now()\n\t\t\tif field == \"all\":\n\t\t\t\treturn str(t)+\"\\n\"\n\t\t\telif field == \"year\":\n\t\t\t\treturn str(t.year)+\"\\n\"\n\t\t\telif field == \"month\":\n\t\t\t\treturn str(t.month)+\"\\n\"\n\t\t\telif field == \"day\":\n\t\t\t\treturn str(t.day)+\"\\n\"\n\t\t\telif field == \"hour\":\n\t\t\t\treturn str(t.hour)+\"\\n\"\n\t\t\telif field == \"minute\":\n\t\t\t\treturn str(t.minute)+\"\\n\"\n\t\t\telif field == \"second\":\n\t\t\t\treturn str(t.second)+\"\\n\"\n\n\tWPApp(MyHandler).run_server(8080)\n\n\nand then call it like this:\n\n.. code-block:: bash\n\n\t$ curl  http://localhost:8080/time\n\t2019-05-05 08:39:49.593855\n\t$ curl  \"http://localhost:8080/time?field=month\"\n\t5\n\t$ curl  \"http://localhost:8080/time?field=year\"\n\t2019\n\nReturn Value\n~~~~~~~~~~~~\nThe output of a web method is a Response object. Conveniently, there is a number of ways to return something from the web method. Ultimately, all of them are used to produce and return the Response object. Here is a list of possibile returns from the web oject and how the framework\nconverts the output to the Response object:\n\n======================================  =================================== ==================================================================\nreturn                                  example                             equivalent Response object\n======================================  =================================== ==================================================================\nResponse object                         Response(\"OK\")                      same - Response(\"OK\")\ntext                                    \"hello world\"                       Response(\"hello world\")\ntext, content type                      \"OK\", \"text/plain\"                  Response(\"OK\", content_type=\"text/plain\")\ntext, status                            \"Error\", 500                        Response(\"Error\", status_code=500)\ntext, status, content type              \"Error\", 500, \"text/plain\"          Response(\"Error\", status_code=500, content_type=\"text/plain\")\ntext, headers                           \"OK\", {\"Content-Type\":\"text/plain\"} Response(\"OK\", headers={\"Content-Type\":\"text/plain\"})\nlist                                    [\"Hello\",\"world\"]                   Response(app_iter=[\"Hello\",\"world\"])\niterable                                (x for x in [\"hi\",\"there\"])         Response(app_iter=(x for x in [\"hi\",\"there\"]))\niterable, content_type\niterable, status, content_type\niterable, status, headers\n======================================  =================================== ==================================================================\n\nThe response body can be returned either as a single string or bytes object, or as a list of strings or\nbytes objects or as an iterable (generator or iterator), producing a sequence of strings or bytes objects.\nIf the handler method returns strings, under Python3, they will be converted to bytes using UTF-8 conversion.\nIf you want to use some other encoding, then you must convert your strings to bytes before returning\nfrom the handler method.\n\n\nStatic Content\n--------------\n\nSometimes the application needs to serve static content like HTML documents, CSS stylesheets, JavaScript code.\nWebPie App can be configured to serve static file from certain directory in the file system.\n\n\n.. code-block:: python\n\n    class MyHandler(WPHandler):\n        #...\n\n    class MyApp(WPApp):\n        #...\n\n    application = MyApp(MyHandler, \n            static_enabled = True,\n            static_path = \"/static\", \n            static_location = \"./scripts\")\n\n    application.run_server(8002)\n\n\nIf you run such an application, a request for URL like \"http://..../static/code.js\" will result in\ndelivery of file local file ./scripts/code.js. static_location can be either relative to the working\ndirectory where the application runs or an absolute path.\n\nBecause serving files from local file system is a potential security vulnerability, this\nfunctionality must be explicitly enabled with static_enabled=True. static_path and static_locations\nhave defaults:\n\n.. code-block:: python\n\n    static_path = \"/static\"\n    static_location = \"./static\"\n\nThreaded Applications\n---------------------\nWebPie provides several mechanisms to build thread safe applications. When working in multithreaded environment, WebPie Handler\nobjects are concurrently created in their own threads, one for each request, whereas WebApp object is created only once and it\nis shared by all the threads handling the requests. This feature makes it possible to use the App object for inter-handler\nsynchronization. The App object has its own lock object and threads can use it in 2 different ways:\n\natomic decorator\n~~~~~~~~~~~~~~~~\nDecorating a web method with \"atomic\" decorator makes the web method atomic in the sense that if a handler thread enters such\na method, any other handler thread of the same application will block before entering any atomic method until the first thread returns from the method.\n\nFor example:\n\n.. code-block:: python\n\n    from webpie import WPApp, WPHandler, atomic\n\n    class MyApp(WPApp):\n\n        def __init__(self, root_class):\n            WPApp.__init__(self, root_class)\n            self.Memory = {}\n\n    class Handler(WPHandler):\n\n        @atomic\n        def set(self, req, relpath, name=None, value=None, **args):\n            self.App.Memory[name]=value\n            return \"OK\\n\"\n\n        @atomic\n        def get(self, req, relpath, name=None, **args):\n            return self.App.Memory.get(name, \"(undefined)\")+\"\\n\"\n\n    application = MyApp(Handler)\n    application.run_server(8002)\n\nYou can also decorate methods of the App. For example:\n\n.. code-block:: python\n\n\tfrom webpie import WPApp, WPHandler, atomic\n\n\tclass MyApp(WPApp):\n\n\t    RecordSize = 10\n\n\t    def __init__(self, root_class):\n\t        WPApp.__init__(self, root_class)\n\t        self.Record = []\n\n\t    @atomic\n\t    def add(self, value):\n\t        if value in self.Record:\n\t            self.Record.remove(value)\n\t        self.Record.insert(0, value)\n\t        if len(self.Record) > self.RecordSize:\n\t            self.Record = self.Record[:self.RecordSize]\n\n\t    @atomic\n\t    def find(self, value):\n\t        try:    i = self.Record.index(value)\n\t        except ValueError:\n\t            return \"not found\"\n\t        self.Record.pop(i)\n\t        self.Record.insert(0, value)\n\t        return str(i)\n\n\tclass Handler(WPHandler):\n\n\t    def add(self, req, relpath, **args):\n\t        return self.App.add(relpath)\n\n\t    def find(self, req, relpath, **args):\n\t        return self.App.find(relpath)\n\n\tapplication = MyApp(Handler)\n\tapplication.run_server(8002)\n\n\nApp object as a context manager\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nAnother to implement a critical section is to use the App object as the context manager:\n\n\n.. code-block:: python\n\n    from webpie import WPApp, WPHandler\n\n    class MyApp(WPApp):\n\n        def __init__(self, root_class):\n            WPApp.__init__(self, root_class)\n            self.Memory = {}\n\n    class Handler(WPHandler):\n\n        def set(self, req, relpath, name=None, value=None, **args):\n            with self.App:\n                self.App.Memory[name]=value\n            return \"OK\\n\"\n\n        def get(self, req, relpath, name=None, **args):\n            with self.App:\n                return self.App.Memory.get(name, \"(undefined)\") + \"\\n\"\n\n    application = MyApp(Handler)\n    application.run_server(8002)\n\n\nSession Management\n------------------\n\n\nJinja2 Environment\n------------------\n\nWebPie is aware of Jinja2 template library and provides some shortcuts in using it.\n\nTo make your application work with Jinja2, you need to initialize Jinja2 environment first:\n\n.. code-block:: python\n\n\tfrom webpie import WPApp, WPHandler\t\t\n\n\tclass MyHandler(WPHandler):    \n        # ...\n\n\n    class MyApp(WPApp):\n        # ...\n\n\tapplication = MyApp(MyHandler)\n    application.initJinjaEnvironment(\n        tempdirs = [...],\n        filters = {...},\n        globals = {...}\n    )\n\nThe initJinjaEnvironment method accepts 3 arguments:\n\ntempdirs - list of directories where to look for Jinja2 templates,\n\nfilters - dictionary with filter names and filter functions to add to the environment,\n\nglobals - dictionary with \"global\" variables, which will be added to the list of variables when a template is rendered\n\n\nHere is an example of such an application and corresponding template:\n\n\n.. code-block:: python\n\n    # templates.py\n    from webpie import WPApp, WPHandler\n    import time\n\n    Version = \"1.3\"\n\n    def format_time(t):\n        return time.ctime(t)\n\n    class MyHandler(WPHandler):\t\t\t\t\t\t\n\n        def time(self, request, relpath):\n            return self.render_to_response(\"time.html\", t=time.time())\n\n    application = WPApp(MyHandler)\n    application.initJinjaEnvironment(\n        [\"samples\"], \n        filters={ \"format\": format_time },\n        globals={ \"version\": Version }\n        )\n    application.run_server(8080)\n\nand the template samples/time.html is:\n\n.. code-block:: html\n\n    <html>\n    <body>\n    <p>Current time is {{t|format}}</p>\n    <p style=\"float:right\"><i>Version: {{version}}</i></p>\n    </body>\n    </html>\n\nIn this example, the application initializes the Jinja2 environment with \"samples\" as the templates location,\nfunction \"format_time\" becomes the filter used to display numeric time as date/time string and \"global\"\nvariable \"version\" is set to the version of the code.\n\nThen the handler calls the \"render_to_response\" method, inherited from WPHandler, to render the template \"time.html\"\nwith current time passed as the \"t\" argument, and implicitly \"version\" passed to the rendering as a global\nvariable. The \"render_to_response\" method renders the template and returns properly constructed Response\nobject with content type set to \"text/html\".\n\nAdvanced Topics\n---------------\n\nPermissions\n~~~~~~~~~~~\n\nStrict Applications\n~~~~~~~~~~~~~~~~~~~\n\nBuilt-in HTTP/HTTPS Server\n~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n",
    "bugtrack_url": null,
    "license": "BSD 3-clause",
    "summary": "A set of useful tools built on top of standard Python threading module",
    "version": "5.16.3",
    "project_urls": {
        "Homepage": "https://webpie.github.io/"
    },
    "split_keywords": [
        "web service",
        "wsgi",
        "web application"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "88a7b59f689bf8b8886971e92f1c86e0f5b2b6606e73d807c57ac272ab9ff6e3",
                "md5": "68c539f3b0dfb27817d8144b00ecf236",
                "sha256": "5b7b54d1fd29af56882997157026e531b8815e41c161c3a7d9c2cadea5408ab0"
            },
            "downloads": -1,
            "filename": "webpie-5.16.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "68c539f3b0dfb27817d8144b00ecf236",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 173057,
            "upload_time": "2023-06-29T18:35:34",
            "upload_time_iso_8601": "2023-06-29T18:35:34.403783Z",
            "url": "https://files.pythonhosted.org/packages/88/a7/b59f689bf8b8886971e92f1c86e0f5b2b6606e73d807c57ac272ab9ff6e3/webpie-5.16.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fb76a27779a1edc33eb93df920b2f48001c349a0d1731442e0c657034a5d9176",
                "md5": "1e37811a726342c3e9f4b118ff9faf04",
                "sha256": "7672844b3972dd98f5c0c5e256d84812daf841fcfd1ed7b0059e5928fffaffaf"
            },
            "downloads": -1,
            "filename": "webpie-5.16.3.tar.gz",
            "has_sig": false,
            "md5_digest": "1e37811a726342c3e9f4b118ff9faf04",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 151236,
            "upload_time": "2023-06-29T18:35:36",
            "upload_time_iso_8601": "2023-06-29T18:35:36.914707Z",
            "url": "https://files.pythonhosted.org/packages/fb/76/a27779a1edc33eb93df920b2f48001c349a0d1731442e0c657034a5d9176/webpie-5.16.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-29 18:35:36",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "webpie"
}
        
Elapsed time: 0.13801s