職案人

求職・歴史・仏教などについて掲載するつもりだが、自分の思いつきが多いブログだよ。適当に付き合って下さい。

Webアプリを作る

2021年04月28日 | Python
Webアプリを作る

【開発環境】
OS:Win10(64ビット)
言語:Python3.8.5(64bit)
Python の統合開発環境:IDLE
IDLEの操作は別サイト参照のこと

【Webサーバの起動】
コマンドプロンプトでWebサーバを起動する
Microsoft Windows [Version 10.0.19042.928]
(c) Microsoft Corporation. All rights reserved.
C:\Users\shyok>

Dドライブへ移動する
C:\Users\shyok>cd/d D:
D:\>CD D:\pg\Python38

Webサーバを起動する
D:\pg\Python38>python -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
::1 - - [27/Apr/2021 11:04:49] "GET / HTTP/1.1" 200 -

簡単なHTML文をテキストエディタで「index.html」ファイルを書き、カレントフォルダーに保存する
「index.html」のコード
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
Hello!
</body>
</html>

・表示させる
Webブラウザを立ち上げ、アドレスバーに「http://localhost:8000/」を入力すると
「index.html」が表示される


【HTTP通信の中身を見る】
1、Webサーバを接続する
D:\pg\Python38>python -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

2、pythonアプリ「Web.py」を書く
import telnetlib
tn = telnetlib.Telnet('localhost',8000)
tn.write(b'GET /index.html HTTP/1.1\r\n\r\n')
res = tn.read_all()
print(res.decode('utf-8'),end = '')

3.実行する
Python 3.8.9 (tags/v3.8.9:a743f81, Apr 2 2021, 11:10:41) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
============================ RESTART: D:/Data/Web.py ===========================
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.8.9
Date: Wed, 28 Apr 2021 04:47:22 GMT
Content-type: text/html
Content-Length: 132
Last-Modified: Mon, 26 Apr 2021 02:17:06 GMT
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
Hello!
</body>
</html>
>>>
以上になる。

コメント    この記事についてブログを書く
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする
« 継承とオブジェクト指向プロ... | トップ | CGIを作る »
最新の画像もっと見る

コメントを投稿

Python」カテゴリの最新記事