Esempio n. 1
0
File: ls.py Progetto: lkct/web-proj
def ls(form, params, cursor):
    """
    params:
        path: directory of file, start with /
    return:
        list: list of {filename, is_dir, size}
    """
    fpath = params['path']

    sql = 'SELECT * FROM file_list WHERE path="%s"' % (fpath)
    result = mysql(sql, cursor)
    ret = [{
        'filename': ln['filename'],
        'is_dir': ln['is_dir'],
        'size': ln['size']
    } for ln in result]

    fin_ret = []
    if fpath == '/':
        sql = 'SELECT * FROM belongs where user_name="%s"' % (params['user'])
        result = mysql(sql, cursor)
        grps = [ln['group_name'] for ln in result]
        grps = [params['user']] + grps
        for item in ret:
            if item['filename'] in grps:
                fin_ret.append(item)
    else:
        fin_ret = ret

    return {'errno': 0, 'list': json.dumps(fin_ret)}
Esempio n. 2
0
def newmbr(form, params, cursor):
    """
    params:
        group: group name
        user2: user to add
    return:
        errno
    """
    group = params['group']
    user2 = params['user2']

    sql = 'SELECT * FROM users WHERE user_name="%s"' % (user2)
    result = mysql(sql, cursor)
    if len(result) == 0:
        return {'errno': 6, 'errmsg': 'User not exist'}

    sql = 'SELECT * FROM belongs WHERE group_name="%s" AND user_name="%s"' % (
        group, user2)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 0}

    sql = 'INSERT INTO belongs (group_name, user_name) ' \
        'VALUES ("%s", "%s")' % (group, user2)
    mysql(sql, cursor)

    return {'errno': 0}
Esempio n. 3
0
def download(form, params, cursor):
    """
    params:
        filename: name of file
        path: directory of file, start with /
    return:
        dl_token: token to download link
    """
    fn = params['filename']
    fpath = params['path']

    sql = 'SELECT * FROM file_list WHERE path="%s" AND filename="%s"' % (fpath,
                                                                         fn)
    result = mysql(sql, cursor)
    md5 = result[0]['md5']
    sql = 'SELECT * FROM md5_list WHERE md5="%s"' % (md5)
    result = mysql(sql, cursor)
    srv_id = result[0]['srv_id']
    sql = 'SELECT * FROM file_server WHERE srv_id=%d' % (srv_id)
    result = mysql(sql, cursor)
    file_srv = result[0]['file_srv']

    dl_msg = {'filename': fn, 'md5': md5, 'file_srv': file_srv}
    dl_msg = encrypt(json.dumps(dl_msg))
    dl_token = generate({'dl_msg': dl_msg}, 3600)

    return {'errno': 0, 'dl_token': dl_token}
Esempio n. 4
0
def delete(srv_id):
    db = MySQLdb.connect('localhost', 'web', 'web', 'web', charset='utf8')
    cursor = db.cursor()

    sql = 'SELECT * FROM md5_list WHERE ref_cnt=0 AND srv_id=%d' % (srv_id)
    result = mysql(sql, cursor)
    md5list = [ln['md5'] for ln in result]
    size = sum([ln['size'] for ln in result])

    sql = 'SELECT * FROM file_server WHERE srv_id=%d' % (srv_id)
    result = mysql(sql, cursor)
    file_srv = result[0]['file_srv']

    payload = {'md5list': json.dumps(md5list)}
    url = 'http://' + file_srv + '/cgi-bin/delete.py'
    r = requests.post(url, data=payload)
    r.raise_for_status()

    sql = [
        'DELETE FROM md5_list WHERE ref_cnt=0 AND srv_id=%d' % (srv_id),
        'UPDATE file_server SET avail_space=avail_space+%d WHERE srv_id="%s"' %
        (size, srv_id)
    ]
    mysql(sql, cursor)

    db.commit()
    db.close()

    return
Esempio n. 5
0
def diff(form, params, cursor):
    """
    params:
        filename: name of file
        path: directory of file, start with /
        md5: md5 to check existence
    return:
        exist: 1 for md5 exist, 0 for not
        errno
    """
    fn = params['filename']
    fpath = params['path']
    md5 = params['md5']

    if fpath == '/':
        return {'errno': 3, 'errmsg': 'Access not authorized'}

    sql = 'SELECT * FROM file_list WHERE path="%s" AND filename="%s"' % (
        fpath, fn)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 7, 'errmsg': 'File of same name already existed at destination'}
    else:
        sql = 'SELECT * FROM md5_list WHERE md5="%s"' % (md5)
        result = mysql(sql, cursor)
        return {'errno': 0, 'exist': len(result)}
Esempio n. 6
0
def signin_ok(request):	
	id=int(request.GET['id'])
	passwd=request.GET['passwd']
	
	import mysql
	res=mysql.mysql('signin1',id)
	if len(res)==1: # id already exists
		return render(request,'signin_fail.html',{'id':id})
	elif len(res)==0:
		mysql.mysql('signin1',id,passwd,'test')
		return render(request,'signin_ok.html',{'id':id,'passwd':passwd})
Esempio n. 7
0
def main():
    from mysql import mysql

    filenames=show_file()
    for filename in filenames:
        result=load_json(filename=filename)
        for j in result:
            info=parse_json(j=j)
            info['refer']=filename.split('-')[0].split('/')[-1]

            print json.dumps(info,ensure_ascii=False)
            mysql().insert_category(info)
Esempio n. 8
0
def reg(form, params, cursor):
    """
    auth:
        user: user name
        passwd: password encrytped and encoded
    return:
        errno
    """
    form['auth'].file.seek(0)
    auth = dict(urlparse.parse_qsl(form['auth'].file.read()))
    user = auth['user']
    passwd = auth['passwd']

    if string.lower(user) == 'root':
        return {'errno': 4, 'errmsg': 'Register name ROOT not allowed'}
    if len(user) >= 32:
        return {'errno': 4, 'errmsg': 'User name too long'}
    allow_char = string.letters + string.digits + ' _'
    for ch in user:
        if ch not in allow_char:
            return {
                'errno': 4,
                'errmsg': 'User name include illegal character'
            }

    sql = 'SELECT * FROM users WHERE user_name="%s"' % (user)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 8, 'errmsg': 'Duplicate name used'}

    sql = 'SELECT * FROM belongs WHERE group_name="%s"' % (user)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 8, 'errmsg': 'Duplicate name used'}

    passwd = decrypt(passwd)
    md5 = hashlib.md5()
    md5.update(passwd)
    passwd = md5.hexdigest()
    sql = [
        'INSERT INTO users (user_name, passwd) VALUES ("%s", "%s")' %
        (user, passwd),
        'INSERT INTO file_list (path, filename, is_dir) '
        'VALUES ("%s", "%s", %d)' % ('/', user, 1)
    ]
    mysql(sql, cursor)

    return {'errno': 0}
Esempio n. 9
0
def login(form, params, cursor):
    """
    auth:
        user: user name
        passwd: password encrytped and encoded
    return:
        token: token to work with
        errno
    """
    form['auth'].file.seek(0)
    auth = dict(urlparse.parse_qsl(form['auth'].file.read()))
    user = auth['user']
    passwd = auth['passwd']

    passwd = decrypt(passwd)
    md5 = hashlib.md5()
    md5.update(passwd)
    passwd = md5.hexdigest()
    sql = 'SELECT * FROM users WHERE user_name="%s" AND passwd="%s"' % (user,
                                                                        passwd)
    result = mysql(sql, cursor)
    if len(result) == 0:
        return {'errno': 2, 'errmsg': 'Invalid user name or password'}
    else:
        token = generate({'user': user}, 300)
        return {'errno': 0, 'token': token}
Esempio n. 10
0
 def __init__(self, configDb, configWarning):
     self.log = mylog.mylog('warning.log')
     self.dbWarningConfig = "adx_warning_config"
     self.dbWarning = "adx_warning"
     self.dbData = db.mysql(configDb)
     self.smsApi = configWarning.SMS_API
     self.smtp = configWarning.MAIL_SMTP
Esempio n. 11
0
def getServerWip(interIp):
    get_outip_from_zichan = arg.gameOption("get_outip_from_zichan",type="bool",default=False)
    iplist = []
    if not get_outip_from_zichan:
        sshobj = ssh.ssh(interIp.strip())
        cmd = "/sbin/ifconfig -a | grep 'inet addr'|cut -d':' -f2|cut -d' ' -f1"
        status,r,stderr = sshobj.cmd(cmd)
        if status == 0:
            for ip in r.split("\n"):
                if re.search(r"^(10\.|192\.168|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[01]\.|169\.254\.|127\.)",ip) or ip.strip() == "":
                    continue
                else:
                    iplist.append(ip.strip())
            if len(iplist) == 0:
                #status,r1,stderr = sshobj.cmd("curl -s ip.cn | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'")
                status,r1,stderr = sshobj.cmd("curl -s ipip.net | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'")
                if check.checkIp(r1) :
                    iplist.append(r1.strip())
        else:
            print "[%s] out:%serr:%s"%(cmd,out,err)
    else:
        hsot = arg.mainOption("zichan_host")
        user = arg.mainOption("zichan_user")
        pwd = arg.mainOption("zichan_pwd")
        port = arg.mainOption("zichan_port",type="int")
        db = arg.mainOption("zichan_db")
        my = mysql.mysql(hsot,user,pwd,db,port)
        mylist = my.query("select asset_outip.ip,isp from asset_inip join asset_outip on asset_outip.did = asset_inip.did and asset_inip.ip = '%s'"%interIp)
        for i in mylist:
            if i[1].strip() == "电信":
                iplist.insert(0,i[0].strip())
            else:
                iplist.append(i[0].strip())
        pass
    return iplist
Esempio n. 12
0
def main():
    config = get_config("setting")
    sql = mysql.mysql(config["mysql"]["hostname"], config["mysql"]["username"],
                      config["mysql"]["password"], config["mysql"]["database"])
    myConnection = sql.connection()
    m_new(myConnection, sql)
    m_do(myConnection, sql)
    sql.close(myConnection)
Esempio n. 13
0
def pMysqlDb():
    data = getDbConfInfo()
    conn = mysql.mysql()
    conn.setHost(data['DB_HOST'])
    conn.setUser(data['DB_USER'])
    conn.setPwd(data['DB_PASS'])
    conn.setPort(int(data['DB_PORT']))
    conn.setDb(data['DB_NAME'])
    return conn
Esempio n. 14
0
def delmbr(form, params, cursor):
    """
    params:
        group: group name
        user2: user to del
    return:
        NONE
    """
    group = params['group']
    user2 = params['user2']
    user = params['user']

    if user == user2:
        return {'errno': 3, 'errmsg': 'Can\'t delete self'}

    sql = 'DELETE FROM belongs WHERE group_name="%s" AND user_name="%s"' % (
        group, user2)
    mysql(sql, cursor)

    return {'errno': 0}
Esempio n. 15
0
 def __init__(self):
     self.dbconfig = {
         'host': '127.0.0.1',
         'port': 3306,
         'user': '******',
         'passwd': '',
         'db': 'spider',
         'charset': 'utf8'
     }
     # self.code=0
     self.cur=mysql.mysql(conn_db=self.dbconfig)
Esempio n. 16
0
 def __init__(self):
     self.dbconfig = {
         'host': '127.0.0.1',
         'port': 3306,
         'user': '******',
         'passwd': '',
         'db': 'spider',
         'charset': 'utf8'
     }
     # self.code=0
     self.cur = mysql.mysql(conn_db=self.dbconfig)
Esempio n. 17
0
def pMysqlDb():
    conf = getDbConfValue()

    host = conf['HOST'].split(':')
    conn = mysql.mysql()

    conn.setHost(host[0])
    conn.setUser(conf['USER'])
    conn.setPwd(conf['PASSWD'])
    conn.setPort(int(host[1]))
    conn.setDb(conf['NAME'])
    return conn
Esempio n. 18
0
def cp(form, params, cursor):
    """
    params:
        filename: name of file
        path: directory of file, start with /
        filename2: name of file
        path2: directory of file, start with /
        mv: 0 for cp, 1 for mv
    return:
        errno
    """
    fn = params['filename']
    fpath = params['path']
    fn2 = params['filename2']
    fpath2 = params['path2']
    mv = int(params['mv'])

    if fpath2 == '/':
        return {'errno': 3, 'errmsg': 'Access not authorized'}
    tmpath = fpath + '/' + fn
    if fpath2[:len(tmpath)] == tmpath:
        return {'errno': 9, 'errmsg': 'Paste into source'}

    sql = 'SELECT * FROM file_list WHERE path="%s" AND filename="%s"' % (
        fpath2, fn2)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {
            'errno': 7,
            'errmsg': 'File of same name already existed at destination'
        }
    else:
        if mv == 1:
            sql = 'UPDATE file_list SET path="%s", filename="%s" WHERE path="%s" AND filename="%s"' % (
                fpath2, fn2, fpath, fn)
            mysql(sql, cursor)
        else:
            cp_r(cursor, fpath, fn, fpath2, fn2)
        return {'errno': 0}
Esempio n. 19
0
  def __init__(self):
    threading.Thread.__init__(self)
    self.db =  mysql.mysql()
    self.db.connect()
    
    dealInfo = self.db.getDealMonitInfo()
    self.dealInfo = dict(dealInfo)
    self.stockList = list(zip(*dealInfo)[0])
    self.moniterRec = dict(zip(self.stockList,[0.1] * len(self.stockList)))

    self.db.discon()

    self.initAvg()
Esempio n. 20
0
def newgrp(form, params, cursor):
    """
    params:
        group: group name
    return:
        errno
    """
    user = params['user']
    group = params['group']

    if string.lower(group) == 'root':
        return {'errno': 4, 'errmsg': 'Register name ROOT not allowed'}
    if len(group) >= 32:
        return {'errno': 4, 'errmsg': 'Group name too long'}
    allow_char = string.letters + string.digits + ' _'
    for ch in group:
        if ch not in allow_char:
            return {'errno': 4, 'errmsg': 'Group name include illegal character'}

    sql = 'SELECT * FROM belongs WHERE group_name="%s"' % (group)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 8, 'errmsg': 'Duplicate name used'}

    sql = 'SELECT * FROM users WHERE user_name="%s"' % (group)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 8, 'errmsg': 'Duplicate name used'}

    sql = [
        'INSERT INTO belongs (group_name, user_name, is_own) '
        'VALUES ("%s", "%s", %d)' % (group, user, 1),

        'INSERT INTO file_list (path, filename, is_dir) '
        'VALUES ("%s", "%s", %d)' % ('/', group, 1)
    ]
    mysql(sql, cursor)

    return {'errno': 0}
Esempio n. 21
0
def lsmbr(form, params, cursor):
    """
    params:
        group: group name
    return:
        list: list of {user, is_own}
    """
    group = params['group']

    sql = 'SELECT * FROM belongs WHERE group_name="%s"' % (group)
    result = mysql(sql, cursor)
    ret = [{'user': ln['user_name'], 'is_own': ln['is_own']} for ln in result]

    return {'errno': 0, 'list': json.dumps(ret)}
Esempio n. 22
0
def update_k(list_view, sex):
    wr_mysql = mysql.mysql()
    numb = len(list_view)
    name_str = "(number,sex"
    values_str = "(" + str(numb) + ",'" + str(sex) + "'"
    for i in range(numb):
        name_str += ",r" + str(i + 1)
        values_str += "," + str(list(list_view.keys())[i])
        if i == numb - 1:
            name_str += ")"
            values_str += ")"
    insert_db = "insert into records " + name_str + " values " + values_str
    wr_mysql.db_insert(insert_db)
    wr_mysql.db_close()
    '''
Esempio n. 23
0
def mkdir(form, params, cursor):
    """
    params:
        filename: name of file
        path: directory of file, start with /
    return:
        errno
    """
    fn = params['filename']
    fpath = params['path']

    if fpath == '/':
        return {'errno': 3, 'errmsg': 'Access not authorized'}

    sql = 'SELECT * FROM file_list WHERE path="%s" AND filename="%s"' % (
        fpath, fn)
    result = mysql(sql, cursor)
    if len(result) > 0:
        return {'errno': 7, 'errmsg': 'File of same name already existed at destination'}
    else:
        sql = 'INSERT INTO file_list (path, filename, is_dir) ' \
            'VALUES ("%s", "%s", %d)' % (fpath, fn, 1)
        mysql(sql, cursor)
        return {'errno': 0}
Esempio n. 24
0
  def __init__(self, stock = None):
    threading.Thread.__init__(self)
    self.db =  mysql.mysql()
    self.db.connect()
    
    if stock == None:
      self.stock = 'sz000001'
      self.stockList = ['000001']
    else:
      self.stock = stock
      self.stockList = map(lambda s:s[2:], self.stock.split(","))
      self.stockOpp = dict(zip(self.stockList, [0] * len(self.stockList)))

    #初始化均线信息
    self.initAvg()
    
    self.db.discon()
Esempio n. 25
0
def lsgrps(form, params, cursor):
    """
    params:
        NONE
    return:
        list: list of {group, is_own}
    """
    user = params['user']

    sql = 'SELECT * FROM belongs WHERE user_name="%s"' % (user)
    result = mysql(sql, cursor)
    ret = [{
        'group': ln['group_name'],
        'is_own': ln['is_own']
    } for ln in result]

    return {'errno': 0, 'list': json.dumps(ret)}
Esempio n. 26
0
def getServerWip(interIp):
    get_outip_from_zichan = arg.gameOption("get_outip_from_zichan",
                                           type="bool",
                                           default=False)
    iplist = []
    if not get_outip_from_zichan:
        sshobj = ssh.ssh(interIp.strip())
        cmd = "/sbin/ifconfig -a | grep 'inet addr'|cut -d':' -f2|cut -d' ' -f1"
        status, r, stderr = sshobj.cmd(cmd)
        if status == 0:
            for ip in r.split("\n"):
                if re.search(
                        r"^(10\.|192\.168|172\.1[6-9]\.|172\.2[0-9]\.|172\.3[01]\.|169\.254\.|127\.)",
                        ip) or ip.strip() == "":
                    continue
                else:
                    iplist.append(ip.strip())
            if len(iplist) == 0:
                #status,r1,stderr = sshobj.cmd("curl -s ip.cn | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'")
                status, r1, stderr = sshobj.cmd(
                    "curl -s ipip.net | awk '{print $2}' | sed 's/[^0-9\\.]*\\(.*\\)/\\1/g'"
                )
                if check.checkIp(r1):
                    iplist.append(r1.strip())
        else:
            print "[%s] out:%serr:%s" % (cmd, out, err)
    else:
        hsot = arg.mainOption("zichan_host")
        user = arg.mainOption("zichan_user")
        pwd = arg.mainOption("zichan_pwd")
        port = arg.mainOption("zichan_port", type="int")
        db = arg.mainOption("zichan_db")
        my = mysql.mysql(hsot, user, pwd, db, port)
        mylist = my.query(
            "select asset_outip.ip,isp from asset_inip join asset_outip on asset_outip.did = asset_inip.did and asset_inip.ip = '%s'"
            % interIp)
        for i in mylist:
            if i[1].strip() == "电信":
                iplist.insert(0, i[0].strip())
            else:
                iplist.append(i[0].strip())
        pass
    return iplist
Esempio n. 27
0
def dlTest(driver, urls):
    print "----------------------------"
    print "|  This is download test.  |"
    print "----------------------------"

    cnt = 5
    db = mysql.mysql()
    for url in urls:
        driver.get(url)
        print "  access to", url,
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "file")))
        print "-> suceess."

        ### Error Text check###
        if '<li class="text-error">' in driver.page_source:
            print "  This malware has error text."
            errorText = driver.find_element_by_class_name('text-error')
            print errorText.text

        ### make dom data ###
        print "    Get dom",
        dom1 = lxml.html.fromstring(driver.page_source)
        print "-> executed."

        ### make dom data ###
        print "    Get SHA-1",
        sha1 = getSha1.sha1txt(dom1)
        if not db.mysql_insert(sha1):
            time.sleep(20)
            if not virustotal_search.virustotal_api(db, sha1):
                pass
        #print "-> executed. :", sha1
            else:
                downloadClick(driver, dom1)

        ### Get anchors to Donwload Bottun
        downloadClick(driver, dom1)
        cnt -= 1
        if cnt == 0:
            break
Esempio n. 28
0
 def __init__(self, **kw):
     """
     set of tools which can be used by all classes
     """
     self.options = kw['options']
     self.hostname = kw['hostname']
     self.binfilepath = self.options.binfilepath
     self.binfile = self.options.binfile
     self.silent = False
     if kw.has_key('silent'): self.silent = kw['silent']
     self.inittime = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
     self.timedict = {}
     self.devinfo = {}
     self.path = expanduser("~") + '/ftosupgrade'
     self.devpath = self.path + '/' + self.hostname + '/'
     self.devinfofile = self.devpath + 'devinfo.json'
     self.errors = {self.hostname: {'warning': list(), 'critical': list()}}
     self.log = self.devpath + 'info.log'
     self.peclog = self.devpath + 'commands.log'
     self.errorlog = self.devpath + 'errors.log'
     self.logger = logging.getLogger('messages')
     self.dbh = mysql()
Esempio n. 29
0
def main(view_list, sex):
    cf_mysql = mysql.mysql()
    select_r = "select * from records where sex = " + "'" + sex + "'" + ";"
    train_data = cf_mysql.db_select(select_r)
    trainpath = "./data/itemcftrain.txt"
    list_view = view_list
    #testpath="./data/itemcftest.txt"
    "creat the project and initialization"
    Item = ItemBasedCF(train_data, list_view)
    "read the history data"
    Item.readData()
    # print Item.hash
    "compute the similarity"
    Item.ItemSimilarity()
    # number=0
    # numerator=0
    # denominator=0
    "get the recommend list of the list_view"
    recommedDic = Item.Recommend(5)
    print("the answer list based on Method 3")
    print(recommedDic)
    return recommedDic
Esempio n. 30
0
def create_data_type(data_type_name, base_dir=None):
    try:
        client = mysql()
        client.connect()
        print('create data_type "%s"...' % data_type_name)
        path = ''
        if base_dir:
            path = base_dir + '/'

        path += data_type_name
        try:
            ret = client.cmd(
                'insert into `data_type` (`type_name`,`type_dir`) values ("%s","%s/")'
                % (data_type_name, path))
        except:
            print("Insert type failed")

        if not os.path.exists("static/%s" % path):
            print('create dir "static/%s"' % path)
            os.makedirs('static/%s' % path)
    except DB_Exception as e:
        print(e.arg(1))
Esempio n. 31
0
 def __init__(self):
     self.msdb = mssql()
     self.mysqldb = mysql()
Esempio n. 32
0
    'h': 0,
    'cx': 0,
    'cy': 0,
    'beingTracked': False,
    'newOrMatch': False,
    'framesWithoutMatch': 0,
    'prevCenterX': 0,
    'prevCenterY': 0,
    'hasCrossed': False
}]

frameIndex = 0
leftCrossedCars = 0
rightCrossedCars = 0

db = mysql()

while True:
    try:
        pngs = glob.glob(r'web/webcam/images/*.png')
        # if os._exists(pngs[0]) == False:
        if cv2.waitKey(33) == 27:
            break

        if (len(pngs) < 1):
            continue

        img = cv2.imread(pngs[0])

        crop_img = img  # Crop from x, y, w, h -> 100, 200, 300, 400
Esempio n. 33
0
def env_init():
    try:
        with open("mysql_auth.txt", "r") as fp:
            host = fp.readline().rstrip()
            user = fp.readline().rstrip()
            passwd = fp.readline().rstrip()
            dbname = fp.readline().rstrip()
    except:
        print("Open authorization file failed")
        exit(1)

    print("Connect to MySQL server...")
    try:
        client = pymysql.connect(host,
                                 user,
                                 passwd,
                                 use_unicode=True,
                                 charset="utf8")
        cursor = client.cursor()
    except:
        print("Connect failed")
        exit(1)

    print("Dropping database \"%s\" if existed..." % dbname)
    cursor.execute("DROP DATABASE IF EXISTS " + dbname)
    print("Creating database \"%s\"..." % dbname)
    cursor.execute("CREATE DATABASE %s character set utf8" % dbname)
    try:
        with mysql() as db:
            db.connect()
            with open("create_tables.sql", "r") as file:
                sql_Commands = file.read()
                sql_Commands = sql_Commands.replace('\n', '').split(';')
            for sql in sql_Commands[:-1]:
                db.cmd(sql)
    except:
        print("Open sql file failed")
        exit(1)
    client.close()

    try:
        client = pymysql.connect(host,
                                 user,
                                 passwd,
                                 dbname,
                                 use_unicode=True,
                                 charset='utf8')
        cursor = client.cursor()
    except:
        print("Connect failed")
        exit(1)

    print("create user \"admin\"...")
    user_name = 'admin'
    user_hashed_passwd = bcrypt.hashpw('admin'.encode('utf-8'),
                                       bcrypt.gensalt())
    try:
        ret = cursor.execute(
            "insert into `user` (`user_name`,`user_password`,`user_level`) values (%s,%s,10000)",
            (user_name, user_hashed_passwd))
    except:
        print("Insert user failed")

    create_data_type("圖片")

    create_data_type("獲獎公告")

    create_data_type("活動公告")

    create_data_type("氣像雲圖", "圖片")

    create_data_type("google_drive_image", "圖片")

    create_data_type("google日曆")

    create_data_type("customized_text")

    print("create arrage_mode 0...")
    try:
        ret = cursor.execute(
            'insert into `arrange_mode` (`armd_mode`) values (0)')
    except:
        print("Insert arrange mode failed")

    client.commit()
    client.close()
Esempio n. 34
0
#   2019/4/30 0030 下午 2:03     

__author__ = 'RollingBear'

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler, ThrottledDTPHandler
from pyftpdlib.servers import FTPServer

import mysql
import config

import logging

conf = config.config('\\config\\config.ini')
sql = mysql.mysql()


def get_user():
    # 定义一个用户列表

    user_list = sql.format_query_dict(_list_name='*', _table_name='user', _condition=None)
    return user_list


def ftp_server():
    # 实例化虚拟用户,这是FTP验证首要条件
    authorizer = DummyAuthorizer()

    # 添加用户权限和路径,括号内的参数是(用户名, 密码, 用户目录, 权限)
    # authorizer.add_user('user', '12345', '/home/', perm='elradfmw')
Esempio n. 35
0
def index(req, data=""):

    _SUCCESS = "0"
    _DATAFORMAT = 1
    _VERSION = 2
    _MISSING = 3
    _DATABASE = 4
    _FLOOD = 5

    # Decode submitted data
    try:
        data = json_decode(data)
    except:
        return _DATAFORMAT
    
    # Check feedback version
    if "version" not in data:
        return _VERSION

    # DB connection
    try:
        sql = mysql(site_config['db_host'], \
                    site_config['db_name'], \
                    site_config['db_user'], \
                    site_config['db_pass'])
    except:
        return _DATABASE

    # Check required fields
    s1 = set(["experience", "question"])
    s2 = set(data.keys())

    if len(s1 - s2) != 0:
        return _MISSING

    # An IP may submit only one feedback in 10 minutes.
    submissions = sql.scalar_query("SELECT Count(*) FROM feedback WHERE ip=%s AND now()-submitdate < 600", req.get_remote_host())
    if submissions > 0:
        return _FLOOD

    if "hw" in data:
        s1 = set(["memtotal", "swaptotal", "cpu_model", "cpu_speed", "kernel"])
        s2 = set(data["hw"].keys())

        if len(s1 - s2) != 0:
            return _MISSING

    values = {
              "ip": req.get_remote_host(),
              "submitdate": time.strftime('%Y-%m-%d %H:%M'),
              "experience": data.get("experience", "0"),
              "purpose": data.get("purpose", "0"),
              "use_where": data.get("use_where", "0"),
              "question": data.get("question", ""),
              "opinion": data.get("opinion", ""),
              "email": data.get("email", ""),
              "email_announce": data.get("email_announce", "F")
              }
    fb = sql.insert("feedback", values)

    if "hw" in data:
        values = {
                  "feedback": fb,
                  "memtotal": data["hw"]["memtotal"],
                  "swaptotal": data["hw"]["swaptotal"],
                  "cpu_model": data["hw"]["cpu_model"],
                  "cpu_speed": data["hw"]["cpu_speed"],
                  "kernel": data["hw"]["kernel"]
                  }
        hw = sql.insert("hardware", values)

    return _SUCCESS
Esempio n. 36
0
def pMysqlDb():
    db = mysql.mysql()
    db.__DB_CNF = getConf()
    db.setPwd(pSqliteDb('config').where(
        'id=?', (1,)).getField('mysql_root'))
    return db
Esempio n. 37
0
    urldoc = urllib.urlopen(url).read()
    urldoc = urldoc.split(';')
    urldoc.pop()
    urldocList = map(dealSinaData, urldoc)
    stockPriceInfo = dict(zip(self.stockList,urldocList))
    
    #对比均线信息,判断机会
    for (sid,cur) in stockPriceInfo.items():
      if sid in self.stockAvg.keys():
        self.checkOpp(cur, self.stockAvg[sid][1], self.stockAvg[sid][2], sid)
    self.db.discon()

if __name__ == '__main__':

  #GET ALL THE MONIT STOCK INFO
  db = mysql.mysql()
  db.connect()
  stockList = db.getMoniterStockInfo()
  db.discon()
  
  stockIdList = list(zip(*stockList)[0])
  
  #THE COUNT OF STOCKS IN EVERY THREAD
  everyStocks = int(math.ceil(float(len(stockList))/float(MONITER_TCOUNT)))

  stocksT = []
  for i in range(MONITER_TCOUNT):
      stocksT.append(stockIdList[(i+1-1)*everyStocks:(i+1)*everyStocks])

  stocksT = map(comJoin, stocksT)
Esempio n. 38
0
        else:
            usage()

    if not args:
        usage()

    myfs = args[0]
    limits = args[1:]

    if opt_tofile:
        if opt_tofile == '-':
            fh = sys.stdout
        else:
            fh = file(opt_tofile, "w")
    else:
        fh = mysql.mysql(**myconf)

    callback = None
    if opt_verbose:
        print "destination: " + fh.name
        callback = mysql.cb_print()

    if opt_verbose:
        pass

    mysql.fs2mysql(fh, myfs, limits, callback, 
                   opt_skip_extended_insert,
                   opt_add_drop_database)

if __name__ == "__main__":
    main()
Esempio n. 39
0
def panel2To3():
    try:
        filename = '/www/server/apache/version.pl'
        if os.path.exists(filename):
            version = public.readFile(filename);
            if version.find('2.2') != -1:
                print 'BT-Panel 3.x does not support apache2.2';
                return;
    except:
        pass
    
    
    import mysql,db,os,shutil,time
    os.system('service mysqld stop')
    os.system('pkill -9 mysqld_safe')
    os.system('pkill -9 mysqld')
    os.system('sleep 2')
    os.system('service mysqld start');
    os.system('service mysqld stop')
    os.system('mysqld_safe --skip-grant-tables&')
    
    
    time.sleep(3);
    sql = mysql.mysql();
    sqldb = db.Sql();
    
    #转移配置
    arr = sql.query('select * from bt_default.bt_config')[0]
    sqldb.table('config').where('id=?',(1,)).save('webserver,backup_path,sites_path,mysql_root',(arr[1],arr[2],arr[3],arr[5]));
    webserver = arr[1];
    setupPath = '/www/server';
    
    if webserver == 'nginx':
        #处理Nginx配置文件
        filename = setupPath + "/nginx/conf/nginx.conf"
        if os.path.exists(filename):
            conf = public.readFile(filename);
            if conf.find('include vhost/*.conf;') != -1:
                conf = conf.replace('include vhost/*.conf;','include ' + setupPath + '/panel/vhost/nginx/*.conf;');
                public.writeFile(filename,conf);
        #处理伪静态文件
        dstPath = setupPath + '/panel/vhost/rewrite'
        srcPath = setupPath + '/nginx/conf/rewrite'
        if os.path.exists(srcPath):
            if os.path.exists(dstPath): os.system('mv -f ' + dstPath + ' ' + dstPath + '_backup_' + time.strftime('%Y%m%d_%H%M%S',time.localtime()));
            os.system('cp -a -r ' + srcPath + ' ' + dstPath);
            os.system('chmod -R 644 ' + dstPath);
    else:
        #处理Apache配置文件
        filename = setupPath + "/apache/conf/httpd.conf"
        if os.path.exists(filename):
            conf = public.readFile(filename);
            if conf.find('IncludeOptional conf/vhost/*.conf') != -1:
                conf = conf.replace('IncludeOptional conf/vhost/*.conf','IncludeOptional ' + setupPath + '/panel/vhost/apache/*.conf');
                public.writeFile(filename,conf);
               
    
    #转移站点
    arr = sql.query('select * from bt_default.bt_sites')
    for siteArr in arr:
        #站点
        pid = sqldb.table('sites').add('name,path,status,ps,addtime',(siteArr[1],siteArr[3],siteArr[4],siteArr[6],str(siteArr[7])))
        
        #域名
        domains = siteArr[2].split(',');
        for domain in domains:
            tmp = domain.split(':');
            if len(tmp) < 2: tmp.append('80');
            sqldb.table('domain').add('pid,name,port,addtime',(pid,tmp[0],tmp[1],public.getDate()))
        
        #子目录
        barr = sql.query("select * from bt_default.bt_binding where pid='"+str(siteArr[0])+"'")
        for binding in barr:
            sqldb.table('binding').add('pid,domain,path,port,addtime',(pid,binding[2],binding[3],str(binding[4]),str(binding[5])))
        
        #迁移配置文件
        letPath = '/etc/letsencrypt/live';
        dstKey = letPath + '/' + siteArr[1] + '/privkey.pem';
        dstCsr = letPath + '/' + siteArr[1] + '/fullchain.pem';
        if webserver == 'nginx':
            confFile = setupPath + '/nginx/conf/vhost/' + siteArr[1] + '.conf';
            if os.path.exists(confFile):
                conf = public.readFile(confFile);
                conf = conf.replace('rewrite/',setupPath+'/panel/vhost/rewrite/').replace('key/' + siteArr[1] + '/key.key',dstKey).replace('key/' + siteArr[1] + '/key.csr',dstCsr);
                filename = setupPath + '/panel/vhost/nginx/' + siteArr[1] + '.conf';
                public.writeFile(filename,conf);
                os.system('chmod 644 ' + filename);
        else:
            confFile = setupPath + '/apache/conf/vhost/' + siteArr[1] + '.conf';
            if os.path.exists(confFile):
                conf = public.readFile(confFile);
                conf = conf.replace('conf/key/' + siteArr[1] + '/key.key',dstKey).replace('conf/key/' + siteArr[1] + '/key.csr',dstCsr);
                filename = setupPath + '/panel/vhost/apache/' + siteArr[1] + '.conf';
                public.writeFile(filename,conf);
                os.system('chmod 644 ' + filename);
        
        #转移其它配置文件
        try:
            otherConf = setupPath + '/'+webserver+'/conf/vhost/default.conf';
            if os.path.exists(otherConf):
                dstOther = setupPath + '/panel/vhost/'+webserver+'/default.conf';
                public.ExecShell('\\cp -a -r ' + otherConf + ' ' + dstOther)
            
            otherConf = setupPath + '/'+webserver+'/conf/vhost/phpfpm_status.conf';
            if os.path.exists(otherConf):
                dstOther = setupPath + '/panel/vhost/'+webserver+'/phpfpm_status.conf';
                public.ExecShell('\\cp -a -r ' + otherConf + ' ' + dstOther)
        except:
            pass
        
        #迁移证书
        srcKey = setupPath + '/' + webserver + '/conf/key/' + siteArr[1] + '/key.key'
        if os.path.exists(srcKey):
            os.system('mkdir -p ' + letPath + '/' + siteArr[1]);
            public.ExecShell('\\cp -a -r ' + srcKey + ' ' + dstKey)
            
        srcCsr = setupPath + '/' + webserver + '/conf/key/'+ siteArr[1] + '/key.csr'
        if os.path.exists(srcCsr):
            public.ExecShell('\\cp -a -r ' + srcCsr + ' ' + dstCsr)
        
        print siteArr[1] + ' -> done.\n';
    
    
    
    #转移数据库
    arr = sql.query('select * from bt_default.bt_databases');
    for databaseArr in arr:
        sqldb.table('databases').add('name,username,password,accept,ps,addtime',(databaseArr[1],databaseArr[2],databaseArr[3],databaseArr[4],databaseArr[5],str(databaseArr[6])));
    
    
    #转移FTP
    arr = sql.query('select * from bt_default.bt_ftps');
    for ftpArr in arr:
        sqldb.table('ftps').add('name,password,path,status,ps,addtime',(ftpArr[1],ftpArr[2],ftpArr[3],ftpArr[4],ftpArr[5],str(ftpArr[6])))
    
    #转移用户
    #arr = sql.query('select * from bt_default.bt_user')[0]
    #sqldb.table('users').where('id=?',(1,)).save('username,password,login_ip,login_time',(arr[1],arr[2],arr[3],arr[4]))
    
    
    #转移日志
    arr = sql.query('select * from bt_default.bt_logs');
    for log in arr:
        sqldb.table('logs').add('type,log,addtime',(log[1],log[2],log[3]))
    
    
    #转移防火墙记录
    arr = sql.query('select * from bt_default.bt_firewall');
    ports = ['80','22','21','20','888','8888']
    for fw in arr:
        if str(fw[1]) in ports: continue;
        sqldb.table('firewall').add('port,ps,addtime',(fw[1],fw[2],fw[3]))
    
    
    #转移计划任务记录
    try:
        arr = sql.query('select * from bt_default.bt_crontab');
        for cron in arr:
            sqldb.table('crontab').add('name,type,where1,where_hour,where_minute,echo,addtime',(cron[1],cron[2],cron[3],cron[4],cron[5],cron[6],cron[7]))
    except:
        pass
    
    
    os.system('/etc/init.d/yunclient stop')
    os.system('chkconfig --del yunclient')
    os.system('rm -f /etc/init.d/yunclient')
    os.system('pkill -9 mysqld_safe')
    os.system('pkill -9 mysqld')
    os.system('sleep 2 && service mysqld start')
    if os.path.exists('/etc/init.d/nginx'): os.system('/etc/init.d/nginx reload');
    if os.path.exists('/etc/init.d/httpd'): os.system('/etc/init.d/httpd reload');
    
    print '=========================';
    print 'successful!'