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.
On the title page, enter the "name", select "favorite noodles", and display the parameters passed by the GET method on the result output page.
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
Get bottle.py
Create index.py
Create views / title.tpl
Create views / show.tpl
Run the built-in server
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
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)
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>
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>
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, 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.
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
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