職案人

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

CGIを作る

2021年04月30日 | Python
CGIで作る動的なWeb

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

【CGIの準備】
・プログラムを保存する「cgi-bin」フォルダーを作る
Microsoft Windows [Version 10.0.19042.928]
(c) Microsoft Corporation. All rights reserved.
C:\Users\shyok>cd/d D:

D:\>cd D:\pg\Python38

D:\pg\Python38>mkdir cgi-bin

D:\pg\Python38>cd cgi-bin

D:\pg\Python38\cgi-bin>

・「my_first_cgi.py」ファイルにプログラムを書き、フォルダーに保存する
サンプルコード
#!/usr/bin/env python←シェバン行

print('はじめましてのCGI')

・実行
下記のように表示されればOK
D:\pg\Python38\cgi-bin>.\my_first_cgi.py
はじめましてのCGI

・「my_first_cgi.py」ファイルのプログラムを変更する
サンプルコード
#!/usr/bin/env python

html_body = ’’’<!DOCTYP html>
<html lang =" "ja">
<head>
<meta charset="utf-8">
<title>CGI</title>

</head>
<body>
はじめましてのCGI
</body>
</htnl>'’’

print('Content-type: text/html')
print('')
print(html_body)
・コマンドの実行
D:\pg\Python38\cgi-bin>.\my_first_cgi.py

・コマンドプロンプトに表示される
Content-type: text/html
<html lang =" "ja">
<head>
<meta charset="utf-8">
<title>CGI</title>

</head>
<body>
はじめましてのCGI
</body>
</html>
D:\pg\Python38\cgi-bin>

【HTTPアクセスに応じてプログラムを動かす】
Python内臓のWebサーバをCGIに対応させる。
一旦、コマンドプロンプトを閉じ
D:\pg\Python38>python -m http.server --cgi
Serving HTTP on :: port 8000 (http://[::]:8000/) ...

・CGIに対応させたWebサーバを起動させ、WebブラウザにURLを入れる
http://localhost:8000/cgi-bin/my_first_cgi.py

※文字化けするを起こした場合、「my_first_cgi.py」のコードを下記のように変更すると文字化けが直る
#!/usr/bin/env python
#文字化け防止
import sys←追加
import io←追加
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')←追加

html_body = ’’’<!DOCTYP html>
<html lang =" "ja">
<head>
<meta charset="utf-8">
<title>CGI</title>

</head>
<body>
はじめましてのCGI
</body>
</htnl>'’’

print('Content-type: text/html')
print('')
print(html_body)

・実行するとWebブラウザに
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

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でシェアする

継承とオブジェクト指向プログラミング

2021年04月25日 | Python
継承とオブジェクト指向

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

【オブジェクト指向プログラミング】
オブジェクト指向とは
→クラスのように、メンバー変数とメソッド関数のまとまりをオブジェクトと言い、それらを使ったプログラミングをオブジェクト指向プログラミングと言う

【継 承】
継承とは
→元となるクラスの機能を受け継ぎながら、そこに別の機能を付け足して新たなクラスを作ったものを継承という。

・クラス継承の書式
class クラス名(基底クラス):
# クラス定義の本体


継承サンプル1
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.
>>> import turtle
>>> class kame(turtle.Turtle):←継承
pass

>>> kame_test = kame()
>>> kame_test.forward(100)
>>>


【継承とメソッド】
上の継承サンプルでは亀に成って無いので、次のようなコードを追加する。
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.
>>> import turtle
>>> class kame(turtle.Turtle):
pass

>>> kame_test = kame()
>>> kame_test.forward(100)
#追加
>>> kame_test.shape('turtle')
>>> kame_test.shapesize(2.2)
>>>


初期化メソッドを使う
「kame.py」で下記のコードを書き、保存し、モジュールファイルを作る
import turtel

class kame(turtle.Turtle):
def __init__(self):←これだけでは親クラスに初期化される
self.shape('turtle')
self.shapesize(2,2)

インタラクティブシェルで
>>> kame_test = kame.kame()
Traceback (most recent call last):
File "", line 1, in <module>
kame_test = kame.kame()
File "D:\pg\Python38\kame.py", line 6, in __init__
self.shape('turtle')
File "D:\pg\Python38\lib\turtle.py", line 2776, in shape
if not name in self.screen.getshapes():
AttributeError: 'kame' object has no attribute 'screen'
>>>
>>>
改造する
import turtle

class kame(turtle.Turtle):
def __init__(self):
super().__init__()
self.shape('turtle')
self.shapesize(2,2)
インタラクティブシェルで実行する
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.
>>> import kame
>>> kame_test = kame.kame()
>>> kame_test.forward(100)

コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

クラスとアトリビュート(属性)

2021年04月20日 | Python
クラスとアトリビュート(属性)

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

【アトリビュートについて】
下記のクラスでは、2つのインスタンスではアトリビュートの値は同じ
・例文1
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.
>>> class MyClass:
num = 3


>>> c1 = MyClass()
>>> c2 = MyClass()
>>>
>>> c1.num
3
>>> c2.num
3
>>>
・例文2
>>> import MyClass
>>> c1 = MyClass.MyClass()
>>> c2 = MyClass.MyClass()
>>> c1.set_num(5)
>>> c1.my_num
5
>>> c2.set_num(8)
>>> c2.my_num
8
>>>
>>> c1.num
3
>>> c2.num
3
※MyClass.pyにクラスMyClassを保存した
class MyClass:
num = 3

def set_num(self,val):
self.my_num = val


例文3:インスタン属性とクラス属性
class MyClass:
num = 3→クラス属性
def set_num(self,val):
self.my_num = val
の所を
def set_num(self,val):
self.num = val→インスタン属性
に変えると
>>> c1.num
5
>>> c2.num
8
に成り、クラス属性が見えなくなってしまう
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする

サイコロゲーム

2021年04月19日 | Python
サイコロゲーム

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

【正多面体で無い時エラーを表示するには】
「dice.py」のクラスDiceを改造する
import random

class Dice:

def __init__(self,val = 6):
self.face_num = val

def shoot(self):
return random.randint(1,self.face_num)

改造後
import random

class Dice:

def __init__(self,val=6):
if val in [4,6,8,12,20]:
self.face_num = val
else:
raise Exception('そんな多面はありません')

def shoot(self):
return random.randint(1,self.face_num)

実行してみる
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.
>>> import dice
>>> sai = dice.Dice()
>>> sai.face_num
6
>>> sai = dice.Dice(7)
Traceback (most recent call last):
File "", line 1, in <module>
sai = dice.Dice(7)
File "D:\pg\Python38\dice.py", line 9, in __init__
raise Exception('そんな多面はありません')
Exception: そんな多面はありません
>>>
【サイコロゲーム】
4、6、8、12、20面体サイコロのどれか1つを選んで、そのサイコロでコンピュータと勝負します。勝敗は、出た目の大きさで決める簡単なゲーム

・サイコロゲームのソースコードを「dice_game.py」ファイルに書く
import dice

num = input('4、6、8、12、20のどれで勝負しますか?:')#input関数で値を受け入れる
num = int(num) #文字列を整数に変換
my_dice = dice.Dice(num) #ユーザー用のサイコロ
cpu_dice = dice.Dice(num) #コンピュータ用のサイコロ

my_pip = my_dice.shoot() #pipはサイコロの目の意味
cpu_pip = cpu_dice.shoot() #コンピュータの出た目

#出目を画面に出力 数字はstr関数を使って文字列に変更
print('cpu: {}/あなた:{}'.format(cpu_pip,my_pip))

#状況によってメッセージを変える
if my_pip > cpu_pip:
print('おめでとうございます。あなたの勝ちです!')

elif my_pip < cpu_pip:
print('残念!あなたの負けです。')

else:
print('引き分けです')

実行
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/dice_game.py ========================
4、6、8、12、20のどれで勝負しますか?:8
cpu: 6/あなた:1
残念!あなたの負けです。
>>>
コメント
  • X
  • Facebookでシェアする
  • はてなブックマークに追加する
  • LINEでシェアする