Try running Python's web framework Bottle on a lollipop server (lollipo plan)

I tried running a lightweight Python web framework called Bottle on a local and lollipop server (lollipop plan, ssh cannot be used ...). When running it on a lollipop server, it needed to be modified to suit the environment.

sample

On the title page, enter the "name", select "favorite noodles", and display the parameters passed by the GET method on the result output page. タイトルページ.png 結果出力のページ.png

First local

file organization

This time, the project name is `` `bottle_study```.

bottle_study
- views/
  |  title.tpl   # /Template file called by
  |  show.tpl    # /Template file called by show
  bottle.py      #Bottle body
  index.py       #dispatcher

procedure

  1. Get bottle.py

  2. Create index.py

  3. Create views / title.tpl

  4. Create views / show.tpl

  5. Run the built-in server

  6. bottle.py The bottle itself, bottle.py, is obtained by `` `wget``` directly under the project directory.

bottle.How to get py


$ mkdir bottle_study
$ pwd
(Depends on your environment)/bottle_study
$ wget http://bottlepy.org/bottle.py
$ ls
bottle.py
  1. index.py This time I wrote all the processing in index.py. At the bottom line, we are running the build-in server.

index.py


#!/usr/local/bin/python
# -*- coding: utf-8 -*-

from bottle import route, run, template, request


# localhost:8080
@route('/')
def title():
    # views/title.Call tpl
    return template('title')


# localhost:8080/show
@route('/show', method='GET')
def men():
    #Get GET parameters(username, men)
    username = request.query.username
    men = request.query.men

    #Controller part=======================================
    if (username == ""):
        username = "nameless person"

    if men in {"ramen"}:
        menname = "ramen"
    elif men in {"soba"}:
        menname = "Soba"
    elif men in {"udon"}:
        menname = "Udon"
    else:
        menname = "Error!!"

    #View section=============================================
    # views/show.Call tpl
    return template('show', name=username, men=menname)


#Run built-in server
run(host='localhost', port=8080, debug=True, reloader=True)
  1. views/title.tpl This is the title page. By default, Bottle reads files under the views directory.

views/title.tpl


<!DOCTYPE html>
<html lang=ja>
  <head>
    <meta charset="UTF-8">
    <title>Title page</title>
  </head>

  <body>
    <h1>Title page</h1>

    <form method="GET" action="/show">
    <p>Name is? :<br>
    <input type="text" name="username"></p>

    <p>What is your favorite noodle?</p>
    <form>
    <select name="men">
      <option value="ramen">ramen</option>
      <option value="soba">Soba</option>
      <option value="udon">Udon</option>
    </select>
    <input type="submit" value="Send">
    </form>
  </body>

</html>
  1. views/show.tpl This page receives parameters from the title page and outputs the results. The parameters of the GET method are received as variables. (name and men)

views/show.tpl


<!DOCTYPE html>
<html lang=ja>
  <head>
    <meta charset="UTF-8">
    <title>Result output page</title>
  </head>

  <body>
    <h1>Result output page</h1>
    <p>Welcome{{name}}San! !!</br>
Next time together{{men}}Let's go eat!<p>
    <a href="/">return to HOME</a>
    </p>
  </body>

</html>

5. Run the build-in server

You can run it on the build-in server by doing python index.py. Type localhost: 8080 in your browser to open the title page.

$ pwd
(Depends on your environment)/bottle_study
$ ls
bottle.py	index.py	view/
$ python index.py

Next, run on the rental server

Next, let's run it with the lollipop plan of the lollipop server. What this is like

So, there are many restrictions, but I tried to see if a bottle with only one file would work.

file organization

The file structure of the lollipop server. This time, the root page of the rental server version sample is "http: // (lollipop server page) /bottle_study/index.cgi", so I placed "bottle_study" in the project directory under the document root.

Document root
| .htaccess   #Added this time
- bottle_study
  - views/
    | title.tpl   #No change
    | show.tpl    #No change(Return to HOME path/ → /bottle_study)
  | bottle.py      #No change
  | index.cgi      # index.py → index.cgi, fix required

Fixed points

  1. index.py (Python execution path, server type specification, extension cgi, permission 700)
  2. With .htaccess, redirect the path back to the root directory to `` `bottle_study / index.cgi / show```

1.index.cgi(index.py→) The following 4 points have been fixed.

index.cgi(Only the difference from the local version)


#!/usr/bin/python2.7

…

run(server='cgi')

2..htaccess

When transitioning from the title page to the result display page, the URL of the expected result display page is

http://(Lollipop server page)/bottle_study/index.cgi/show?username=yamjack&men=ramen

However, if this is left as it is

http://(Lollipop server page)/show?username=yamjack&men=ramen

And it seems to return to the page of the document root, and the page cannot be found. Redirect this with the document root .htaccess to get to the expected page.

.htaccess


RewriteEngine on
RewriteRule ^show(.*)$ http://(Lollipop server page)/bottle_study/index.cgi/show$1

Don't forget to put a line break only in the last line of .htaccess and set the permission to 704 for lollipop servers.

Now it behaves the same as it did locally. The ones I make are for personal study at the moment, so I'll try them out in this environment until I can make something that I want to show to others!

Recommended Posts

Try running Python's web framework Bottle on a lollipop server (lollipo plan)
Build a web server on your Chromebook
[Part 2] Let's build a web server on EC2 Linux
Try running a Django application on an nginx unit
[Introduction to AWS] A memorandum of building a web server on AWS
I made a scaffolding tool for the Python web framework Bottle