Learning content:
Python Module Introduction
1. Classic class or new type
2. Abstract interface
3. Static methods, class methods, property methods
4. Reflection
5. Exception Handling
6, socket programming first knowledge
7, using the socket to achieve get, put files and other functions
I. Classic class or New class
Second, abstract interface
3. Static methods, class methods, property methods
4. Reflection
5. Exception Handling
The try/except statement is used to detect errors in a try statement block, allowing the except statement to catch exception information and handle it.
If you do not want to end your program when an exception occurs, simply capture it in a try.
Grammar:
The following is a simple syntax for try....except...else :
Try:< statement > #运行别的代码except< name:< statement >#如果在try部份引发了 ' name ' exception except< name >,< data:< statement >#如果引发了 ' name ' exception, get additional data else:< Statement > #如果没有异常发生
Custom exceptions
6, socket programming first knowledge
Server-Side instances:
Import socket
Server = Socket.socket (socket.af_inet,socket. SOCK_STREAM)
Print (' created instance ... ')
Server.bind (' 0.0.0.0 ', 8000)
Print (' Bound ... ')
Server.listen (5)
Print (' Started listening ... ')
While True:
CONN,ADDR = Server.accept ()
Print (CONN,ADDR)
While True:
Try
data = CONN.RECV (1024)
Print (Data.decode ())
Conn.send (b ' I had received your message ' +data)
Except Connectionreseterror as E:
Print (e)
Break
Server.close ()
Client instance:
Import socket
Client = Socket.socket ()
Client.connect ((' localhost ', 8001))
While True:
msg = input (' >>: '). Strip ()
If Len (msg) = = 0:continue
Client.send (Msg.encode ())
Total_size = CLIENT.RECV (1024)
Receive_size = 0
Data_all = B '
Print (Total_size.decode ())
While receive_size < int (Total_size.decode ()):
data = CLIENT.RECV (1024)
Receive_size +=len (data)
Print (' Received data length: ', receive_size)
Data_all +=data
Print (Data_all.decode ())
Print (' Total data length: Received data length ', Total_size.decode (), Len (Data_all))
Client.close ()
7, using the socket to achieve get, put files and other functions
Python Learning Path _python Basics (8)