Package: 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 WebPieHere is the simplest WebPie application you can write:# hello_world.pyfromwebpieimportWPApp,WPHandlerclassMyHandler(WPHandler):# 1defhello(self,request,relpath):# 2return"Hello, World!\n"# 3application=WPApp(MyHandler)# 4Let’s go over the code line by line:We created class MyHandler, which will handle HTTP requests. In order to work with WebPie, it has to be a subclass of WPHandler class.We defined one web method “hello”, which will be called when a URL likehttp://host.org/hellois requested.It will always return text “Hello, World!”.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.:$uwsgi--http:8080--wsgi-filehello_world.pyand try it:$curlhttp://localhost:8080/helloHelloworld!$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:# hello_world_server.pyfromwebpieimportWPApp,WPHandler,run_serverimporttimeclassMyHandler(WPHandler):defhello(self,request,relpath):return"Hello, World!\n"application=WPApp(MyHandler)application.run_server(8080)# run the server at port 8080$pythonhello_world_server.py&$curlhttp://localhost:8080/helloHelloworld!$URL StructureNotice 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”:fromwebpieimportWPApp,WPHandlerimporttimeclassMyHandler(WPHandler):defhello(self,request,relpath):return"Hello, World!\n"deftime(self,request,relpath):returntime.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:$curlhttp://localhost:8080/helloHello,World!$curlhttp://localhost:8080/timeSunMay506:47:152019$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:# time_hello_split.pyfromwebpieimportWPApp,WPHandlerimporttimeclassHelloHandler(WPHandler):defhello(self,request,relpath):return"Hello, World!\n"classClockHandler(WPHandler):deftime(self,request,relpath):returntime.ctime()+"\n","text/plain"classTopHandler(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:$curlhttp://localhost:8080/greet/helloHello,World!$curlhttp://localhost:8080/clock/timeSunMay506:49:142019$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:# time_hello_split2.pyfromwebpieimportWPApp,WPHandlerimporttimeclassHelloHandler(WPHandler):defhello(self,request,relpath):return"Hello, World!\n"classClockHandler(WPHandler):deftime(self,request,relpath):returntime.ctime()+"\n","text/plain"classTopHandler(WPHandler):def__init__(self,*params,**kv):WPHandler.__init__(self,*params,**kv)self.greet=HelloHandler(*params,**kv)self.clock=ClockHandler(*params,**kv)defversion(self,request,relpath):# non-leaf handler can have a web methodreturn"1.0.3"application=WPApp(TopHandler)application.run_server(8080)$curlhttp://localhost:8080/version1.0.2Application and HandlerThe WPApp object is createdoncewhen 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:# time_count.pyfromwebpieimportWPApp,WPHandlerimporttimeclassHandler(WPHandler):deftime(self,request,relpath):self.App.Counter+=1returntime.ctime()+"\n","text/plain"defcount(self,request,relpath):returnstr(self.App.Counter)+"\n"classApp(WPApp):def__init__(self,handler_class):WPApp.__init__(self,handler_class)self.Counter=0application=App(Handler)application.run_server(8080)$curlhttp://localhost:8080/timeSunMay508:10:122019$curlhttp://localhost:8080/timeSunMay508:10:142019$curlhttp://localhost:8080/count2$curlhttp://localhost:8080/timeSunMay508:10:172019$curlhttp://localhost:8080/count3Of 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 DetailsThe 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.ArgumentsMost generally, web method looks like this:defmethod(self,request,relpath,**url_args):# ...returnresponseWeb method arguments are:requestrequest is WebOb request object built from the WSGI environment. For convenience, it is also available as the handler’s
Request member.relpathSometimes, 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:http://localhost:8080/time/month    # month only
http://localhost:8080/time/minute   # minute only
http://localhost:8080/time          # whole day/timeIn 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:fromwebpieimportWPApp,WPHandlerfromdatetimeimportdatetimeclassMyHandler(WPHandler):deftime(self,request,relpath):t=datetime.now()ifnotrelpath:returnstr(t)+"\n"elifrelpath=="year":returnstr(t.year)+"\n"elifrelpath=="month":returnstr(t.month)+"\n"elifrelpath=="day":returnstr(t.day)+"\n"elifrelpath=="hour":returnstr(t.hour)+"\n"elifrelpath=="minute":returnstr(t.minute)+"\n"elifrelpath=="second":returnstr(t.second)+"\n"application=WPApp(MyHandler)application.run_server(8080)url_argsAnoter, 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.:http://localhost:8080/time?field=minuteWebPie 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:# time_args.pyfromwebpieimportWPApp,WPHandlerfromdatetimeimportdatetimeclassMyHandler(WPHandler):deftime(self,request,relpath,field="all"):t=datetime.now()iffield=="all":returnstr(t)+"\n"eliffield=="year":returnstr(t.year)+"\n"eliffield=="month":returnstr(t.month)+"\n"eliffield=="day":returnstr(t.day)+"\n"eliffield=="hour":returnstr(t.hour)+"\n"eliffield=="minute":returnstr(t.minute)+"\n"eliffield=="second":returnstr(t.second)+"\n"WPApp(MyHandler).run_server(8080)and then call it like this:$curlhttp://localhost:8080/time2019-05-0508:39:49.593855$curl"http://localhost:8080/time?field=month"5$curl"http://localhost:8080/time?field=year"2019Return ValueThe 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:returnexampleequivalent Response objectResponse objectResponse(“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”, 500Response(“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_typeiterable, status, content_typeiterable, status, headersThe 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 ContentSometimes 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.classMyHandler(WPHandler):#...classMyApp(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:static_path="/static"static_location="./static"Threaded ApplicationsWebPie 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 decoratorDecorating 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:fromwebpieimportWPApp,WPHandler,atomicclassMyApp(WPApp):def__init__(self,root_class):WPApp.__init__(self,root_class)self.Memory={}classHandler(WPHandler):@atomicdefset(self,req,relpath,name=None,value=None,**args):self.App.Memory[name]=valuereturn"OK\n"@atomicdefget(self,req,relpath,name=None,**args):returnself.App.Memory.get(name,"(undefined)")+"\n"application=MyApp(Handler)application.run_server(8002)You can also decorate methods of the App. For example:fromwebpieimportWPApp,WPHandler,atomicclassMyApp(WPApp):RecordSize=10def__init__(self,root_class):WPApp.__init__(self,root_class)self.Record=[]@atomicdefadd(self,value):ifvalueinself.Record:self.Record.remove(value)self.Record.insert(0,value)iflen(self.Record)>self.RecordSize:self.Record=self.Record[:self.RecordSize]@atomicdeffind(self,value):try:i=self.Record.index(value)exceptValueError:return"not found"self.Record.pop(i)self.Record.insert(0,value)returnstr(i)classHandler(WPHandler):defadd(self,req,relpath,**args):returnself.App.add(relpath)deffind(self,req,relpath,**args):returnself.App.find(relpath)application=MyApp(Handler)application.run_server(8002)App object as a context managerAnother to implement a critical section is to use the App object as the context manager:fromwebpieimportWPApp,WPHandlerclassMyApp(WPApp):def__init__(self,root_class):WPApp.__init__(self,root_class)self.Memory={}classHandler(WPHandler):defset(self,req,relpath,name=None,value=None,**args):withself.App:self.App.Memory[name]=valuereturn"OK\n"defget(self,req,relpath,name=None,**args):withself.App:returnself.App.Memory.get(name,"(undefined)")+"\n"application=MyApp(Handler)application.run_server(8002)Session ManagementJinja2 EnvironmentWebPie 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:fromwebpieimportWPApp,WPHandlerclassMyHandler(WPHandler):# ...classMyApp(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 renderedHere is an example of such an application and corresponding template:# templates.pyfromwebpieimportWPApp,WPHandlerimporttimeVersion="1.3"defformat_time(t):returntime.ctime(t)classMyHandler(WPHandler):deftime(self,request,relpath):returnself.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:<html><body><p>Current time is {{t|format}}</p><pstyle="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 TopicsPermissionsStrict ApplicationsBuilt-in HTTP/HTTPS Server