11_Python&Networking
100周年ホール 11/28 5限 大学院紹介ガイダンスが100周年ホールであります。是非きてください。
python3環境をインストールしてきてください。
macの人 brew編
https://qiita.com/7110/items/1aa5968022373e99ae28
windowsの人
https://qiita.com/kituneazami/items/4d2db7726bdc0eb37f2b
linux or mac (apt編)
http://www.python.ambitious-engineer.com/archives/7
(0)python coding Styleの参考にあとで見返してください。
開発にはatomを利用しましょうか。
(1)terminal,cmdでpython3と打つ
python3 -V
python3の環境からでるのはexit()
(2)Pythonで数値計算
python3
x=3
y=4.5
print(x)
print (x+1) # 加算
print (x - 1) # 減算
print (x * 2) # 乗算
print (x ** 2) # 冪乗 x^2
print (x+y)
(3 + 2j)-(6 + 4j) #虚数
(2)Type(型)を見てみよう
type(x)
type(y)
t = True
f = False
type(t) #=> "<type 'bool'>"
print (t and f) # 論理積; "False"
print (t or f) # 論理和; "True"
print (not t) # 否定; "False"
print (t != f) # 排他的論理和; "True"
(3)文字列処理
hello = 'hello' # シングルクォートか、
world = "world" # ダブルクォートで文字列を表す
print (hello) #=> "hello"
print (len(hello)) # 文字列の長さ;=> "5"
hw = hello + ' ' + world # 文字列の結合
print (hw) #=> "hello world"
hw12 = '%s %s %d' % (hello, world, 12) # 文字列の代入
print (hw12) #=> "hello world 12"
nihongo = u'日本語' # 明示的にUnicode型を作るにはuをつける
print (nihongo) #=> '日本語'
a = 10 #整数型(int)
b = "みかんの数は"
sum = b + str(a)
print(sum)
b = "hoge"*10 #文字列繰り返し
print(b)
(4)文字列操作
s = "hello"
print (s.capitalize()) #=> "Hello"
print (s.upper()) #=> "HELLO"
print (s.rjust(7)) # 右寄せ;=> " hello"
print (s.center(7)) # 中央寄せ;=> " hello "
print (s.replace('l', '(ell)')) # 部分文字列の置換;=> "he(ell)(ell)o"
print (' world '.strip()) # 空白の削除;=> "world"
その他の文字列メソッド
http://docs.python.jp/3.6/library/stdtypes.html#string-methods
(4.5)三重引用符他
moji = """
明日は何しようか。
pythonの勉強をしよう"""
moji
#文字列の長さを取得
moji = "python面白いね"
len(moji)
#3つだけ文字列を入れ替える
b = "aaaaaaaaaaaaaaaa"
b.replace("a","love",3)
#.formatの便利な使い方{0} に 変数1の値 、{1} に変数2の値 が割り当てられます。
sokudo = 30 #[km/h]
kyori = 120 #[km]
zikan = kyori /sokudo
ans = "{0}[km/h]で、{1}[km]を走るとすると、{2}[h]かかります。".format(sokudo,kyori,zikan)
print(ans)
#入力させる
tate = float(input("縦の長さは何[cm]?"))
yoko = float(input("横の長さは何[cm]?"))
s = tate * yoko
print("面積 s =",s,"[cm^2]です")
#上記プログラムをmenseki.pyと保存してpythn3 mesenki.pyの実行できます。
(5)リスト構造
リストはJavaで言う所の配列に相当します。ただし、リストはサイズを変えることができ、また複数のデータ型を含むことができます。
xs = [3, 1, 2] # リスト作成
print (xs, xs[2]) #=> "[3, 1, 2] 2"
print (xs[-1]) # 負のインデックス値は後ろから数えます;=> "2"
xs[2] = 'foo' # リストには複数のデータ型を混ぜられます
print (xs) #=> "[3, 1, 'foo']"
xs.append('bar') # データを後ろに追加します
print (xs) #=> "[3, 1, 'foo', 'bar']"
x = xs.pop() # データを後ろから取り除きます
print (x, xs) #=> "bar [3, 1, 'foo']"
その他のList操作:
https://docs.python.org/ja/3.6/tutorial/datastructures.html#more-on-lists
(6)Listのスライス
nums = range(5) # rangeは整数範囲を作成する組み込み型関数です
nums = [0, 1, 2, 3, 4] //整数配列
print (nums) #=> "[0, 1, 2, 3, 4]"
print (nums[2:4]) # インデックス2以上4未満;=> "[2, 3]"
print (nums[2:]) # インデックス2以上最後まで;=> "[2, 3, 4]"
print (nums[:2]) # 最初から2未満;=> "[0, 1]"
print (nums[:]) # 全要素;=> ["0, 1, 2, 3, 4]"
print (nums[:-1]) # 負のインデックスも可能;=> ["0, 1, 2, 3]"
moji = "ABCDEF"
moji[::2] # 2 刻みで全て抽出
# 'ACE'
#indexを使おう
a = ["a","b","c","d"]
a.index("b")
a.index("d")
#リストに値が存在するかどうか
a = ["a","b","c","d"]
"b" in a
"f" in a
#個数をカウント使用
a = ["a","b","c","c","b"]
a.count("c")
a.count("g")
#ソートする
a = [5,2,4,3,0,1]
s = sorted(a)
s
a = ["b","g","a","d"]
s = sorted(a)
s
#降順ソート
a = [5,2,4,3,0,1]
a.sort(reverse = True)
a
#タプルをリストへ変換
a = ("takasi","jun","tomoki")
list(a)
list((1,2,3,4,5))
#タプルアンパック
d = ("python","Java","c")
a,b,c = d
a
b
c
(7)Loop
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print (animal)
#↑改行が最後にいります。
#=> "cat", "dog", "monkey"
(8)Netoworking
#!/usr/bin/python3 # This is server.py file
import socket
# create a socket object
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
# bind to the port
serversocket.bind((host, port))
# queue up to 5 requests
serversocket.listen(5)
cnt=0;
while True:
# establish a connection
clientsocket,addr = serversocket.accept()
print("Got a connection from %s" % str(addr))
msg='Thank you for connecting'+ "\r\n"
clientsocket.send(msg.encode('ascii'))
clientsocket.close()
以下クライアント
#!/usr/bin/python3 # This is client.py file
import socket
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = socket.gethostname()
port = 9999
# connection to hostname on the port.
s.connect((host, port))
# Receive no more than 1024 bytes
msg = s.recv(1024)
s.close()
print (msg.decode('ascii'))
今日の課題:第11回フォルダに提出
(簡単)
pythonのサーバクライアント通信プログラム(tcp)でクライアントからの英語文字列"abc"をサーバおくって、サーバにて全て大文字CAPITALの"ABC"にして表示せよ。
python_server1.py
python_client1.py
(やや難:python経験者におすすめ)
pythonのサーバクライアント通信プログラム(tcp)でクライアントからの英語文字列"abc"などをユーザに入力させその文字列をサーバおくって、サーバにて全て大文字にしてクライアントへ返信せよ。クライアントでは返信内容を表示せよ.
python_server2.py
python_client2.py
(難)上記をマルチスレッドにせよ
来週までに最終課題の企画書を考えてきてください。