python笔记
动态模块导入:
x =importlib.import_module('??')
try
...
except(ValueError,KeyError) as e:
except Exception as e:放在最后处理所以其他异常
else:没发生异常执行
finally:无论如何都执行
raise 自定义异常触发
断言: if type(??) is str
socket:
tcp/ip send,recv
udp
family address
AF.INET ipv4
AF.INET6 ipv6
AF.UNIX local
socket protoca
l type
sock.SOCK_STREAM tcp/ip
sock.SOCK_DGRAM UDP
sock.SOCK_RAW
服务端:
server = socket.socket(AF.INET,SOCK_STREAM) server.bind(localhost,9999) server.listen() while True: conn,addr = server.accept() #阻塞 while True: print("new conn",addr) data = conn.recv(1024) #revc默认是阻塞的 if not data: break #客户端已断开,conn.revc print(data) conn.send(data.upper()) #客户端已断开,conn.revc收到的都是空数据以为没断开
客户端:
client = socket.socket() client = connect(serverip,9999) client = send(data) client = revc(1024)