P.41 03-01 SimpleHTTPServer を使う
標準ライブラリに含まれる SimpleHTTPServer を使って簡単な Web サーバを作ります。
SimpleHTTPServer はファイルや画像を扱う機能しか持っていません
- http://docs.python.org/lib/module-SimpleHTTPServer.html
- http://www.python.jp/doc/release/lib/module-SimpleHTTPServer.html
インタラクティブシェルを使って Web サーバを動かしてみる
- インタラクティブシェルを起ち上げる
- コードを入力する
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test() - Web サーバが立ち上がる
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test()
Serving HTTP on 0.0.0.0 port 8000 ... - ブラウザで http://127.0.0.1:8000/ へアクセス
(インタラクティブシェルを立ち上げた際の作業ディレクトリのファイル一覧が表示される) - メッセージが出力される
>>> import SimpleHTTPServer
>>> SimpleHTTPServer.test()
Serving HTTP on 0.0.0.0 port 8000 ...
localhost - - [11/Dec/2007 01:25:48] "GET / HTTP/1.1" 200 -
localhost - - [11/Dec/2007 01:25:49] code 404, message File not found
localhost - - [11/Dec/2007 01:25:49] "GET /favicon.ico HTTP/1.1" 404 -
最後の1行、favicon を取得しようとしてるところ("Get /favicon 〜)以外は本の通りでした。
- Ctrl + c でサーバを停止
- なんかダラダラと出力されてアセる
Traceback (most recent call last):
File "", line 1, in
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SimpleHTTPServer.py", line 214, in test
BaseHTTPServer.test(HandlerClass, ServerClass)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/BaseHTTPServer.py", line 574, in test
httpd.serve_forever()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 201, in serve_forever
self.handle_request()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 217, in handle_request
request, client_address = self.get_request()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/SocketServer.py", line 374, in get_request
return self.socket.accept()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/socket.py", line 167, in accept
sock, addr = self._sock.accept()
KeyboardInterrupt
Python スクリプト、HTML ファイルを扱う
- P.43 図03 と同様に、python スクリプトファイルと HTML ファイルを作成する
- simpleserver.py
import simpleHTTPServer
SimpleHTTPServer.test() - index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>SimpleHTTPServer</title>
</head>
<body>
<p>Python is awesome !</p>
</body>
</html>
- simpleserver.py
- ターミナルで作成したファイルのあるディレクトリをカレントディレクトリへ移動する
- コマンドを入力してサーバを起ち上げる
$ python simpleserver.py
- エラー ><
「simpleHTTPServer なんて名前のモジュールは無ぇよ」って言われました。最初の「s」は大文字の「S」Traceback (most recent call last):
File "simpleserver.py", line 1, in
import simpleHTTPServer
ImportError: No module named simpleHTTPServer - python スクリプトを修正
- simpleserver.py
import SimpleHTTPServer
SimpleHTTPServer.test()
- simpleserver.py
- (気を取り直して、)コマンドを入力してサーバを起ち上げる
$ python simpleserver.py
- (無事、)Web サーバが起ち上がる
$ python simpleserver.py
Serving HTTP on 0.0.0.0 port 8000 ... - ブラウザで http://127.0.0.1:8000/ へアクセス
- 「Python is awesome !」
今日はここまで。