#-*- coding:UTF-8 -*-
import socket
import select
import datetime
import thread
import time
import urllib2

#=====================温习Socket========================== 
def timeServer():
    host = ''
    port = 9001
    _socket = None
    
    addr_list = socket.getaddrinfo(host, port ,socket.AF_UNSPEC ,socket.SOCK_STREAM ,0 ,socket.AI_PASSIVE)
    for addr in addr_list:
        family, socktype, proto, canonname, sockaddr = addr
        try:
            _socket = socket.socket(family, socktype, proto)
        except socket.error as msg:
            _socket = None
            continue
        
        try:
            _socket.bind(sockaddr)
            _socket.listen(10)
        except socket.error as msg:
            _socket.close()
            _socket = None
            continue
            
        break;
    if _socket == None:
        print 'socket inited fail ...'
        
    while True:
        cli , r_addr = _socket.accept()
        print 'client socket\'s addr is ',r_addr
        cli.sendall(str(datetime.datetime.now()))
 
 
def timeClient():
    host = 'fe80::e4a9:4817:e40b:e10f%11'
    port = 9001
    try:
        _socket = socket.socket(socket.AF_INET6 ,socket.SOCK_STREAM)
        c_state = _socket.connect_ex((host,port))
        if c_state <> 0:
             print 'client socket connect to time server faile.'
             return
        
        r_data = _socket.recv(56)
        print 'client receive msg is :',r_data
    except socket.error as msg:
        print 'client socket meet error :',msg
        return
    
def socket_Test():
    thread.start_new_thread(timeServer,())
    time.sleep(3)
    thread.start_new_thread(timeClient,())
    time.sleep(3)
    thread.start_new_thread(timeClient,())
    time.sleep(3)
    thread.start_new_thread(timeClient,())
    time.sleep(12)  
#=====================温习Urllib========================== 
def accessBaidu():
    p_handler = urllib2.ProxyHandler({'http':'10.99.60.201:8080'})
    
    password_msg = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_msg.add_password(None, 
                              uri = 'http://10.99.60.201:8080', 
                              user = 'houwei', 
                              passwd = 'messenger022783$$$$')
    auth_handler = urllib2.ProxyBasicAuthHandler(password_msg)
    
    m_opener = urllib2.build_opener(p_handler ,auth_handler)
    acc_state = m_opener.open('http://www.baidu.com')
#     print acc_state.geturl()
#     print acc_state.info()
#     print acc_state.getcode()
    print acc_state.read()

def urllib2_Test():
       accessBaidu()

#=====================温习单例========================== 
class singleInstance(object):  
    __myInstance = None
    
    def __new__(cls ,*args ,**cwd):
        global __myInstance
        
        if singleInstance.__myInstance == None:
            singleInstance.__myInstance = object.__new__(cls ,args ,cwd)
            print '__new__() <<< ', cls, ' <<< ' ,args,' <<< ' ,cwd ,' <<< ' ,singleInstance.__myInstance
        return singleInstance.__myInstance
        
    def __init__(self ,*args ,**cwd):
        print '__init__() <<< ',self
        object.__init__(self ,args ,cwd)
        
    @staticmethod
    def getClassInfo():
        return '<class \'__main__.singleInstance\'>'

def sigleInstance_Test():  
    s1 = singleInstance('one' ,'two' ,'three',one = 'lilong' , three = 'lushan')
    print '----------------------------------'
    s2 = singleInstance()
    
    print singleInstance.getClassInfo()
if __name__ == '__main__':
#     socket_Test()
#     urllib2_Test()
    sigleInstance_Test()
    
    
    