Python Notes-8

Source: Internet
Author: User

Object-oriented extension

1, isinstance (obj, CLS), Issubclass (sub, super) example

Class A (object):


def a (self):

Print "A"



Class B (A): #B继承A


def b (self):

Print "B"


t = B ()

Print isinstance (t, A) #判断t是否为A的实例对象

Print isinstance (t, B) #判断t是否为B的实例对象

Print Issubclass (B, A) #判断B是否为A的派生类


2. Exception handling


the complete structure of the exception

Try

Pass #主代码块在这里执行

Except

Pass #出现异常时, execute the statement here

Else

Pass #主代码块执行完毕之后, execute the statement here

Finally

Pass #不管是否有异常 and the results will eventually be output


Custom Exceptions

"" "__str__ Method" ""

Class Myerror (Exception):


def __init__ (self, msg):

Self.message = Msg


def __str__ (self):

Return Self.message


Try

# "HTTP Error" is an incoming message message, the __str__ method is called when an exception is generated, and an incoming # HTTP error is returned

Raise Myerror ("HTTP Error")

Except Exception, E:

Print E


3. Reflection

"" "The First Method: Reflection (for a huge number of pages, depending on the URL to return different pages)" ""

Print "Welcome, My Web"

url = raw_input ("/http")

Controller, action = Url.split ("/")

Func = GetAttr (home, action)

ret = func ()

Print ret


"" "The second method, matching each different URL, returns a different page" ""

Print "Welcome, My Web"

url = raw_input ("/http")

If url = = "Home/home":

Print Home.home ()

Elif url = = "Home/page":

Print Home.page ()

Elif url = = "Home/row":

Print Home.row ()

Else

print "Wrong url"


GetAttr, SetAttr, Hasattr, delattr

Print dir (home)

Print hasattr (home, "home")

SetAttr (Home, "Liyang", "gentle men")

Print dir (home)

Delattr (Home, "Liyang")

Delattr (Home, "Row")

Print dir (home)



Results:

[' __author__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' home ', ' page ', ' Row ']

True

[' __author__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' home ', ' Liyang ', ' page ', ' Row ']

[' __author__ ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' home ', ' page ']






"" "Reflection Three, Dynamic import module" ""

Controller, action = raw_input ("http:/"). Split ("/")

#将模块以字符串的形式导入, enter Home/home, get the return value home

module = __import__ (Controller)

Func = GetAttr (module, action)

ret = func ()

Print ret


Results:

Http://home/home

Home


4. Socket programming

"" "Socket-server" ""

def main ():

# Create a Socket object

Sock = Socket.socket (socket.af_inet, socket. Sock_stream, 0)

# Listening port, address in the form of tuples

Sock.bind (' 127.0.0.1 ', 8001)

# Start listening, 5 indicates a connection that can be saved in the queue that is not processed by the server side

Sock.listen (5)

While True:

# Blocking, Deng ....

# until there is a request to connect

print ' .... '

Connection, address = sock.accept ()

# connection, representing the client socket object,

# Address, client IP address

#handle_request (Connection)

#接受客户端传来的信息

BUF = CONNECTION.RECV (1024)

Print BUF

Connection.send ("http/1.1 ok\r\n\r\n")

Connection.send ("Hello, World")

Connection.close ()

if __name__ = = ' __main__ ':

Main ()


"" "" Socket-client: "" "

Import socket

__author__ = ' Liyang '


#创建socket对象

Obj_client = Socket.socket ()

#客户端连接服务器端的地址

Obj_client.connect (("localhost", 8021))

#客户端向服务器端发送的数据

Obj_client.send ("Client")

#客户端接受服务器端的返回值, and output

Server_data = OBJ_CLIENT.RECV (1024)

Print Server_data

#关闭客户端连接

Obj_client.close ()


Python Notes-8

Related Article

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.