Ejemplo n.º 1
0
    def test_051_postgres(self):
        """Is the server running?"""

        # test mode: either the server is running or it isn't.  both are ok as far as testing is concerned?

        # common error when no server is present:
        # note three addresses, all for "localhost"
        
        # could not connect to server: Connection refused
        #  Is the server running on host "localhost" (::1) and accepting
        #  TCP/IP connections on port 5432?
        # could not connect to server: Connection refused
        #  Is the server running on host "localhost" (127.0.0.1) and accepting
        #  TCP/IP connections on port 5432?
        # could not connect to server: Connection refused
        #  Is the server running on host "localhost" (fe80::1) and accepting
        #  TCP/IP connections on port 5432?
        

        try:
            cnx = psycopg2.connect(database='files',  user='******', host='localhost' )    
            GPR.print_attrs("connect", cnx, verbose_level_threshold=1)     # 4
        except psycopg2.OperationalError as err:
            errmsg = 'could not connect to server: Connection refused\n\tIs the server running on host "localhost" (::1) and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Connection refused\n\tIs the server running on host "localhost" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Connection refused\n\tIs the server running on host "localhost" (fe80::1) and accepting\n\tTCP/IP connections on port 5432?\n'
            self.assertEqual ( err.message , errmsg )
Ejemplo n.º 2
0
def db_connect(verbose_level_threshold=2):
    """open and return mysql connector object"""

    default_dict = {
        'user': '******',
        'password': '',
        'host': '127.0.0.1',
        'database': 'files'
    }

    try:
        config = ConfigParser.ConfigParser()
        config.readfp(open(CONFIG_FILE))
        config_dict = dict(config.items('postgres'))
        default_dict.update(config_dict)
    except ConfigParser.NoSectionError as err:
        print err

    GPR.print_dict("postgres", default_dict, verbose_level_threshold=3) 

    try:
        cnx = psycopg2.connect(**default_dict) # connection could have a cursor factory?   
        GPR.print_attrs("connect", cnx, verbose_level_threshold=3)   
    except psycopg2.OperationalError as err:
        if err.message.startswith( 'could not connect to server: Connection refused') :
            print "Is the postgres server running?" 
        else:
            print err.message
            
        return
    
    return cnx




    
    # config.add_section('postgres')
    # for k,v in config_dict.items(): # self.__dict__.items():
    #     config.set('postgres', k, v)
    # with open(CONFIG_FILE, 'wb') as configfile:
    #     config.write(configfile)

    # GPR.print_dict("db_connect(default_dict)", default_dict, verbose_level_threshold=1) 

    try:
        cnx = mysql.connector.connect(**default_dict)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Username or password %r and %r?" % (default_dict['user'], default_dict['password']))
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print "Database %r does not exist." % default_dict['database']
        else:
            print 'err:', err
            
    GPR.print_attrs("mysql.connector", cnx, verbose_level_threshold=4) 
    
    return cnx
Ejemplo n.º 3
0
def db_connect():
    """open and return mysql connector object"""
    default_dict = {
        'user': '******',
        'password': '',
        'host': '127.0.0.1',
        'database': 'files',
        'buffered': True
    }

    config = ConfigParser.ConfigParser()
    config.readfp(open(CONFIG_FILE))
    config_dict = dict(config.items('dbstuff'))
    
    default_dict.update(config_dict)
    
    GPR.print_dict("db_connect(default_dict+config_dict)", default_dict, verbose_level_threshold=1) 
    
    # config.add_section('dbstuff')
    # for k,v in config_dict.items(): # self.__dict__.items():
    #     config.set('dbstuff', k, v)
    # with open(CONFIG_FILE, 'wb') as configfile:
    #     config.write(configfile)

    # GPR.print_dict("db_connect(default_dict)", default_dict, verbose_level_threshold=1) 

    try:
        cnx = mysql.connector.connect(**default_dict)
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Username or password %r and %r?" % (default_dict['user'], default_dict['password']))
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print "Database %r does not exist." % default_dict['database']
        else:
            print 'err:', err
            
    GPR.print_attrs("mysql.connector", cnx, verbose_level_threshold=4) 
    
    return cnx
Ejemplo n.º 4
0
def db_connect_psycopg2():
    
    cnx = psycopg2.connect(database='files',  user='******', host='localhost' )    
    GPR.print_attrs("psycopg2.connect", cnx, verbose_level_threshold=1)     # 4
    return cnx