LoginSignup
1
0

More than 3 years have passed since last update.

Android Smart Phoneをpythonを使ってWeb Serverにする。

Last updated at Posted at 2020-05-20

Android Smart Phoneをpythonを使ってWeb Serverにする。

Pydroid 3 - IDE for Python 3を使ったけど手を火傷して使用中止!

image.png

image.png

UserLandをGoogle Playよりインストールする。

image.png

image.png

sshでログインする。Rloginを使っています。Port 2022です。

image.png

linux画面が立ち上がります。
image.png

環境整備します。vim editorを使えるようにします。

sudo apt update
sudo apt upgrade -y
sudo apt install -y vim

vim test.c
#include <stdio.h>
int main(){
        printf("hello world\n");
}

hirata@localhost:~$ cc test.c
hirata@localhost:~$ ./a.out
hello world
hirata@localhost:~$

hello worldが出ればOK!

pythonの導入と確認

sudo apt update
sudo apt install python3
sudo apt install python3-pip

hirata@localhost:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

bottoleの導入

sudo pip3 install bottle

テストプログラムの作成

web.py
from bottle import route, run
@route('/')
def root():
    str=""
    for i in range(10):
        str=str+"<h1>Hello %d</h1>"%i
    return str
@route('/hello')
def hello():
    return "<h1>Hello World!</h1>"
run(host='192.168.1.16', port=8080, debug=True)

実行

image.png

Visual Studio 2019で作成されたテンプレートの実行

image.png

.
|-- app.py
|-- routes.py
|-- static
|   |-- content
|   |   |-- bootstrap-grid.css
|   |   |-- bootstrap-grid.css.map
|   |   |-- bootstrap-grid.min.css
|   |   |-- bootstrap-grid.min.css.map
|   |   |-- bootstrap-reboot.css
|   |   |-- bootstrap-reboot.css.map
|   |   |-- bootstrap-reboot.min.css
|   |   |-- bootstrap-reboot.min.css.map
|   |   |-- bootstrap.css
|   |   |-- bootstrap.css.map
|   |   |-- bootstrap.min.css
|   |   |-- bootstrap.min.css.map
|   |   |-- jumbotron.css
|   |   `-- site.css
|   |-- fonts
|   |   |-- glyphicons-halflings-regular.eot
|   |   |-- glyphicons-halflings-regular.svg
|   |   |-- glyphicons-halflings-regular.ttf
|   |   `-- glyphicons-halflings-regular.woff
|   `-- scripts
|       |-- _references.js
|       |-- bootstrap.bundle.js
|       |-- bootstrap.bundle.js.map
|       |-- bootstrap.bundle.min.js
|       |-- bootstrap.bundle.min.js.map
|       |-- bootstrap.js
|       |-- bootstrap.js.map
|       |-- bootstrap.min.js
|       |-- bootstrap.min.js.map
|       |-- jquery-1.10.2.intellisense.js
|       |-- jquery-1.10.2.js
|       |-- jquery-1.10.2.min.js
|       |-- jquery-1.10.2.min.map
|       |-- jquery.validate-vsdoc.js
|       |-- jquery.validate.js
|       |-- jquery.validate.min.js
|       |-- jquery.validate.unobtrusive.js
|       |-- jquery.validate.unobtrusive.min.js
|       |-- modernizr-2.6.2.js
|       |-- respond.js
|       `-- respond.min.js
`-- views
    |-- about.tpl
    |-- contact.tpl
    |-- index.tpl
    `-- layout.tpl
app.py
"""
This script runs the application using a development server.
"""

import bottle
import os
import sys

# routes contains the HTTP handlers for our server and must be imported.
import routes

if '--debug' in sys.argv[1:] or 'SERVER_DEBUG' in os.environ:
    # Debug mode will enable more verbose output in the console window.
    # It must be set at the beginning of the script.
    bottle.debug(True)

def wsgi_app():
    """Returns the application to make available through wfastcgi. This is used
    when the site is published to Microsoft Azure."""
    return bottle.default_app()

if __name__ == '__main__':
    PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
    STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static').replace('\\', '/')
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555

    @bottle.route('/static/<filepath:path>')
    def server_static(filepath):
        """Handler for static files, used with the development server.
        When running under a production server such as IIS or Apache,
        the server should be configured to serve the static files."""
        return bottle.static_file(filepath, root=STATIC_ROOT)

    # Starts a local test server.
    HOST,PORT="192.168.1.16",8080
    bottle.run(server='wsgiref', host=HOST, port=PORT)

routes.py
"""
Routes and views for the bottle application.
"""

from bottle import route, view
from datetime import datetime

@route('/')
@route('/home')
@view('index')
def home():
    """Renders the home page."""
    return dict(
        year=datetime.now().year
    )

@route('/contact')
@view('contact')
def contact():
    """Renders the contact page."""
    return dict(
        title='Contact',
        message='Your contact page.',
        year=datetime.now().year
    )

@route('/about')
@view('about')
def about():
    """Renders the about page."""
    return dict(
        title='About',
        message='Your application description page.',
        year=datetime.now().year
    )

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0