python學習----8.28---單例模式,網路編程

來源:互聯網
上載者:User

標籤:ssm   如何   turn   cdc   new   互動   style   nbsp   網路編程   

單例模式:多次執行個體化的結果指向同一個執行個體

  單例實現方式1

import settingsclass MySQL:    __instance=None    def __init__(self, ip, port):        self.ip = ip        self.port = port    @classmethod    def from_conf(cls):        if cls.__instance is None:            cls.__instance=cls(settings.IP, settings.PORT)        return cls.__instanceobj1=MySQL.from_conf()obj2=MySQL.from_conf()obj3=MySQL.from_conf()# obj4=MySQL(‘1.1.1.3‘,3302)print(obj1)print(obj2)print(obj3)# print(obj4)
View Code

    單例實現方式2(裝飾器)

import settingsdef singleton(cls):    _instance=cls(settings.IP,settings.PORT)    def wrapper(*args,**kwargs):        if len(args) !=0 or len(kwargs) !=0:            obj=cls(*args,**kwargs)            return obj        return _instance    return wrapper@singleton #MySQL=singleton(MySQL) #MySQL=wrapperclass MySQL:    def __init__(self, ip, port):        self.ip = ip        self.port = port# obj=MySQL(‘1.1.1.1‘,3306) #obj=wrapper(‘1.1.1.1‘,3306)# print(obj.__dict__)obj1=MySQL() #wrapper()obj2=MySQL() #wrapper()obj3=MySQL() #wrapper()obj4=MySQL(‘1.1.1.3‘,3302) #wrapper(‘1.1.1.3‘,3302)print(obj1)print(obj2)print(obj3)print(obj4)
View Code

    單例實現方式3(元類)

import settingsclass Mymeta(type):    def __init__(self,class_name,class_bases,class_dic):        #self=MySQL這個類        self.__instance=self(settings.IP,settings.PORT)    def __call__(self, *args, **kwargs):        # self=MySQL這個類        if len(args) != 0 or len(kwargs) != 0:            obj=self.__new__(self)            self.__init__(obj,*args, **kwargs)            return obj        else:            return self.__instanceclass MySQL(metaclass=Mymeta): #MySQL=Mymeta(...)    def __init__(self, ip, port):        self.ip = ip        self.port = portobj1=MySQL()obj2=MySQL()obj3=MySQL()obj4=MySQL(‘1.1.1.3‘,3302)print(obj1)print(obj2)print(obj3)print(obj4)
View Code

    單例實現方式4(模組匯入)

def f1():    from singleton import instance    print(instance)def f2():    from singleton import instance,My    SQL    print(instance)    obj=MySQL(‘1.1.1.3‘,3302)    print(obj)f1()f2()
View Code
import settingclass MySQL:    print((‘run.....‘))    def __init__(self,ip,port):        self.ip=ip        self.port=portinstance=MySQL(setting.IP,setting.PORT)
View Code

網路編程

1. 目標:編寫一個C/S架構的軟體
C/S: Client--------基於網路----------Server
B/S: Browser-------基於網路----------Server

2. 服務端需要遵循的原則:
1. 服務端與用戶端都需要有唯一的地址,但是服務端的地址必須固定/綁定
2. 對外一直提供服務,穩定運行
3. 服務端應該支援並發

3. 網路
網路建立的目的是為資料互動(通訊)
如何?通訊:
1. 建立好底層的物理串連介質
2. 有一套統一的通訊標準,稱之為互連網協議

4. 互連網協議:就是電腦界的英語
OSI七層協議
      應用程式層
      展示層
      會話層
      傳輸層
      網路層
      資料連結層
      物理層

ip+mac可以標識全世界範圍內獨一無二的一台電腦的位置
port可以標識一台電腦之上唯一的一個基於網路通訊的應用軟體

ip+mac+port:可以標識全世界範圍內獨一無二的一個應用軟體(基於網路通訊)

 

python學習----8.28---單例模式,網路編程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.