BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Tornado blog app

Tornado.png




Bookmark and Share





bogotobogo.com site search:



Tornado

Tornado is a Python web framework and asynchronous networking library, originally developed at FriendFeed.

By using non-blocking network I/O, Tornado can scale to tens of thousands of open connections, making it ideal for long polling, WebSockets, and other applications that require a long-lived connection to each user.







Get the source code

Clone the repo : tornado:

$ git clone https://github.com/tornadoweb/tornado.git

We're going to use "demo/blog", so let's go into the directory:

$ cd tornado/demos/blog




Install tornado

Install Tornado:

$ sudp pip install tornado

Actually, we can just install all necessary packages on virtualenv like this:

(venv)$ pip install -r requirements.txt




Local run

Tornado supplies its own HTTPServer.

Here is a simple "Hello, world" example web app for Tornado:

# hello.py
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Run it:

$ python hello.py

Then, on the browser:


HelloWorld.png




Reverse Proxy

A proxy server relays a client's request to the appropriate server. Some network installations use proxy servers to filter and cache HTTP requests that machines on the local network make to the Internet.

Since we will be running a number of Tornado instances on a range of TCP ports, we will use a proxy server in reverse. In other words, clients across the Internet will connect to a reverse proxy server which forward requests to a pool of Tornado servers behind the proxy. The proxy server is designed to be transparent to the client and yet pass valuable information like the original client's IP address and TCP scheme to the upstream Tornado node.

Here is the Apache proxy configuration:

<VirtualHost *:80>
     ServerName www.pykey.com
     ServerAlias pykey.com

     ProxyPreserveHost On

     ProxyPass / http://127.0.0.1:8888/
     ProxyPassReverse / http://127.0.0.1:8888/

     ErrorLog /var/www/pykey.com/logs/error.log
     CustomLog /var/www/pykey.com/logs/access.log combined
     LogLevel warn
     ServerSignature Off
</VirtualHost>

pykey-local.png



pm2

PM2 is a process manager which makes managing a number of long-running programs a trivial task by providing a consistent interface through which they can be monitored and controlled. So, we can run the "hello.py" like this:

$ pm2 start hello.py




MySQL setup

At this point, make sure we've already installed the packages MySQL-python, torndb, and markdown using pip. Otherwise, do the following:

$ pip install MySQL-python
$ pip install torndb
$ pip install markdown

Next, connect to MySQL and create a database (tornadoBlog) and user (tornadoUser) for our blog:

$ mysql -u root -p

mysql> CREATE DATABASE tornadoBlog;
mysql> CREATE USER 'tornadoUser'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'tornadoUser'@'localhost';
mysql> FLUSH PRIVILEGES;

Let's create tables in the "tornadoBlog" database:

$ mysql -u tornadoUser -p tornadoBlog < schema.sql

Check the db:

mysql> use tornadoBlog;

mysql> show tables;
+-----------------------+
| Tables_in_tornadoBlog |
+-----------------------+
| authors               |
| entries               |
+-----------------------+



Run the blog app

Let's see what's in the directory (tornado/demos/blog):

$ blog.py  docker-compose.yml  Dockerfile  README  requirements.txt  schema.sql  static  templates
$ pm2 start blog.py

As a sample for the site (pykey.com), here is a new post page after login:

pykey-post.png







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong








Tornado



Tornado - Blog app



Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong






Python tutorial



Python Home

Introduction

Running Python Programs (os, sys, import)

Modules and IDLE (Import, Reload, exec)

Object Types - Numbers, Strings, and None

Strings - Escape Sequence, Raw String, and Slicing

Strings - Methods

Formatting Strings - expressions and method calls

Files and os.path

Traversing directories recursively

Subprocess Module

Regular Expressions with Python

Regular Expressions Cheat Sheet

Object Types - Lists

Object Types - Dictionaries and Tuples

Functions def, *args, **kargs

Functions lambda

Built-in Functions

map, filter, and reduce

Decorators

List Comprehension

Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism

Hashing (Hash tables and hashlib)

Dictionary Comprehension with zip

The yield keyword

Generator Functions and Expressions

generator.send() method

Iterators

Classes and Instances (__init__, __call__, etc.)

if__name__ == '__main__'

argparse

Exceptions

@static method vs class method

Private attributes and private methods

bits, bytes, bitstring, and constBitStream

json.dump(s) and json.load(s)

Python Object Serialization - pickle and json

Python Object Serialization - yaml and json

Priority queue and heap queue data structure

Graph data structure

Dijkstra's shortest path algorithm

Prim's spanning tree algorithm

Closure

Functional programming in Python

Remote running a local file using ssh

SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table

SQLite 3 - B. Selecting, updating and deleting data

MongoDB with PyMongo I - Installing MongoDB ...

Python HTTP Web Services - urllib, httplib2

Web scraping with Selenium for checking domain availability

REST API : Http Requests for Humans with Flask

Blog app with Tornado

Multithreading ...

Python Network Programming I - Basic Server / Client : A Basics

Python Network Programming I - Basic Server / Client : B File Transfer

Python Network Programming II - Chat Server / Client

Python Network Programming III - Echo Server using socketserver network framework

Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn

Python Coding Questions I

Python Coding Questions II

Python Coding Questions III

Python Coding Questions IV

Python Coding Questions V

Python Coding Questions VI

Python Coding Questions VII

Python Coding Questions VIII

Python Coding Questions IX

Python Coding Questions X

Image processing with Python image library Pillow

Python and C++ with SIP

PyDev with Eclipse

Matplotlib

Redis with Python

NumPy array basics A

NumPy Matrix and Linear Algebra

Pandas with NumPy and Matplotlib

Celluar Automata

Batch gradient descent algorithm

Longest Common Substring Algorithm

Python Unit Test - TDD using unittest.TestCase class

Simple tool - Google page ranking by keywords

Google App Hello World

Google App webapp2 and WSGI

Uploading Google App Hello World

Python 2 vs Python 3

virtualenv and virtualenvwrapper

Uploading a big file to AWS S3 using boto module

Scheduled stopping and starting an AWS instance

Cloudera CDH5 - Scheduled stopping and starting services

Removing Cloud Files - Rackspace API with curl and subprocess

Checking if a process is running/hanging and stop/run a scheduled task on Windows

Apache Spark 1.3 with PySpark (Spark Python API) Shell

Apache Spark 1.2 Streaming

bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...

Flask app with Apache WSGI on Ubuntu14/CentOS7 ...

Selenium WebDriver

Fabric - streamlining the use of SSH for application deployment

Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App

Neural Networks with backpropagation for XOR using one hidden layer

NLP - NLTK (Natural Language Toolkit) ...

RabbitMQ(Message broker server) and Celery(Task queue) ...

OpenCV3 and Matplotlib ...

Simple tool - Concatenating slides using FFmpeg ...

iPython - Signal Processing with NumPy

iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github

iPython and Jupyter Notebook with Embedded D3.js

Downloading YouTube videos using youtube-dl embedded with Python

Machine Learning : scikit-learn ...

Django 1.6/1.8 Web Framework ...









Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master