zoukankan      html  css  js  c++  java
  • 进击的Python【第七章】:python各种类,反射,异常处理和socket基础

    Python的高级应用(三)面向对象编程进阶

     本章学习要点:

    • 面向对象高级语法部分
      • 静态方法、类方法、属性方法
      • 类的特殊方法
      • 反射
    • 异常处理
    • Socket开发基础

    一、面向对象高级语法部分

    静态方法

      要在类中使用静态方法,需在类成员函数前面加上@staticmethod标记符,以表示下面的成员函数是静态函数。使用静态方法的好处是,不需要定义实例即可使用这个方法。另外,多个实例共享此静态方法。 

    类方法

      类方法与普通的成员函数和静态函数有不同之处,在接触的语言中好像也没见过这种语义,看它的定义: 
      一个类方法就可以通过类或它的实例来调用的方法, 不管你是用类来调用这个方法还是类实例调用这个方法,该方法的第一个参数总是定义该方法的类对象。 
      记住:方法的第一个参数都是类对象而不是实例对象. 
      按照惯例,类方法的第一个形参被命名为 cls.任何时候定义类方法都不是必须的(类方法能实现的功能都可以通过定义一个普通函数来实现,只要这个函数接受一个类对象做为参数就可以了).  
      python中实现静态方法和类方法都是依赖于python的修饰器来实现的。 对象方法有self参数,类方法有cls参数,静态方法是不需要这些附加参数的。

    class TestClassMethod(object):
    
        METHOD = 'method hoho'
    
        def __init__(self):
            self.name = 'leon'
    
        def test1(self):
            print('test1')
            print(self)
    
        @classmethod
        def test2(cls):
            print(cls)
            print('test2')
            print(TestClassMethod.METHOD)
            print('----------------')
    
        @staticmethod
        def test3():
            print(TestClassMethod.METHOD)
            print('test3')
    
    if __name__ == '__main__':
        a = TestClassMethod()
        a.test1()
        a.test2()
        a.test3()
        TestClassMethod.test3()
    

    输出:

    test1为实例方法

    test2为类方法,第一个参数为类本身

    test3为静态方法,可以不接收参数

    类方法和静态方法皆可以访问类的静态变量(类变量),但不能访问实例变量,test2、test3是不能访问self.name的,而test1则可以

    test1
    <__main__.TestClassMethod object at 0x0061BD30>
    <class '__main__.TestClassMethod'>
    test2
    method hoho
    ----------------
    method hoho
    test3
    method hoho
    test3
    

    属性方法

    属性方法的作用就是通过@property把一个方法变成一个静态属性

    Python内置的@property装饰器就是负责把一个方法变成属性调用的:

    class Student(object):
    
        @property
        def score(self):
            return self._score
    
        @score.setter
        def score(self, value):
            if not isinstance(value, int):
                raise ValueError('score must be an integer!')
            if value < 0 or value > 100:
                raise ValueError('score must between 0 ~ 100!')
            self._score = value
    

    @property的实现比较复杂,我们先考察如何使用。把一个getter方法变成属性,只需要加上@property就可以了,此时,@property本身又创建了另一个装饰器@score.setter,负责把一个setter方法变成属性赋值,于是,我们就拥有一个可控的属性操作:

    >>> s = Student()
    >>> s.score = 60 # OK,实际转化为s.set_score(60)
    >>> s.score # OK,实际转化为s.get_score()
    60
    >>> s.score = 9999
    Traceback (most recent call last):
      ...
    ValueError: score must between 0 ~ 100!
    

    注意到这个神奇的@property,我们在对实例属性操作的时候,就知道该属性很可能不是直接暴露的,而是通过getter和setter方法来实现的。

    还可以定义只读属性,只定义getter方法,不定义setter方法就是一个只读属性:

    class Student(object):
    
        @property
        def birth(self):
            return self._birth
    
        @birth.setter
        def birth(self, value):
            self._birth = value
    
        @property
        def age(self):
            return 2016 - self._birth
    

    上面的birth是可读写属性,而age就是一个只读属性,因为age可以根据birth和当前时间计算出来。

    类的特殊方法

    1. __doc__  表示类的描述信息

    class Foo:
        """ 描述类信息,这是用于看片的神奇 """
     
        def func(self):
            pass
     
    print Foo.__doc__
    #输出:类的描述信息
    

    2. __module__ 和  __class__ 

      __module__ 表示当前操作的对象在那个模块

      __class__     表示当前操作的对象的类是什么

    class C:
    
        def __init__(self):
            self.name = 'wupeiqi'
    from lib.aa import C
    
    obj = C()
    print obj.__module__  # 输出 lib.aa,即:输出模块
    print obj.__class__      # 输出 lib.aa.C,即:输出类
    

    3. __init__ 构造方法,通过类创建对象时,自动触发执行。

    4.__del__

     析构方法,当对象在内存中被释放时,自动触发执行。

    注:此方法一般无须定义,因为Python是一门高级语言,程序员在使用时无需关心内存的分配和释放,因为此工作都是交给Python解释器来执行,所以,析构函数的调用是由解释器在进行垃圾回收时自动触发执行的

     5. __call__ 对象后面加括号,触发执行。

    注:构造方法的执行是由创建对象触发的,即:对象 = 类名() ;而对于 __call__ 方法的执行是由对象后加括号触发的,即:对象() 或者 类()()

    class Foo:
     
        def __init__(self):
            pass
         
        def __call__(self, *args, **kwargs):
     
            print '__call__'
     
     
    obj = Foo() # 执行 __init__
    obj()       # 执行 __call__
    

    6.__dict__ 查看类或对象中的所有成员 

    class Province:
     
        country = 'China'
     
        def __init__(self, name, count):
            self.name = name
            self.count = count
     
        def func(self, *args, **kwargs):
            print 'func'
     
    # 获取类的成员,即:静态字段、方法、
    print Province.__dict__
    # 输出:{'country': 'China', '__module__': '__main__', 'func': <function func at 0x10be30f50>, '__init__': <function __init__ at 0x10be30ed8>, '__doc__': None}
     
    obj1 = Province('HeBei',10000)
    print obj1.__dict__
    # 获取 对象obj1 的成员
    # 输出:{'count': 10000, 'name': 'HeBei'}
     
    obj2 = Province('HeNan', 3888)
    print obj2.__dict__
    # 获取 对象obj1 的成员
    # 输出:{'count': 3888, 'name': 'HeNan'}
    

    7.__str__ 如果一个类中定义了__str__方法,那么在打印 对象 时,默认输出该方法的返回值

    class Foo:
     
        def __str__(self):
            return 'alex li'
     
     
    obj = Foo()
    print obj
    # 输出:alex li
    

    8.__getitem__、__setitem__、__delitem__

    用于索引操作,如字典。以上分别表示获取、设置、删除数据

    class Foo(object):
     
        def __getitem__(self, key):
            print('__getitem__',key)
     
        def __setitem__(self, key, value):
            print('__setitem__',key,value)
     
        def __delitem__(self, key):
            print('__delitem__',key)
     
     
    obj = Foo()
     
    result = obj['k1']      # 自动触发执行 __getitem__
    obj['k2'] = 'alex'   # 自动触发执行 __setitem__
    del obj['k1']   
    

    9. __new__ __metaclass__

    class Foo(object):
     
     
        def __init__(self,name):
            self.name = name
     
     
    f = Foo("alex")
    

    反射

     通过字符串映射或修改程序运行时的状态、属性、方法, 有以下4个方法

    class Foo(object):
     
        def __init__(self):
            self.name = 'wupeiqi'
     
        def func(self):
            return 'func'
     
    obj = Foo()
     
    # #### 检查是否含有成员 ####
    hasattr(obj, 'name')
    hasattr(obj, 'func')
     
    # #### 获取成员 ####
    getattr(obj, 'name')
    getattr(obj, 'func')
     
    # #### 设置成员 ####
    setattr(obj, 'age', 18)
    setattr(obj, 'show', lambda num: num + 1)
     
    # #### 删除成员 ####
    delattr(obj, 'name')
    delattr(obj, 'func')
    
    反射代码示例
    

      

     二、异常处理

     Python标准异常

    异常名称	描述
    BaseException	所有异常的基类
    SystemExit	解释器请求退出
    KeyboardInterrupt	用户中断执行(通常是输入^C)
    Exception	常规错误的基类
    StopIteration	迭代器没有更多的值
    GeneratorExit	生成器(generator)发生异常来通知退出
    StandardError	所有的内建标准异常的基类
    ArithmeticError	所有数值计算错误的基类
    FloatingPointError	浮点计算错误
    OverflowError	数值运算超出最大限制
    ZeroDivisionError	除(或取模)零 (所有数据类型)
    AssertionError	断言语句失败
    AttributeError	对象没有这个属性
    EOFError	没有内建输入,到达EOF 标记
    EnvironmentError	操作系统错误的基类
    IOError	输入/输出操作失败
    OSError	操作系统错误
    WindowsError	系统调用失败
    ImportError	导入模块/对象失败
    LookupError	无效数据查询的基类
    IndexError	序列中没有此索引(index)
    KeyError	映射中没有这个键
    MemoryError	内存溢出错误(对于Python 解释器不是致命的)
    NameError	未声明/初始化对象 (没有属性)
    UnboundLocalError	访问未初始化的本地变量
    ReferenceError	弱引用(Weak reference)试图访问已经垃圾回收了的对象
    RuntimeError	一般的运行时错误
    NotImplementedError	尚未实现的方法
    SyntaxError	Python 语法错误
    IndentationError	缩进错误
    TabError	Tab 和空格混用
    SystemError	一般的解释器系统错误
    TypeError	对类型无效的操作
    ValueError	传入无效的参数
    UnicodeError	Unicode 相关的错误
    UnicodeDecodeError	Unicode 解码时的错误
    UnicodeEncodeError	Unicode 编码时错误
    UnicodeTranslateError	Unicode 转换时错误
    Warning	警告的基类
    DeprecationWarning	关于被弃用的特征的警告
    FutureWarning	关于构造将来语义会有改变的警告
    OverflowWarning	旧的关于自动提升为长整型(long)的警告
    PendingDeprecationWarning	关于特性将会被废弃的警告
    RuntimeWarning	可疑的运行时行为(runtime behavior)的警告
    SyntaxWarning	可疑的语法的警告
    UserWarning	用户代码生成的警告

    异常处理

    捕捉异常可以使用try/except语句。

    try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。

    如果你不想在异常发生时结束你的程序,只需在try里捕获它。

    语法:

    以下为简单的try....except...else的语法:

    try:
    <语句>        #运行别的代码
    except <名字>:
    <语句>        #如果在try部份引发了'name'异常
    except <名字>,<数据>:
    <语句>        #如果引发了'name'异常,获得附加的数据
    else:
    <语句>        #如果没有异常发生
    

    try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句先执行,接下来会发生什么依赖于执行时是否出现异常。

    • 如果当try后的语句执行时发生异常,python就跳回到try并执行第一个匹配该异常的except子句,异常处理完毕,控制流就通过整个try语句(除非在处理异常时又引发新的异常)。
    • 如果在try后的语句里发生了异常,却没有匹配的except子句,异常将被递交到上层的try,或者到程序的最上层(这样将结束程序,并打印缺省的出错信息)。
    • 如果在try子句执行时没有发生异常,python将执行else语句后的语句(如果有else的话),然后控制流通过整个try语句。

    实例

    下面是简单的例子,它打开一个文件,在该文件中的内容写入内容,且并未发生异常:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    try:
        fh = open("testfile", "w")
        fh.write("这是一个测试文件,用于测试异常!!")
    except IOError:
        print "Error: 没有找到文件或读取文件失败"
    else:
        print "内容写入文件成功"
        fh.close()
    

    以上程序输出结果:

    $ python test.py 
    内容写入文件成功
    $ cat testfile       # 查看写入的内容
    这是一个测试文件,用于测试异常!!
    

    实例

    下面是简单的例子,它打开一个文件,在该文件中的内容写入内容,但文件没有写入权限,发生了异常:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    try:
        fh = open("testfile", "w")
        fh.write("这是一个测试文件,用于测试异常!!")
    except IOError:
        print "Error: 没有找到文件或读取文件失败"
    else:
        print "内容写入文件成功"
        fh.close()
    

    在执行代码前为了测试方便,我们可以先去掉 testfile 文件的写权限,命令如下:

    chmod -w testfile
    

    再执行以上代码:

    $ python test.py 
    Error: 没有找到文件或读取文件失败
    

    使用except而不带任何异常类型

    你可以不带任何异常类型使用except,如下实例:

    try:
        正常的操作
       ......................
    except:
        发生异常,执行这块代码
       ......................
    else:
        如果没有异常执行这块代码
    

    以上方式try-except语句捕获所有发生的异常。但这不是一个很好的方式,我们不能通过该程序识别出具体的异常信息。因为它捕获所有的异常。

    使用except而带多种异常类型

    你也可以使用相同的except语句来处理多个异常信息,如下所示:

    try:
        正常的操作
       ......................
    except(Exception1[, Exception2[,...ExceptionN]]]):
       发生以上多个异常中的一个,执行这块代码
       ......................
    else:
        如果没有异常执行这块代码
    

    try-finally 语句

    try-finally 语句无论是否发生异常都将执行最后的代码。

    try:
    <语句>
    finally:
    <语句>    #退出try时总会执行
    raise
    

    实例

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    try:
        fh = open("testfile", "w")
        fh.write("这是一个测试文件,用于测试异常!!")
    finally:
        print "Error: 没有找到文件或读取文件失败"
    

    如果打开的文件没有可写权限,输出如下所示:

    $ python test.py 
    Error: 没有找到文件或读取文件失败
    

    同样的例子也可以写成如下方式:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    try:
        fh = open("testfile", "w")
        try:
            fh.write("这是一个测试文件,用于测试异常!!")
        finally:
            print "关闭文件"
            fh.close()
    except IOError:
        print "Error: 没有找到文件或读取文件失败"
    

    当在try块中抛出一个异常,立即执行finally块代码。

    finally块中的所有语句执行后,异常被再次触发,并执行except块代码。

    参数的内容不同于异常

    三、Socket开发基础

    什么是 Socket?

    Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯。

    socket()函数

    Python 中,我们用 socket()函数来创建套接字,语法格式如下:

    socket.socket([family[, type[, proto]]])
    

    实现一个socket至少要分以下几步,(伪代码)

    1 Socket socket = getSocket(type = "TCP")  #设定好协议类型
    2 connect(socket, address = "1.2.3.4", port = "80") #连接远程机器
    3 send(socket, "Hello, world!") #发送消息
    4 close(socket) #关闭连接

    socket API is an application programming interface (API), usually provided by the operating system, that allows application programs to control and use network sockets. Internet socket APIs are usually based on the Berkeley sockets standard. In the Berkeley sockets standard, sockets are a form of file descriptor (a file handle), due to the Unix philosophy that "everything is a file", and the analogies between sockets and files: you can read, write, open, and close both.   

    socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Sockets need not have an address (for example for only sending data), but if a program binds a socket to an address, the socket can be used to receive data sent to that address. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread.

    Socket Families(地址簇)

    socket.AF_UNIX unix本机进程间通信 

    socket.AF_INET IPV4 

    socket.AF_INET6  IPV6

    These constants represent the address (and protocol) families, used for the first argument to socket(). If the AF_UNIX constant is not defined then this protocol is unsupported. More constants may be available depending on the system.

    Socket Types

    socket.SOCK_STREAM  #for tcp

    socket.SOCK_DGRAM   #for udp 

    socket.SOCK_RAW     #原始套接字,普通的套接字无法处理ICMP、IGMP等网络报文,而SOCK_RAW可以;其次,SOCK_RAW也可以处理特殊的IPv4报文;此外,利用原始套接字,可以通过IP_HDRINCL套接字选项由用户构造IP头。

    socket.SOCK_RDM  #是一种可靠的UDP形式,即保证交付数据报但不保证顺序。SOCK_RAM用来提供对原始协议的低级访问,在需要执行某些特殊操作时使用,如发送ICMP报文。SOCK_RAM通常仅限于高级用户或管理员运行的程序使用。

    socket.SOCK_SEQPACKET #废弃了

    These constants represent the socket types, used for the second argument to socket(). More constants may be available depending on the system. (Only SOCK_STREAM and SOCK_DGRAM appear to be generally useful.)

    参数

    socket.socket(family=AF_INETtype=SOCK_STREAMproto=0fileno=None)  必会

    Create a new socket using the given address family, socket type and protocol number. The address family should be AF_INET (the default), AF_INET6AF_UNIXAF_CAN or AF_RDS. The socket type should beSOCK_STREAM (the default), SOCK_DGRAMSOCK_RAW or perhaps one of the other SOCK_ constants. The protocol number is usually zero and may be omitted or in the case where the address family is AF_CAN the protocol should be one of CAN_RAW or CAN_BCM. If fileno is specified, the other arguments are ignored, causing the socket with the specified file descriptor to return. Unlike socket.fromfd()fileno will return the same socket and not a duplicate. This may help close a detached socket using socket.close().

    socket.socketpair([family[, type[, proto]]])

    Build a pair of connected socket objects using the given address family, socket type, and protocol number. Address family, socket type, and protocol number are as for the socket() function above. The default family is AF_UNIX if defined on the platform; otherwise, the default is AF_INET.

    socket.create_connection(address[, timeout[, source_address]])

    Connect to a TCP service listening on the Internet address (a 2-tuple (host, port)), and return the socket object. This is a higher-level function than socket.connect(): if host is a non-numeric hostname, it will try to resolve it for both AF_INET and AF_INET6, and then try to connect to all possible addresses in turn until a connection succeeds. This makes it easy to write clients that are compatible to both IPv4 and IPv6.

    Passing the optional timeout parameter will set the timeout on the socket instance before attempting to connect. If no timeout is supplied, the global default timeout setting returned by getdefaulttimeout() is used.

    If supplied, source_address must be a 2-tuple (host, port) for the socket to bind to as its source address before connecting. If host or port are ‘’ or 0 respectively the OS default behavior will be used.

    socket.getaddrinfo(hostportfamily=0type=0proto=0flags=0) #获取要连接的对端主机地址 必会

    sk.bind(address) 必会

      s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。

    sk.listen(backlog) 必会

      开始监听传入连接。backlog指定在拒绝连接之前,可以挂起的最大连接数量。

          backlog等于5,表示内核已经接到了连接请求,但服务器还没有调用accept进行处理的连接个数最大为5
          这个值不能无限大,因为要在内核中维护连接队列

    sk.setblocking(bool) 必会

      是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。

    sk.accept() 必会

      接受连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。

      接收TCP 客户的连接(阻塞式)等待连接的到来

    sk.connect(address) 必会

      连接到address处的套接字。一般,address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。

    sk.connect_ex(address)

      同上,只不过会有返回值,连接成功时返回 0 ,连接失败时候返回编码,例如:10061

    sk.close() 必会

      关闭套接字

    sk.recv(bufsize[,flag]) 必会

      接受套接字的数据。数据以字符串形式返回,bufsize指定最多可以接收的数量。flag提供有关消息的其他信息,通常可以忽略。

    sk.recvfrom(bufsize[.flag])

      与recv()类似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。

    sk.send(string[,flag]) 必会

      将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容全部发送。

    sk.sendall(string[,flag]) 必会

      将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。

          内部通过递归调用send,将所有内容发送出去。

    sk.sendto(string[,flag],address)

      将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。

    sk.settimeout(timeout) 必会

      设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如 client 连接最多等待5s )

    sk.getpeername()  必会

      返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。

    sk.getsockname() 

      返回套接字自己的地址。通常是一个元组(ipaddr,port)

    sk.fileno() 

      套接字的文件描述符

    socket.sendfile(fileoffset=0count=None)

         发送文件 ,但目前多数情况下并无什么卵用。

     

     一个简单的server/client程序

    server端

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    
    import socket
    
    ip_port = ('127.0.0.1',9999)
    
    sk = socket.socket()
    sk.bind(ip_port)
    sk.listen(5)
    
    while True:
        print 'server waiting...'
        conn,addr = sk.accept()
    
        client_data = conn.recv(1024)
        print client_data
        conn.sendall('不要回答,不要回答,不要回答')
    
        conn.close()
    

    client端

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    import socket
    ip_port = ('127.0.0.1',9999)
    
    sk = socket.socket()
    sk.connect(ip_port)
    
    sk.sendall('请求占领地球')
    
    server_reply = sk.recv(1024)
    print server_reply
    
    sk.close()
    

      

  • 相关阅读:
    iOS提醒用户进入设置界面进行重新授权通知定位等功能
    JQ获得ul li 值导成字符串
    “真的”
    原来还有插入代码功能 我还挨个缩进呢 好白菜啊
    ffmpeg 批处理
    DW english
    CI 分页“第一页”问题
    Discuz论坛发帖统计
    我的产品被评为一个“玩具”
    根据css文件采集图片
  • 原文地址:https://www.cnblogs.com/yunweiqiang/p/5854992.html
Copyright © 2011-2022 走看看