Python Learning Path _python Basics (8)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.