Python之路,day9-Python基础
回顾:
抽象方法
@staticmethod 不能访问类的任何属性
@classmethod 类方法 只能访问公有属性
@property 属性方法 , 把一个方法变成一个静态属性
def sayhi()
pass
@sayhi.setter
def sayhi(k):
@sayhi.delter
def sayhi():
__call__() : 实例 + ()会触发call method
__dict__ 打印实例中所有属性
__getitem__ 以字典的形式操作实例
__new__ 先于 __init__执行, 可以在new中自定义类的实例化过程
__str__ 返回实例的字符串形式
__metaclass__ 元类,
type() 可以动态创建一个类
try ... except
IOError
ValueError
IndexError
IndentationError
TypeError
try
except (IndexError,IndentationError) as e:
print(e)
else:
raise
assert
__import__('day9.testmod')
hasattr
getattr
setattr
delattr
**********************************
socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
s.listen(5)
conn,clientaddr = s.accept
conn.send
推荐书籍:
林达美国
百年孤独
追风筝的人
三杯茶
白鹿原
平凡的世界
三体
浪潮之巅
数学之美
推荐电影:
华尔街之狼
上帝之城
焦土之城
绝美之城
阿甘正传
辛德勒的名单
肖申克的救赎
勇敢的心
角斗士
飘 乱世佳人 after all , tomorrow is another day
血钻
卢旺达饭店
战争之王
教父
楚门的世界
革命之路
帝国的覆灭
当幸福来敲门
千与千寻
哈尔的移动城堡
龙猫
百元之恋
***********************************************************8
sock_server.py
1 import socket 2 import json 3 s = socket.socket() 4 s.bind(('localhost',9000)) 5 s.listen(5) 6 print('start listen.......') 7 while True: 8 conn,client_addr = s.accept() 9 print('got a new conn:',client_addr) 10 11 while True: 12 data = conn.recv(1024) 13 print('recv data',data) 14 data = json.loads(data.decode()) 15 16 if data.get('action') is not None: 17 if data['action'] == 'put': 18 #client sends file to server 19 file_obj = open(data['filename'],'wb') 20 received_size = 0 21 22 while received_size < data['size']: 23 recv_data = conn.recv(1024) 24 file_obj.write(recv_data) 25 received_size += len(recv_data) 26 print(data['size'],received_size) 27 else: 28 print('successfully receviced file [%s]---'%data['filename']) 29 file_obj.close() 30 elif data['action'] == 'get': 31 # client downlands file from server 32 pass
sock_client.py
1 import socket 2 import os 3 import json 4 client = socket.socket() 5 client.connect(('localhost',9000)) 6 7 while True: 8 choice = input('>>').strip() 9 if len(choice) == 0:continue 10 cmd_list = choice.split() 11 if cmd_list[0] == 'put': 12 if len(cmd_list) == 1: 13 print('no filename follows after put cmd.') 14 continue 15 filename = cmd_list[1] 16 if os.path.isfile(filename): 17 file_obj = open(filename,'rb') 18 base_filename = filename.split('/')[-1] 19 print(file_obj.name,os.path.getsize(filename)) 20 data_header = { 21 'action':'put', 22 'filename':base_filename, 23 'size':os.path.getsize(filename) 24 } 25 client.send( json.dumps(data_header).encode()) 26 for line in file_obj: 27 client.send(line) 28 print('----send file down-------') 29 30 else: 31 print('file is not vaild.') 32 continue 33 elif cmd_list[0] == 'get': 34 pass
socket_server.py
1 import socketserver 2 3 class MyTCPHandler(socketserver.BaseRequestHandler): 4 def handle(self): 5 while True: 6 self.data = self.request.recv(1024).strip() 7 print(self.client_address[0]) 8 print(self.data) 9 self.request.send(self.data.upper()) 10 11 if __name__ == '__main__': 12 host,port = 'localhost',9000 13 14 server = socketserver.TCPServer((host,port),MyTCPHandler) 15 16 server.serve_forever()