Python之路,day8-Python基础
***面向对象的好处***
更容易扩展、提高代码使用效率,使你的代码组织性更强,更清晰
更适合复杂项目的开发 封装
把功能的实现细节封装起来,只暴露调用接口
继承 多态
接口继承 定义
类----》模板
对象---》实例化的模板 属性
私有属性 __private
公有属性 存在类的内存里,所有势力共享
成员属性 ---》实例变量
方法 ---》函数
构造函数
析构函数:实例销毁时,自动执行 静态方法
类方法
属性方法
class Flight(object):
'''我是类的注释'''
def __init__(self, name):
self.flight_name = name def checking_status(self):
print("checking flight %s status " % self.flight_name)
return 1
@property
def flight_status(self):
status = self.checking_status()
if status == 0:
print("flight got canceled...")
elif status == 1:
print("flight is arrived...")
elif status == 2:
print("flight has departured already...")
else:
print("cannot confirm the flight status...,please check later") @flight_status.setter # 修改
def flight_status(self, status):
status_dic = {
0: "canceled",
1: "arrived",
2: "departured"
}
print("\033[31;1mHas changed the flight status to \033[0m", status_dic.get(status)) def __call__(self, *args, **kwargs):
print('--call:',args,kwargs) @flight_status.deleter # 删除
def flight_status(self):
print("status got removed...") f = Flight("CA980")
# f.flight_status
# f.flight_status = 2 # 触发@flight_status.setter
# del f.flight_status # 触发@flight_status.deleter
f('','')
print(f.__doc__)
print(f.__module__)
print(f.__class__)
***异常处理***
一、基本异常处理结构
try:
代码块
except Exception as e:
将错误信息输出写入日志文件 二、复杂结构
try:
。。。
except:
。。。
else:
。。。
finally
。。。
三、异常对象
try:
代码块
except Exception as e:#python内部将错误信息封装到e对象中
将错误信息输出写入日志文件
四、 异常种类
Exception,能将所有的异常都捕获
。。。
其他的处理方法,只能处理某一情况
try:
代码块
except Exception as e:#python内部将错误信息封装到e对象中
将错误信息输出写入日志文件 try:
int('asdd')
list = [1,2]
list[1]
except IndexError as e:
pass
except Exception as e:
pass
else:
print('else')
finally:
print('finally')
======>Exception,其他错误的关系
继承关系
五、主动触发异常 六、断言
assert条件 七、自定义异常 反射
hashattr(容器,名称)#
app.py
import os
import sys BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from controller import account action = input('>>')
if hasattr(account,action):
func = getattr(account,action)
result =func()
else:
result = ''
print(result)
account.py
def login():
return '请输入永户名密码'
def logout():
return '跳转回登陆界面'
***socket***
server.py
import socket
import os
import sys
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(('0.0.0.0',8000))
server.listen(5)
print('start listen')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
new_size = 0
while True:
conn,client_addr = server.accept()
print(conn,client_addr)
num = 0
while True:
try:
data = conn.recv(1024)
if num == 0:
filename = data.decode()
if num == 1:
total_size = int(data.decode())
if num >= 2:
print(filename,'这什么情况')
with open(filename,'ab') as f:
new_size = os.path.getsize(r'%s' % filename)
f.write(data)
print('recv from client:',filename)
conn.send(b'got your message')
num += 1
except Exception as e:
break
client.py
import socket
import os
import sys
import re
# BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
print(sys.path)
print(BASE_DIR)
client = socket.socket()
client.connect(('localhost',8000))
while True:
input_str = input('[put filename]>>').strip()
input_list = re.split(' ',input_str)
msg = input_list[1]
if input_list[0] != 'put':
print('command error:',input_list[0],'input [put filename] command')
continue
if r'\\' not in msg:
if os.path.exists(msg):
filename = msg
else:
print('not found file')
continue
else:
filename = msg
length_file = os.path.getsize(r'C:\Users\heshaochuan\PycharmProjects\py_s15\day8\反射\test.py')
length_file_str = '%s'%length_file
if len(filename) == 0:continue
client.send(filename.encode())
print('send',filename)
data = client.recv(1024)
if data:
client.send(length_file_str.encode())
data1 = client.recv(1024)
if data1:
with open(filename, 'rb') as f:
for i in f:
client.send(i)
data2 = client.recv(1024)
print('recv from server:',data2)
Python之路,day8-Python基础的更多相关文章
- Python之路,Day8 - Python基础 面向对象高级进阶与socket基础
类的成员 类的成员可以分为三大类:字段.方法和属性 注:所有成员中,只有普通字段的内容保存对象中,即:根据此类创建了多少对象,在内存中就有多少个普通字段.而其他的成员,则都是保存在类中,即:无论对象的 ...
- Python之路,Day8 - Socket编程进阶
Python之路,Day8 - Socket编程进阶 本节内容: Socket语法及相关 SocketServer实现多并发 Socket语法及相关 socket概念 socket本质上就是在2台 ...
- Python之路,Day4 - Python基础4 (new版)
Python之路,Day4 - Python基础4 (new版) 本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...
- Python之路,Day1 - Python基础1
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python之路,Day1 - Python基础1(转载Alex)
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python之路,Day1 - Python基础1 --转自金角大王
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- Python之路,Day1 - Python基础1 介绍、基本语法、流程控制
本节内容 1.python介绍 2.发展史 3.python 2.x or python 3.x ? 4.python 安装 5.第一个程序 Hello World 程序 6.变量 7.用户输入 8. ...
- Python之路Day8
摘要: Socket编程 异常处理 线程.进程 1.socket编程 1.1 socket 三次握手,注意阻塞的应用. 1.2 socketserver(2.x写作:SocketServer) 实现多 ...
- Python之路:Python简介
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间他为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承 ...
- Python之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
随机推荐
- Eclipse打不开,提示: An error has occurred. see the log file
解决办法 删除.metadata目录下.plugins/org.eclipse.e4.workbench即可
- iOS推送生成服务器端p12文件
生成服务器端推送p12文件 所需文件:A.开发证书 aps_production.cer B.本地导出的私钥 : aps_production.p12 C.生成证书时用到的请求文件:Push.c ...
- Selenium+Python的环境配置
因为项目的原因,最近较多的使用了UFT来进行自动化测试工作,半年没有使用Selenium了,于是在自己的电脑上重新配置了基于python3.x的selenium环境,配置过程大致如下: 1. Sele ...
- HTML DOM
1.DOM方法 常用 getElementById() 返回带有指定 ID 的元素 getElementsByTagName() 返回包含带有指定标签名称的所有元素的节点列表(集合/节点数组) ge ...
- 【Python】个人所得税
以月收入1w,举例计算个税: #!/usr/bin/python #-*- encoding:UTF-8 -*- #========================================== ...
- maven 配置
四.eclipse配置maven eclipse---window---maven------User Settings: 之前设置的仓库的位置: 五.idea15配置maven idea14---s ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- mac中使用brew安装软件,下载太慢怎么办?
mac中使用brew安装软件,下载太慢怎么办? 本文所说的软件是指较大的软件,如果软件较小,例如软件只有几M,那么使用此方法后,提升会非常小. 了解brew原理: 1: 从网络下载安装包 2: 执行一 ...
- 在VS的EF中连接MySQL
VS没有主动提供那些繁多的连接器,需要的话得自己再安装这些第三方程序包. MySQL为windows平台开发者提供了许多程序包:http://dev.mysql.com/downloads/windo ...
- Java语言的安全性的体现
Java语言的安全性的体现 1.严格遵循面向对象的规范.这样封装了数据细节,只提供接口给用户.增加了数据级的安全性. 2.无指针运算.java中的操作,除了基本类型都是引用的操作.引用是不能进行增减运 ...