from pattern.server import static from pattern.server import HTTPError # The pattern.server module allows you to run a pure-Python web server. # It is built on CherryPy and inspired by Flask and bottle.py. # This example demonstrates a basic web app. # At the bottom of the script is a call to app.run(). # So, to start the web server, run this script. # If you make any changes to the script and save it, # the server automatically restarts to reflect the changes. # If the server is running in "production mode", i.e., app.run(debug=False), # it will not restart automatically. app = App(name="basic", static="static/") # Here are some properties of the app. # app.path yields the absolute path to the app folder. # app.static yields the absolute path to the folder for static content. print app.name print app.path print app.static # The @app.route() decorator can be used to define a URL path handler. # A path handler is simply a Python function that returns a string, # which will be displayed in the browser. # For example, visit http://127.0.0.1:8080/:
import os, sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from pattern.server import App, Database, html # This example demonstrates a web app with a simple database back-end. # The pattern.server module has a Database object # that can be used with SQLite and MySQL databases. # SQLite is part of Python 2.5+. # MySQL requires the mysql-python bindings (http://sourceforge.net/projects/mysql-python/). app = App("store") # In this example we'll use SQLite. # The advantage is that you don't need to install anything, # and that the database is a file at a location of your choice. # We use SQLite Database browser (Mac OS X) to browse the file. # (http://sourceforge.net/projects/sqlitebrowser/) # The disadvantage is that SQLite is not multi-threaded. # This will lead to problems ("database is locked") in larger projects. # The app server uses multiple threads to handle concurrent requests. # If two request want to write to the database at the same time, # one of them will have to wait while the other finishes writing. # If enough requests are waiting in line, the database may crash. # The next example uses a DatabaseTransaction to remedy this. # Reading from the database is no problem. # The following code creates a new database "store.db", # in the same folder as this script.
from pattern.server import static from pattern.server import HTTPError # The pattern.server module allows you to run a pure-Python web server. # It is built on CherryPy and inspired by Flask and bottle.py. # This example demonstrates a basic web app. # At the bottom of the script is a call to app.run(). # So, to start the web server, run this script. # If you make any changes to the script and save it, # the server automatically restarts to reflect the changes. # If the server is running in "production mode", i.e., app.run(debug=False), # it will not restart automatically. app = App(name="basic", static="static/") # Here are some properties of the app. # app.path yields the absolute path to the app folder. # app.static yields the absolute path to the folder for static content. print app.name print app.path print app.static # The @app.route() decorator can be used to define a URL path handler. # A path handler is simply a Python function that returns a string, # which will be displayed in the browser. # For example, visit http://127.0.0.1:8080/: @app.route("/")
from builtins import str, bytes, dict, int import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from pattern.server import App, Database, html # This example demonstrates a web app with a simple database back-end. # The pattern.server module has a Database object # that can be used with SQLite and MySQL databases. # SQLite is part of Python 2.5+. # MySQL requires the mysql-python bindings (http://sourceforge.net/projects/mysql-python/). app = App("store") # In this example we'll use SQLite. # The advantage is that you don't need to install anything, # and that the database is a file at a location of your choice. # We use SQLite Database browser (Mac OS X) to browse the file. # (http://sourceforge.net/projects/sqlitebrowser/) # The disadvantage is that SQLite is not multi-threaded. # This will lead to problems ("database is locked") in larger projects. # The app server uses multiple threads to handle concurrent requests. # If two request want to write to the database at the same time, # one of them will have to wait while the other finishes writing. # If enough requests are waiting in line, the database may crash. # The next example uses a DatabaseTransaction to remedy this. # Reading from the database is no problem.
import os, sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from pattern.server import App from pattern.server import MINUTE, HOUR, DAY from pattern.text import language app = App("api") # The language() function in pattern.text guesses the language of a given string. # For example: language("In French, goodbye is au revoir.") returns ("en", 0.83). # It can handle "en", "es", "de", "fr", "nl", "it" with reasonable accuracy. # To create a web service like Google Translate with pattern.server is easy. # Normally, URL handlers return a string with the contents of that web page. # If we return a dictionary instead, it will be formatted as a JSON-string, # the data interchange format used by many popular web services. # So clients (e.g., a user's Python script) can query the web service URL # and catch the JSON reply. # There is only one tricky part: rate limiting. # Note the "limit", "time" and "key" parameters in @app.route() below. # We'll explain them in more detail. # First, run the script and visit: # http://127.0.0.1:8080/language?q=in+french+goodbye+is+au+revoir # You should see some JSON-output:
import os, sys; sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "..")) from pattern.server import App, template, threadsafe from codecs import open # This example demonstrates a simple wiki served by pattern.server. # A wiki is a web app where each page can be edited (e.g, Wikipedia). # We will store the contents of each page as a file in /data. app = App(name="wiki") # Our wiki app has a single URL handler listening at the root ("/"). # It takes any combination of positional and keyword arguments. # This means that any URL will be routed to the index() function. # For example, http://127.0.0.1:8080/pages/bio.html?edit calls index() # with path=("pages", "bio.html") and data={"edit": ""}. @app.route("/") def index(*path, **data): #print "path:", path #print "data:", data # Construct a file name in /data from the URL path. # For example, path=("pages", "bio.html") # is mapped to "/data/pages/bio.html.txt". page = "/".join(path) page = page if page else "index.html" page = page.replace(" ", "-") page = page + ".txt" page = os.path.join(app.path, "data", page) # Absolute paths are safer. #print "page:", page