1. 程式人生 > >python-接口開發flask模塊(一)工具類準備

python-接口開發flask模塊(一)工具類準備

update date lose code def let mit pip clas

我們常常聽說測試http接口、測試java接口,測試socket接口等等;那麽python這麽強大的語言當然也可以用來開發接口了。

flask模塊介紹:

python中用來開發接口的模塊:flask,flask是一個第三大方的模塊需要pip install flask 就可以安裝使用

準備:

在tools中寫一些工具類比如操作mysql、redis、加密......

一、首先是操作mysql

import pymysql


class MyConnect(object):
    def __init__(self,host,port,user,passwd,db,charset=utf8
) self.host=host self.port=port self.user=user self.passwd=passwd self.db=db      self.get_cur() def get_cur(self): try: self.coon = pymysql.connect( host=self.__host, port=self.port, user=self.user, passwd=self.passwd, charset
=self.charset, db=self.db ) except Exception as e: print(這裏出錯了%s%e) else: self.cur = self.coon.cursor() def select_sql(self,sql): self.cur.excute(sql) return self.cur.fetchall() def other_sql(self,sql):
try: self.cur.excute(sql) except exception as e: print(sql執行錯了%s%e) else: self.coon.commit() def __del__(self): self.cur.close() self.coon.close()

二、操作redis

import redis


class OpRedis(object):
    def __init__(self,host,port,password)
        self.host = host    
        self.port = port
        self.password=password

     def get_r(self):
        try:
            self.r = redis.Redis(host=self.host,port=self.port,password=self.password)
        except Exception as e:
            print(“鏈接redis失敗%s”%e)
    
    def insert_redis(self,k,v)
        self.r.setex(k,v,EX_TIME)
    
    def selet_redis(self,k)
        return self.r.get(k).decode()

三、加密

import hashlib
def md5_passwd(s)
    s = str(s)+SALT
    m =hashlib.md5()
    m.update(s.encode())
    res = m.hexdigest()
    return res

python-接口開發flask模塊(一)工具類準備