try:
pass
except Exception:#捕捉异常
pass
else:#无异常时执行
pass
finally:#无论对错都会执行
pass
#自定义错误
class EgonException(BaseException):
def __init__(self,msg):
self.msg=msg
def __str__(self):
return self.msg
assert expression#断言失败则退出执行
socket
服务端:
import socket
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#socket.AF_INET基于网络,.AF_UNIX基于文件
#socket.SOCK_STREAM基于TCP,.SOCK_DGRAM基于UDP
server.bind(('127.0.0.1',8080))#绑定端口
server.listen(5)#最大挂起数5
conn,client_addr=server.accept()
client_msg=conn.recv(1024)#接收数据一次1024bytes
print('client_msg:',client_msg.decode('utf-8 '))
conn.send('滚'.encode'utf-8')
conn.close()
server.close()
客户端:
import socket
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(('127.0.0.1',8080))#连接
client.send('whatever'.encode('utf-8'))
client.close()