3
4

More than 5 years have passed since last update.

Javaの実行結果をbottle.pyでWebに表示する

Last updated at Posted at 2014-11-13

ちょっと前にEclipseを使ってJavaのプログラムを書いたのだが、これをWebにするのが面倒そうだったので、Pythonのbottle.pyというフレームワークを使ってjava hoge.hoge.com.HelloWorldというコマンドを実行して標準出力を表示するサーバを作ってみた。

import os
from bottle import route, run, template

@route('/java')
def index():
    os.chdir("c:\\workspace\\HelloWorld\\bin")
    s = os.popen("java hoge.hoge.com.HelloWorld")
    return template('{{output}}', output=s.read())

run(host='127.0.0.1', port=8080)

ちなみにbottle.pyは、こんな感じで書けばWebサーバにできる軽量サーバ。ちょっとした確認に便利そう。というかJavaは苦手っす。

from bottle import route, run, template

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)
3
4
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
3
4