python 功能库

from flask import Flask , request

import json

import os

import requests

 

import pymysql

import pymssql

 

import redis

import subprocess, time, sys

 

class TUtils:

    def __init__(self):

        pass

    '''

    返回webapi的所有输入参数

    '''

    @staticmethod

def getRequestPs(req , defRes = {}):

    res=defRes

    try:

        ps={}        

        try:

            body = request.get_data()

            bodyPs = json.loads(body)

            ps=dict(ps , **bodyPs)

        except Exception as ee:

            pass        

        psArgs = dict(request.args)

        for k,v  in psArgs.items():

            ps[k] = v[0]

        psArgs = dict(request.form)

        for k,v  in psArgs.items():

            ps[k] = v[0]

        res = dict(res , **ps)

    except Exception as er:

        print("------------ getRequestParameters error ----------------")

        print(er)

        print("--------------------------------------------------------")

    return res

    '''

        读文件

    '''

    @staticmethod

    def readFile( fileName ):

        res =""

        try:

            if os.path.exists( fileName):

                with open( fileName , 'r') as f:

                    res = f.read()

        except Exception as er:

            print(er)

        return res

        

    '''

        写文件

    '''

    @staticmethod

    def writeFile( fileName , content ):

        res =""

        try:

            path , fn = os.path.split(fileName)

            if not os.path.exists(path):

                os.makedirs(path)

 

            if os.path.exists( path):

                with open( fileName , 'w') as f:

                    res = f.write(content)

        except Exception as er:

            print(er)

        return res

        

    '''

        Http代理请求

    '''

    @staticmethod

    def httpProxy( url , method = 'get' ):

        res =""

        try:

            if method=="post" :

                res = requests.post(url).text

            else:

                res = requests.get(url).text

        except Exception as er:

            print(er)

        return res

 

    '''

        MySQL 数据库连接池

    '''

    @staticmethod

    def getMysqlConnect( ahost , aport , auserName , apassword , adbName , asDict = False ):

        res =None

        try:

            conn = None

            if asDict :

                conn = pymysql.connect(host=ahost , port = aport , user = auserName , passwd = apassword, db = adbName , charset='utf8' , cursorclass=pymysql.cursors.DictCursor)

            else:

                conn = pymysql.connect(host=ahost , port = aport , user = auserName , passwd = apassword, db = adbName , charset='utf8' )

            res = conn

        except Exception as er:

            print(er)

        return res

 

    '''

        MySql 数据库查询

    '''

    @staticmethod

    def queryMySqlWithConnection( connection , sql , parameters ={} ):

        res =[]

        try:

            cursor = connection.cursor() 

            cursor.execute(sql, parameters)

            res = cursor.fetchall()

            cursor.close()

        except Exception as er:

            print(er)

        return res

 

    '''

        MySql 数据库查询

    '''

    @staticmethod

    def queryMySql( ahost , aport , auserName , apassword , adbName , sql , parameters ={} ):

        res =[]

        try:

            conn = TUtils.getMysqlConnect(ahost,aport,auserName,apassword,adbName, True)            

            res = TUtils.queryMySqlWithConnection(conn, sql, parameters)

            conn.close()

        except Exception as er:

            print(er)

        return res

 

    '''

        MySQL 数据库执行操作( insert , update , delete , exec proc)

    '''

    @staticmethod

    def executeMySqlWithConnection( connection , sql , parameters ={} ):

        res = 0

        try:

            cursor = connection.cursor() 

            cursor.execute(sql, parameters)

            connection.commit()

            cursor.close()

            res = 1

        except Exception as er:

            print(er)

        return res

 

    '''

        MySQL 数据库执行操作( insert , update , delete , exec proc)

    '''

    @staticmethod

    def executeMySql( ahost , aport , auserName , apassword , adbName , sql , parameters ={} ):

        res =[]

        try:

            conn = TUtils.getMysqlConnect(ahost,aport,auserName,apassword,adbName, False)            

            res = TUtils.executeMySqlWithConnection(conn, sql, parameters)

            conn.close()

        except Exception as er:

            print(er)

        return res

 

    '''

        ms Sql server 数据库连接池

    '''

    @staticmethod

    def getMssqlConnect( host , port , userName , password , dbName ):

        res =None

        try:

            res = pymssql.connect(server=host, port=port, user=userName, password=password, database=dbName, as_dict=True, charset="utf8")

        except Exception as er:

            print(er)

        return res

 

    '''

        ms Sql server 数据库查询

    '''

    @staticmethod

    def queryMsSqlWithConnection( connection , sql , parameters ={} ):

        res =[]

        try:

            cur =  connection.cursor()

            cur.execute(sql , parameters)

            res = cur.fetchall()

            cur.close()

        except Exception as er:

            print(er)

        return res

    

    @staticmethod

    def queryMsSql( host , port , userName , password , dbName  , sql , parameters ={} ):

        res =[]

        try:

            with pymssql.connect(server=host, port=port, user=userName, password=password, database=dbName, as_dict=True, charset="utf8") as conn:

                res = TUtils.queryMsSqlWithConnection(conn , sql , parameters)

                conn.close()

        except Exception as er:

            print(er)

        return res

 

    '''

        ms Sql server 数据库执行操作( insert , update , delete , exec proc)

    '''

    @staticmethod

    def executeMsSqlWithConnection( connection , sql , parameters ={} ):

        res = 0

        try:

            cur =  connection.cursor()

            cur.execute(sql , parameters)

            cur.close()

            res =1

        except Exception as er:

            print(er)

        return res

 

    '''

        ms Sql server 数据库执行操作( insert , update , delete , exec proc)

    '''

    @staticmethod

    def executeMsSql( host , port , userName , password , dbName  , sql , parameters ={} ):

        res =[]

        try:

            with pymssql.connect(server=host, port=port, user=userName, password=password, database=dbName, autocommit=True, charset="utf8") as conn:

                res = TUtils.executeMsSqlWithConnection(conn , sql , parameters)

                conn.close()

        except Exception as er:

            print(er)

        return res

 

    '''

        redis 连接池

    '''

    @staticmethod

    def getRedisConnect( host , port , password , dbIndex ):

        res =None

        try:

            res = redis.Redis(

                host= host, 

                port = port,

                db= dbIndex,

                password= password,

                decode_responses=True

            )

        except Exception as er:

            print(er)

        return res

 

    '''

        调用命令行

    '''

    @staticmethod

    def callCommandLine( commandLine ): 

        try:

            p = subprocess.Popen(commandLine,

                     shell=True,

                     bufsize=64,

                     stdin=subprocess.PIPE,

                     stderr=subprocess.PIPE,

                     stdout=subprocess.PIPE)

            for line in p.stderr:

                print("OUTPUT>>> " + str(line.rstrip()))                  

        except Exception as er:

            print(er) 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值