简体   繁体   中英

Python program not getting executed

I am getting below error while executing the python program. Can some help me in solving this?

[root@saas-promethus linuxtest]# /usr/bin/python2 script_azure.py
    Traceback (most recent call last):
      File "script_azure.py", line 43, in <module>
        with connect('HOST', 'user', 'password', 'db') as cur:
    AttributeError: __exit__

Python code :

import os, time, json
from itertools import groupby
from MySQLdb import connect
def refresh(cur):
    # Fetch all rows.
    cur.execute("SELECT azure_vm.PrivateIpAddress, azure_vm.VmName, azure_vm.Subnet, azure_vm.ResourceGroupName, azure_vm.VmSize, azure_vm.OsType, azure_vm.Region, azure_vm.subscriptionId, azure_vm.projectName, azure_vm.environmentName, azure_vm.departmentName, azure_vm.applicationName, azure_vm.costCenterName, azure_vm.costCenterCode, azure_app.app FROM azure_vm LEFT JOIN azure_app ON azure_vm.VmId = azure_app.VmId WHERE azure_vm.state !=  'VM Deleted' and azure_vm.Provisioned = 'api_azure' and azure_app.status != 'failed' and azure_app.status != 'removed'")
    tgs = []
    # Group all instances by their job and zone values.
    for key, vals in groupby(cur.fetchall(), key=lambda r: (r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14])):
        tgs.append({
            'labels': dict(zip(['VmName', 'Subnet','ResourceGroupName', 'VmSize', 'OsType', 'Region','SubscriptionId', 'ProjectName', 'Environment', 'DepartmentName', 'ApplicationName', 'CostCenterName', 'CostCeneterCode', 'App'], key)),
            'targets': [t[0] for t in vals],
        })
    # Persist the target groups to disk as JSON file.
    with open('/tgroups/target_groups_azure.json.new', 'w') as f:
        json.dump(tgs, f)
        f.flush()
        os.fsync(f.fileno())
    os.rename('/tgroups/target_groups_azure.json.new', '/tgroups/targets_azure.json')
    sql_select_Query = "SELECT azure_vm.VmId FROM azure_vm LEFT JOIN azure_app ON azure_vm.VmId = azure_app.VmId WHERE azure_vm.state !=  'VM Deleted' and azure_vm.Provisioned = 'api_azure' and azure_app.status != 'failed' or azure_app.status != 'removed'"
    cur.execute(sql_select_Query)
    records = cur.fetchall()
    out=[item for t in records for item in t]
    print(out)
    for i in out:
        manage = "Managed"
        mySql_delete_query = """UPDATE azure_app LEFT JOIN azure_vm ON azure_vm.VmId = azure_app.VmId set azure_app.Monitoring_status = %s WHERE azure_app.VmId = %s and azure_app.status != 'failed' and azure_vm.state != 'VM Deleted' and azure_app.status != 'removed'"""
        inputdata =(manage,i)
        cur.execute(mySql_delete_query,inputdata)
        cur.autocommit = True
if __name__ == '__main__':
    while True:
        with connect('HOST', 'username', 'password', 'database') as cur:
            refresh(cur)
        time.sleep(30)

In the absence of context manager support, change the last part of your code to this:

if __name__ == '__main__':
  while True:
    cur = None
    try:
      cur = connect('HOST', 'username', 'password', 'database')
      refresh(cur)
    finally:
      if cur:
        cur.close()
    time.sleep(30)

with connect('HOST', 'username', 'password', 'database') as cur:也许connect函数的返回对象不支持上下文管理语法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM